Skip to content

Instantly share code, notes, and snippets.

@zachelko
Created April 11, 2010 19:06
Show Gist options
  • Save zachelko/362984 to your computer and use it in GitHub Desktop.
Save zachelko/362984 to your computer and use it in GitHub Desktop.
#include <vector>
#include <iostream>
using namespace std;
int main() {
std::vector<int> foo;
// Add an item and then obtain an iterator to it
foo.push_back(3);
vector<int>::iterator current = foo.begin();
// Check the addresses of our iterator and the first item
std::cerr
<< "Address of the current iterator: " << &current << std::endl;
std::cerr
<< "Address of our first object: " << &foo.at(0) << std::endl;
// Add a new item to the end and update our iterator
current = foo.insert(foo.end(),4);
std::cerr
<< "Address of the current iterator (same as before): "
<< &current << std::endl;
// Check the address of the first item
std::cerr
<< "Address of our first object different from before): "
<< &foo.at(0) << std::endl;
// See if our iterator is valid
std::cerr << "current (4): " << *current << std::endl;
return 0;
}
// output
//
// Address of the current iterator:
// 0xbf809f40
// Address of our first object:
// 0x8ba1008
// Address of the current iterator after adding a new item:
// 0xbf809f40
// Address of our first object (different from before):
// 0x8ba1018
// current (4): 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment