在C++中map容器是一个键值对容器,也就是一个Key对应一个Value,例如:(1,"hello),或者也可以是这样:("hello2",2),其中一个值为Key,第二个值为Value,在上面的例子中1和“hello2"是Key,当然它们对应的”hello"和2就是Value。
1、map定义在头文件<map>中,使用时要#include <map>;
2、声明时要使用类似这样的形式:map<int,string> testmap;或者map<stirng,string> testmap;等等。
3、map中支持的函数有:
Iterators:
-
begin
- Return iterator to beginning (public member function )
-
end
- Return iterator to end (public member function )
-
rbegin
- Return reverse iterator to reverse beginning (public member function )
-
rend
- Return reverse iterator to reverse end (public member function )
-
cbegin
- Return const_iterator to beginning (public member function )
-
cend
- Return const_iterator to end (public member function )
-
crbegin
- Return const_reverse_iterator to reverse beginning (public member function )
-
crend
- Return const_reverse_iterator to reverse end (public member function )
Capacity:
-
empty
- Test whether container is empty (public member function )
-
size
- Return container size (public member function )
-
max_size
- Return maximum size (public member function )
Element access:
-
operator[]
- Access element (public member function )
-
at
- Access element (public member function )
Modifiers:
-
insert
- Insert elements (public member function )
-
erase
- Erase elements (public member function )
-
swap
- Swap content (public member function )
-
clear
- Clear content (public member function )
-
emplace
- Construct and insert element (public member function )
-
emplace_hint
- Construct and insert element with hint (public member function )
Observers:
-
key_comp
- Return key comparison object (public member function )
-
value_comp
- Return value comparison object (public member function )
Operations:
-
find
- Get iterator to element (public member function )
-
count
- Count elements with a specific key (public member function )
-
lower_bound
- Return iterator to lower bound (public member function )
-
upper_bound
- Return iterator to upper bound (public member function )
-
equal_range
- Get range of equal elements (public member function )
Allocator:
-
get_allocator
- Get allocator (public member function )
4、插入数据时一般使用下面几种方法:
map<int,string> testmap1;
map<string,int> testmap2;
(1) testmap2["a"] = 1; //当然你得保证有“a”这个Key!
(2) testmap2.insert(map<string,int>::value_type("b",2));
(3) testmap2.insert(pair<string,int>("c",3));
(4) testmap1.insert(make_pair<int,string>(4,"d"));
5、查询和修改数据时要使用跌代器
下面举个例子,把它弄明白了就差不多了。
程序要求用户输入一行文字,按回车结束一行后输出当前这一行中所有单词及单词出现的次数,是不是很简单?
#include "iostream"
#include "map"
#include "string"
#include "sstream"
using namespace std;
int main(int argc, char *argv[])
{
string input;
string word;
map<string,int> wordmap;
while(getline(cin,input))
{
istringstream line(input);
string word;
while(line>>word)
{
map<string,int>::iterator i=wordmap.find(word);
if(i==wordmap.end())
{
wordmap.insert(pair<string,int>(word,1));
}
else
{
wordmap[word]++;
}
}
for(map<string,int>::iterator i=wordmap.begin();i!=wordmap.end();i++)
{
string times = (i->second>1)?" times!":" time!";
cout<<"the word:"<<i->first<<" has appeared ";
cout<<i->second;
cout<<times<<endl;
}
wordmap.clear();
}
return 0;
}
像map这种键值对容器是很有用的,大家根据上面这个例子体会一下吧。