Last active
September 25, 2021 17:54
-
-
Save fernandozamoraj/5c154236803c97fd5df659b1c7e9664b to your computer and use it in GitHub Desktop.
C++ 11 Cheatshet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This cheat sheet highlights some important c++11 but not all of them | |
//To dig deeper I recommend this article | |
//https://www.codeproject.com/articles/570638/ten-cplusplus11-features-every-cplusplus-developer | |
//Also look into constexpr and noexcept | |
//Here is another link | |
//https://www.w3schools.in/cplusplus11-tutorial/noexcept-keyword/ | |
#include <iostream> | |
#include <vector> | |
#include <array> | |
#include <string> | |
#include <utility> | |
#include <functional> | |
using namespace std; | |
//swaps two values if compare returns > 0 | |
void swapLambda(int &a, int &b, function<int(int a, int b)> compare){ | |
if(compare(a,b) > 0){ | |
int temp = a; | |
a = b; | |
b = temp; | |
} | |
} | |
int main(int argc, char *argv[]){ | |
//auto to define type by assignment... | |
//like var in c# and java | |
auto isValid = false; //created boolean | |
auto age = 34; //creates int | |
//Initializers | |
int count1; //no init has gargage | |
int count2{}; //initilized to 0 | |
array<int,3> counts1{1,2,3}; | |
cout << "count1: " << count1 << " count2: " << count2 << " counts1[0]: " << counts1[0] << endl; | |
//copy over array | |
array<int, 3> counts2{}; //initialized to 0 | |
counts2 = std::move(counts1); | |
//for range | |
cout << "counts2: "; | |
for(auto i:counts2){ | |
cout << ", " << i; | |
} | |
cout << endl; | |
//Raw string literal... similar to @ in C# | |
string pathString = R"(c:\path\to\my\file)"; //NOTICE the parentheses | |
string escapeString = R"(\nThis will not produce a return line \n)"; | |
cout << pathString << endl; | |
cout << escapeString << endl; | |
//lambda example | |
auto x = 10; | |
auto y = 20; | |
cout << "x and y prior to swap " << x << " " << y << endl; | |
//pass anonymous function implementation to swapper | |
//swap if a > b | |
swapLambda(x, y, [](int a, int b){ return a - b; }); | |
cout << "x and y after swap (asc order) " << x << " " << y << endl; | |
//swap if a < b | |
swapLambda(x, y, [](int a, int b){ return b - a; }); | |
cout << "x and y after swap (desc order) " << x << " " << y << endl; | |
int *pVal1 = nullptr; | |
int *pVal2{}; | |
//*pVal1 != NULL | |
if(pVal1 == pVal2){ | |
//This executes... the else does not | |
cout << "pVal1 = nullptr == pVal2{} == nullptr" << endl; | |
} | |
else{ | |
cout << "pVal1 = nullptr != pVal2{} " << endl; | |
} | |
return 0; | |
} | |
//DELETE everything below this line | |
//Sample output | |
count1: 48 count2: 0 counts1[0]: 1 | |
counts2: , 1, 2, 3 | |
c:\path\to\my\file | |
\nThis will not produce a return line \n | |
x and y prior to swap 10 20 | |
x and y after swap (asc order) 10 20 | |
x and y after swap (desc order) 20 10 | |
pVal1 = nullptr == pVal2{} == nullptr |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Initial commit of c++ 11 cheat sheet | |
Added comments |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment