Skip to content

Instantly share code, notes, and snippets.

@satishgoda
Last active December 11, 2015 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satishgoda/4619971 to your computer and use it in GitHub Desktop.
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
#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;
}
@satishgoda
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment