C++ STL hash_map::begin() iterator program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the C++ hash_map::begin() which return an iterator addressing the first element in the hash_map in C++ programming

To show: How to use the C++ hash_map:: begin() iterator which return an iterator addressing the first element in the hash_map in C++ programming

 

// C++ STL hash_map::begin() iterator

#include <hash_map>

#include <iostream>

using namespace std;

using namespace stdext;

 

int main(void)

{

// container and iterators

hash_map <int, int> hm1;

hash_map <int, int> :: iterator hm1_Iter;

hash_map <int, int> :: const_iterator hm1_cIter;

typedef pair <int, int> Int_Pair;

 

// pushing/inserting data

hm1.insert(Int_Pair(3, 7));

hm1.insert(Int_Pair(9, 5));

hm1.insert(Int_Pair(2, 4));

hm1_cIter = hm1.begin();

 

// print the elements

cout<<"The first element of hm1 hash map is "<<hm1_cIter->first<<endl;

hm1_Iter = hm1.begin();

hm1.erase(hm1_Iter);

// the following 2 lines would err because the iterator is const

// hm1_cIter = hm1.begin();

// hm1.erase(hm1_cIter);

hm1_cIter = hm1.begin();

cout<<"After erase() operation, the first element of hm1 hash map is now "<<hm1_cIter->first<<endl;

return 0;

}

 

Output examples:

 

The first element of hm1 hash map is 9

After erase() operation, the first element of hm1 hash map is now 2

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index