Last active
December 11, 2015 15:18
-
-
Save satishgoda/4619971 to your computer and use it in GitHub Desktop.
A quick example program that is using some of the new features that are part of C++11. Tested and compiled with gcc 4.7.1
This file contains 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
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
//---------------------------------------------------------------------------- | |
using VectorOfIntPointers = vector<const int *>; | |
void printPointersToElements(const VectorOfIntPointers& pointers) | |
{ | |
for (const auto& pointer : pointers) { | |
if (pointer != nullptr) | |
cout << pointer << " " << (*pointer) << endl; | |
else | |
cout << boolalpha << (bool) pointer << endl; | |
} | |
cout << endl << endl; | |
} | |
//---------------------------------------------------------------------------- | |
int main() | |
{ | |
vector<int> elements {1,2,3,4,5}; | |
vector<const int*> pointersToElements {elements.size(), nullptr}; | |
printPointersToElements(pointersToElements); | |
auto index = 0; | |
for(const auto& element : elements) | |
{ | |
auto address = &element; | |
pointersToElements[index++] = address; | |
} | |
printPointersToElements(pointersToElements); | |
reverse(begin(pointersToElements), end(pointersToElements)); | |
printPointersToElements(pointersToElements); | |
elements[2] = 300; | |
pointersToElements[4] = nullptr; | |
printPointersToElements(pointersToElements); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
false
false
false
false
false
0x2f76c0 1
0x2f76c4 2
0x2f76c8 3
0x2f76cc 4
0x2f76d0 5
0x2f76d0 5
0x2f76cc 4
0x2f76c8 3
0x2f76c4 2
0x2f76c0 1
0x2f76d0 5
0x2f76cc 4
0x2f76c8 300
0x2f76c4 2
false