关于map-set-multimap-multiset基本的知识点都打上注释了
map1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#include <iostream>
#include <map>
#include <set>
#include <string>
using
std::string;
using
std::cout;
using
std::cin;
using
std::endl;
using
std::map;
using
std::set;
int
main(){
//两个主要的关联容器类型是map和set
//标准库提供了8个关联容器
//map-关联数组-保存key-value
//set-关键字即值,即只保存关键字的容器
//简单的单词计数程序
map<string,
size_t
> word_count;
string word;
while
(cin>>word){
++word_count[word];
}
for
(
const
auto
&w:word_count){
//遍历map中的每个元素
//打印结果
cout<<w.first<<
" occurs "
<<w.second<<((w.second>1)?
" time"
:
" times"
)<<endl;
}
}
|
map2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
#include <iostream>
#include <map>
#include <set>
#include <string>
using
std::string;
using
std::cout;
using
std::cin;
using
std::endl;
using
std::map;
using
std::set;
int
main(){
//两个主要的关联容器类型是map和set
//标准库提供了8个关联容器
//map-关联数组-保存key-value
//set-关键字即值,即只保存关键字的容器
//使用set
map<string,
size_t
> word_count_2;
//空map
set<string> exclude = {
"The"
,
"But"
,
"And"
,
"Or"
,
"An"
,
"A"
,
"the"
,
"but"
,
"and"
,
"or"
,
"an"
,
"a"
};
string word_2;
while
(cin>>word_2){
//只统计不在exclude中的单词
if
(exclude.find(word_2)==exclude.end()){
++word_count_2[word_2];
}
}
for
(
const
auto
&w:word_count_2){
//遍历map中的每个元素
//打印结果
cout<<w.first<<
" occurs "
<<w.second<<((w.second>1)?
" time"
:
" times"
)<<endl;
}
}
|
map3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#include <iostream>
#include <map>
#include <set>
#include <string>
using
std::string;
using
std::cout;
using
std::cin;
using
std::endl;
using
std::map;
using
std::set;
int
main(){
//定义关联容器-必须指明关键字类型,同时指明值类型
//每个关联容器都定义了一个默认构造函数
//对关联容器进行列表初始化
map<string,string> authors = {{
"hi"
,
"wo"
},{
"ji"
,
"ba"
},{
"yl"
,
"ca"
}};
cout<<
"map关联数组还可以这样访问啊==="
<<authors[
"hi"
]<<
" "
<<authors[
"ji"
]<<endl;
for
(
auto
&a:authors){
cout<<
"key="
<<a.first<<
" "
<<
"value="
<<a.second<<
"\n"
<<endl;
}
}
|
map4.cpp-标准库类型pair
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
#include <set>
#include <string>
#include <utility>
#include <iostream>
#include <map>
#include <vector>
using
std::string;
using
std::cout;
using
std::cin;
using
std::endl;
using
std::map;
using
std::set;
using
std::pair;
using
std::vector;
int
main(){
//pair标准库类型,定义在头文件utility中
//pair使用默认构造函数对数据成员进行值初始化
pair<string,string> nonn;
pair<string,vector<
int
>> line;
pair<string,string> author {
"jjjj"
,
"sdsdsd"
};
pair<string,string> hello {
"sdsd"
,
"dddd"
};
//pair的数据成员是public的,成员分别命名为first和second
cout<<author.first<<
" "
<<author.second<<endl;
cout<<
"author和hello比较当first和second都相等时才会相等="
<<(author==hello?
"true"
:
"false"
)<<endl;
}
|
map5.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#include <set>
#include <string>
#include <utility>
#include <iostream>
#include <map>
#include <vector>
using
std::string;
using
std::cout;
using
std::cin;
using
std::endl;
using
std::map;
using
std::set;
using
std::pair;
using
std::vector;
int
main(){
//关联容器的操作
//容器关键字和值的类型
/*
key_type:此容器类型的关键字类型
mapped_type:每个关键字关联的类型,只适用于map
value_type:对于set,与key_type相同
对于map,为pair<const key_type,mapped_type>
*/
set<string>::key_type v2;
//v2是一个string
set<string>::value_type v1;
//v1是一个string
map<string,
int
>::value_type v3;
//v3是一个pair<const string,int>
map<string,
int
>::key_type v4;
//v4是一个string
map<string,
int
>::mapped_type v5;
//v5是一个int
///
map<string,
size_t
> word_count;
string word;
while
(cin>>word){
++word_count[word];
}
//关联容器的迭代器
auto
word_count_it = word_count.cbegin();
cout<<word_count_it->first<<endl;
cout<<word_count_it->second<<endl;
//word_count_it->first = "new key"; //key不能被重新赋值
//遍历关联容器
//使用迭代器
while
(word_count_it != word_count.cend()){
//打印结果
cout<<word_count_it->first<<
" occurs "
<<word_count_it->second<<((word_count_it->second>1)?
" time"
:
" times"
)<<endl;
++word_count_it;
}
}
|
map6.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#include <set>
#include <string>
#include <utility>
#include <iostream>
#include <map>
#include <vector>
using
std::string;
using
std::cout;
using
std::cin;
using
std::endl;
using
std::map;
using
std::set;
using
std::pair;
using
std::vector;
using
std::make_pair;
int
main(){
//使用insert插入元素
vector<
int
> ivec = {1,2,3,4,5,6,7};
set<
int
> set2;
set2.insert(ivec.cbegin(),ivec.cend());
//set2现在有7个元素
//接受一个初始化列表
set2.insert({8,9});
//set2现在又9个元素
//向map添加元素
//insert的四种方式
map<string,string> hellomap;
hellomap.insert({
"hello"
,
"world"
});
hellomap.insert(make_pair(
"hhh"
,
"ssd"
));
cout<<hellomap[
"hhh"
]<<endl;
hellomap.insert(pair<string,string>(
"sdsd"
,
"sdsdsdsd"
));
cout<<hellomap[
"sdsd"
]<<endl;
hellomap.insert(map<string,string>::value_type(
"sdsdsdsd"
,
"kkkkkkkkkkkk"
));
cout<<hellomap[
"sdsdsdsd"
]<<endl;
//insert的返回值
//insert返回的值依赖于容器类型和参数
//insert返回一个pair,pair的first成员是一个迭代器,指向具有给定关键字的元素;second成员是一个bool值,指出元素是
//插入成功还是已经存在容器内。如果关键字已经存在容器中,则insert什么都不做,返回值的second为false。如果不存在,
//bool为true
map<string,
size_t
> word_count;
string word;
while
(cin>>word){
auto
ret = word_count.insert({word,1});
if
(!ret.second){
++ret.first->second;
//递增计数器
}
}
for
(
const
auto
&w:word_count){
//遍历map中的每个元素
//打印结果
cout<<w.first<<
" occurs "
<<w.second<<((w.second>1)?
" time"
:
" times"
)<<endl;
}
}
|
multimap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
//using std::string;
//using std::cout;
//using std::cin;
//using std::endl;
//using std::map;
//using std::set;
//using std::vector;
//using std::multiset;
//using std::multimap;
using
namespace
::std;
int
main(){
//multimap中关键字可以重复,而map中关键字必须是唯一的。
//multiset中关键字可以重复,二set中关键字必须是唯一的。
//使用vector初始化一个set和一个multiset
vector<
int
> ivec;
for
(vector<
int
>::size_type i=0;i!=10;++i){
ivec.push_back(i);
ivec.push_back(i);
}
//初始化一个set
//ivec.cbegin()返回一个vector<int>::const_iterator类型的变量
set<
int
> iset(ivec.cbegin(),ivec.cend());
//初始化一个multiset
multiset<
int
> miset(ivec.cbegin(),ivec.cend());
cout<<
"set的长度=="
<<iset.size()<<endl;
cout<<
"multiset的长度=="
<<miset.size()<<endl;
multimap<string,string> hello_multimap;
//插入第一个元素
hello_multimap.insert({
"hello"
,
"sdsdsd"
});
//插入第二个元素
hello_multimap.insert({
"hello"
,
"sdsdsdsdsdsd"
});
cout<<hello_multimap.size()<<endl;
//在multimap中查找元素
auto
entries = hello_multimap.count(
"hello"
);
auto
it = hello_multimap.find(
"hello"
);
cout<<
"hello_multimap容器中第一个key=hello的value="
<<hello_multimap.find(
"hello"
)->second<<endl;
while
(entries){
cout<<it->second<<endl;
++it;
--entries;
}
}
|
map_remove.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
#include <set>
#include <string>
#include <utility>
#include <iostream>
#include <map>
#include <vector>
using
std::string;
using
std::cout;
using
std::cin;
using
std::endl;
using
std::map;
using
std::set;
using
std::pair;
using
std::vector;
using
std::make_pair;
int
main(){
//删除map中的元素
//使用erase函数
//对于允许重复关键字的容器,删除的数量可能大于1
map<string,string> hello_map{{
"f"
,
"f"
},{
"d"
,
"d"
},{
"a"
,
"a"
},{
"s"
,
"s"
}};
if
(hello_map.erase(
"f"
)){
//返回0不在此容器中
//返回1表示在容器中并删除成功
cout<<
"删除成功!!!"
<<
" "
<<endl;
}
//map的下标操作
hello_map[
"ni"
] =
"ni"
;
//上面这条语句执行的操作:
//在容器中搜索关键字为ni的元素,未找到
//将一个新的关键字-值对插入到容器中,值会进行值初始化,为字符串
//提取出新插入的元素,并将新值赋给他
cout<<hello_map[
"ni"
]<<endl;
if
(hello_map.find(
"d"
)==hello_map.end()){
cout<<
"key==d的元素不在该容器内!!!"
<<endl;
}
else
{
cout<<
"key==d的元素在该容器内!!!"
<<endl;
}
//使用find方法访问元素
set<
int
> iset = {1,2,3,4,5,6,7,8,9};
auto
it1=iset.find(1);
//返回一个迭代器,指向key==1的元素
auto
it2=iset.find(11);
//返回一个迭代器,其值为iset.end();
iset.count(2);
//返回key==2的元素的数量,对于不允许重复的容器,返回的值永远是0或1
cout<<
"it1指向的值为"
<<(*it1)<<
"\n"
<<
"it2指向的值为"
<<(*it2)<<endl;
/*
删除成功!!!
ni
key==d的元素在该容器内!!!
it1指向的值为1
it2指向的值为9
*/
}
|
=====END=====
FROM: http://my.oschina.net/xinxingegeya/blog/226110S