pair 和map对象的使用

pair对象:

pair 对象是记录一对值得容器,只能记录一对值。这对值得类型使用泛型的方式。如下:

 1 // ConsoleApplication32.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <utility>
 6 #include <iostream>
 7 #include <string>
 8 
 9 using namespace std;
10 
11 int main1()
12 {
13     pair<string, string> anon;
14     anon = make_pair("key001","value001");
15     cout << anon.first << endl;
16     cout << anon.second << endl;
17     anon = make_pair("key002", "value002");
18     cout << anon.first << endl;
19     cout << anon.second << endl;
20     anon.first = "key003";
21     anon.second = "value003";
22     cout << anon.first << endl;
23     cout << anon.second << endl;
24     system("PAUSE");
25     return 0;
26 }
View Code

 

首先使用pair来定义一个对象。要使用pair,需要添加一个头文件:#include <utility>

其次,使用make_pair方法,来添加一对元素。这个对象的first和second 分别指向key,value。

每一次调用make_pair方法,之前的key-value就没有了。

map对象:

map对象才是真正记录key-value对的容器。和java语言里面的map对象也比较类似。

 1 #include "stdafx.h"
 2 #include <map>
 3 #include <iostream>
 4 #include <string>
 5 #include <list>
 6 
 7 
 8 using namespace std;
 9 
10 int main()
11 {
12     map<string, string> map1;
13     map1["key001"] = "value001";
14     cout << map1["key001"] << endl;
15     map1.insert(map<string,string>::value_type("key002","value002"));
16     cout << map1["key002"] << endl;
17 
18     map1.insert(make_pair("key003","value003"));
19     cout << map1["key003"] << endl;
20 
21     pair<map<string, string>::iterator, bool> ret = map1.insert(make_pair("key004","value004"));
22     cout << ret.first->second << endl;
23 
24     int count = map1.erase("key004");
25     cout << count << endl;
26     cout << "begin to print:" << endl;
27     map<string, string>::iterator begin = map1.begin();
28     while (begin != map1.end())
29     {
30         cout << begin->first << endl;
31         ++begin;
32     }
33     system("PAUSE");
34     return 0;
35 } 
View Code

 

这里有几点:

  1. 头文件必须包含:#include <map>
  2. 使用下标操作时,如果key值不存在,则会自动创建这样一个key值并添加到map对象中,且值为空。
  3. 可以使用iterator对象来遍历map。
  4. insert 函数插入的是pair对象。换句话说,可以理解为map对象为N个pair对象的集合。

set 对象:

set对象是单个键值的集合。不能重复。

 1 #include "stdafx.h"
 2 #include <set>
 3 #include <iostream>
 4 #include <string>
 5 
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     set<string> set1;
12     set1.insert("001");
13     set1.insert("002");
14     set<string>::iterator ite = set1.find("001");
15     cout << *ite << endl;
16 
17     set<string>::iterator begin = set1.begin();
18     while (begin != set1.end())
19     {
20         cout << *begin << endl;
21         ++begin;
22     }
23     system("pause");
24     return 0;
25 }
View Code

 

要点如下:

  1. 头文件必须包含:#include <set>
  2. 使用find操作时,返回迭代器。
  3. 可以使用Iterator来遍历set。
  4. insert操作可以直接插入元素。
  5. 不支持下标操作。

 

转载于:https://www.cnblogs.com/lucy-lizhi/p/6673432.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值