您的位置:首页 > 编程语言

map代码

2013-11-27 16:21 176 查看
#include <iostream>

#include <string>

#include <map>

#include <set>

using namespace std;

int main()

{

map<int,string> t_map1;

pair<map<int,string>::iterator,bool> t_pairResult;

//插入数据方式一

t_pairResult = t_map1.insert(pair<int,string>(1,"Hello"));

if(t_pairResult.second)

{

cout << "insert date success!" << endl;

cout << "map1 key : " << t_pairResult.first->first << " map1 value : " << t_pairResult.first->second << endl;

}

else

{

cout << "insert date failed!" << endl;

}

//插入数据方式二

t_pairResult = t_map1.insert(map<int,string>::value_type(2,"World"));

if(t_pairResult.second)

{

cout << "insert date success!" << endl;

cout << "map1 key : " << t_pairResult.first->first << " map1 value : " << t_pairResult.first->second << endl;

}

else

{

cout << "insert date failed!" << endl;

}

cout << "&&&&&&&&&&&&" << endl;

//插入数据方式三

t_map1[3] = "how are you?";

t_map1[3] = "How are you!";

t_map1[4] = "Fine,thank you!";

t_map1[5] = "What is your name?";

t_map1[9] = "OK";

t_map1[10] = "Good";

map<int,string>::iterator t_iter1;

map<int,string>::iterator t_iter2;

for(t_iter1 = t_map1.begin();t_iter1 != t_map1.end();t_iter1++)

{

cout << "map1 key : " << t_iter1->first << " map1 value : " << (*t_iter1).second << endl;

}

if(t_map1.count(4) == 1)

{

cout << "key 4 is in map1" << endl;

}

else

{

cout << "key 4 is not in map1" << endl;

}

if(t_map1.find(5) != t_map1.end())

{

cout << "find key 5 in map1 " << endl;

cout << "key 5 value : " << t_map1.find(5)->second << endl;

}

else

{

cout << "not found!" << endl;

}

t_iter2 = t_map1.lower_bound(5);

if(t_iter2 != t_map1.end())

{

cout << "t_iter2->first : " << t_iter2->first << " t_iter2->second : " << t_iter2->second << endl;

}

else

{

cout << "not found!" << endl;

}

cout << "%%%%%%%%%%%%%%" << endl;

t_iter2 = t_map1.upper_bound(5);

if(t_iter2 != t_map1.end())

{

cout << "t_iter2->first : " << t_iter2->first << " t_iter2->second : " << t_iter2->second << endl;

}

else

{

cout << "not found!" << endl;

}

cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;

pair<map<int,string>::iterator,map<int,string>::iterator> t_pair2;

t_pair2 = t_map1.equal_range(6);

cout << "t_pair2->first->first key : " << t_pair2.first->first << " t_pair2->first->second value : " << t_pair2.first->second << endl;

cout << "t_pair2->second->first key : " << t_pair2.second->first << " t_pair2->second->second value : " << t_pair2.second->second << endl;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: