+ 2

C++ cool tricks?

Can anyone suggest me tricks in c++ like: a=a+b-(b=a); //for swapping variables or: (condition) ? true : false; //coditions, or: string s = "abcde"; for (int x:s){ cout << x << endl; } //ranged for loop, or functions like: stoi(string, nullptr, 10) //converts any base to base 10.

27th Feb 2018, 10:25 AM
CuriousCI
CuriousCI - avatar
3 Antworten
+ 5
1) decltype() returns the type of a variable. Thus, you may use it when you do not have an idea about what the return type is going to be. Eg - decltype(variable) var2; 2) typeid() accepts types and lets you compare them as if they were variables. Eg - if(typeid(var2)==typeid(int)) cout<<"Int";
27th Feb 2018, 4:18 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 4
that's a lot of effort just to avoid semicolons ^^
27th Feb 2018, 11:38 AM
Alex
Alex - avatar
+ 4
auto variable = expression; This means that the type is determined looking at the expression.
27th Feb 2018, 1:02 PM
Timon Paßlick