Hikikomorichka's blog

By Hikikomorichka, history, 3 years ago, In English

I recently came across some very cool C++ tricks from some of the best coders which can help increase your code efficiency as well as make the code very neat , clean and easy to understand.I would be sharing some of these here.

1.Normally to find minimum among few elements we use something like this.

int a = min(x1,min(x2,min(x3,min(x4,x5))));

Instead of this we can use this.

int a = min({x2,x2,x3,x4,x5});

2.Using emplace_back in place of push_back

In C++ 11,emplace_back works just like push_back adding elements at the end of a vector. emplace_back is faster than push_back as push_back first creates a temporary variable and then adds it to the end of vector.

vector<int> v;
v.push_back(1); // Slower
v.emplace_back(2); //Faster

3.Using tuples

//Normally we use something like this
pair<int,pair<int,pair<char,int > > > p;
p=make_pair(1,make_pair(2,make_pair('a',3)));
// To access different elements we use something like this.
// TO access 1
int x=p.first;
// To access 2
int y=p.second.first;
//To access 'a'
char z=p.second.second.first;
//TO access 3
int s=p.second.second.second

// Instead of the above usage we can use tuples

tuple(int,int,char,int) t1=make_tuple(1,2,'a',3);
// TO access the ith element of a tuple we can use get<i> nameOfTuple
cout << get<0> t1 <<"  "<<get<2>t1<<endl; // Prints 1 and 'a'

4.Lambda Functions

Yes we have Lambda functions in C++ as well.Lambda functions are functions which do not have a name.Normally when we have to write a comparator function for usage whenever we want to sort an array according to a certain rule.Sometimes these functions are very simple and we do not need to explicitly write a comparator function for that.This can be simply done using lambda function.

bool cmp(int a,int b){
 return a>b;
}

vector<int> v;
v.emplace_back(1);
v.emplace_back(101);
v.emplace_back(5);
v.emplace_back(12);
v.emplace_back(-4);

// Now sorting

sort(v.begin(),v.end(),cmp); 


// The above statement can be simple written without the comparator function
// using the lambda function

sort(v.begin(),v.end(),[](int a,int b){return a>b;}); 

// A lambda function is written like   [](argument1,argument2,.....){//code}

5.Using Conditional Operators

Using conditional operators can make the code clean and efficient.

// Without conditional operators
if (a & 1) { // when a is odd
  a=a*2;
}
else{
  a=a+1;
}

// With conditional operators

a&1?a*=2:a+=1;

6. Bit Manipulation

// Conventional way to check ith bit set or not.
bool isSet(int num,int i){ // bit is counted from lsb to msb ..int this order 0th bit,1st bit...and so on
   for(int j=0;j<i;j++){
      num=num/2;
   }
  if(num&1)return true;
  return false;
}

// Using shift operator 
int t= num & (1<<i); // t is non zero (2^(i)) to be exact if ith bit is set else 0
// Setting ith bit
num|=(1<<i);
// flipping ith bit
num ^ = (1<<i); // ^ is the xor operator
// clearing the ith bit
num=num & ~(1<<i);

7.Using auto

Auto automatically determines the data type of the variable at run time and hence you need not to specify the data type of the variable.It also makes iterating over containers easy(code wise efficient).

 auto a = 1; // a will become 'int'
    auto b = 1LL; // b will become 'long long'
    auto c = 1.0; // c will become 'double'
    auto d = "variable"; // d will become 'string'


    vector<int > v;
    v.push_back(5);
    v.push_back(10);
    v.push_back(13);
    // or instead you can write vector<int>v={5,10,13};
    //  Now to print the contents of v  you can use
    for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) 
        cout << *it << ' ';
    // or you can use auto keyword to ease your work
    for(auto n:v)
    cout<<n<<" ";
    // Same goes for set , map and other data structures.
    set<pair<int,int> >s;
    s.insert({1,2}) // or you can use s.insert(make_pair(1,2))
    s.insert({5,6}) //  or s.insert(make_pair(5,6))
    // or simply you can write set<pair<int,int> >s={ {1,2} , {5,6} }
    // for printing the elements present in s
    for (set<pair<int,int>>::iterator it = s.begin(); it != s.end(); ++it) 
    cout << (*it).first << '  '<<(*it).second<<"\n";
    // or you can use auto to print its elements
    for(auto x:s)
    cout<<x.first<<" "<<x.second<<"\n";

Full text and comments »

  • Vote: I like it
  • +13
  • Vote: I do not like it