首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++ 地图

2012-10-10 
c++map#includeiostream#include vector#includemapusing std::vectorusing std::cinusing std::c

c++ map

#include<iostream>
#include <vector>
#include<map>

using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::pair;
using std::map;

typedef pair<int, string> Author;
int playPair() {

?Author a1(1, "david");

?cout << a1.first << "? " << a1.second << endl;
?return 0;
}

void playMap() {
?typedef map<int, string> address;
?Author a1(2, "david");
?Author a2(3, "ccde");
?address mapAddress;

?

?//add element
?mapAddress.insert(a1);
?mapAddress.insert(a2);
?mapAddress.insert(address::value_type(4, "df43234"));

?

?//use iterator

?map<int, string>::iterator iter;

?

?//get element by key
?iter = mapAddress.find(2);
?cout << iter->first << " the first key of 2" << endl;


?//遍历map
?for (iter = mapAddress.begin(); iter != mapAddress.end();
???iter.operator ++()) {

??cout << "| " << iter->first << " | " << iter->second << endl;

?}

?

?//delete element
?iter = mapAddress.find(2);
?mapAddress.erase(iter);
?cout << "after erase" << endl;

?


?for (iter = mapAddress.begin(); iter != mapAddress.end();
???iter.operator ++()) {
??cout << "| " << iter->first << " | " << iter->second << endl;
?}

}

热点排行