#include "stdafx.h" #include <map> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { setlocale(LC_ALL,"Rus"); std::map<char,int> first_map,second_map; first_map['i']=1; first_map['j']=2; second_map['m']=11; second_map.swap(first_map); for (std::map<char,int>::iterator it=second_map.begin(); it!=second_map.end(); ++it) { std::cout << it->first << " => " << it->second << '\n'; } cout<<endl; std::map<char,int> my_map; my_map['a']=2; my_map['b']=4; my_map['c']=7; cout<<my_map.size()<<endl; std::map<char,int>::iterator it_insert = my_map.begin(); my_map.insert(it_insert, std::pair<char,int>('d',10)); my_map.erase ('b'); for (std::map<char,int>::iterator it=my_map.begin(); it!=my_map.end(); ++it) { std::cout << it->first << " => " << it->second << '\n'; } cout<<endl; for (std::map<char,int>::reverse_iterator rit=my_map.rbegin(); rit!=my_map.rend(); ++rit) { std::cout << rit->first << " => " << rit->second << '\n'; } my_map.clear(); if(my_map.empty()){ cout<<"Empty"<<endl; } system("pause"); return 0; }
Результат работы программы Map на C++: