标签:
六、查找并读取map中的元素
map容器提供了两个操作:count和find,用于检查某个键是否存在而不会插入该键:
|
不修改map对象的查询 |
|
|---|---|
|
m.count(k) |
返回m中k的出现次数 |
|
m.find(k) |
如果m容器中存在k索引的元素,则返回指向该元素的迭代器。如果不存在,则返回超出末端的迭代器 |
1、使用count检查map对象中某键是否存在
因为map容器只允许一个键对应一个实例,所以,对于map对象,count成员的返回值只能是1或0。
如果返回值非0,则可以使用下标操作符来获取该键所关联的值,而不必担心这样会在map容器中插入新元素:
int occurs = 0;
if (word_cnt.count("Dream"))
{
occurs = word_cnt["Dream"];
}
cout << occurs << endl;
2、读取元素而不插入元素
int occurs = 0;
map<string,int>::iterator iter = word_cnt.find("Dream");
if (iter != word_cnt.end())
{
occurs = iter -> second;
}
cout << occurs << endl;
//P316 习题10.16
map<string,vector<int> > str_vec;
map<string,vector<int> >::iterator iter = str_vec.find("dream");
七、从map对象中删除元素
|
从map对象中删除元素 |
|
|---|---|
|
m.erase(K) |
删除m中键为K的元素。返回size_type类型的值,表示删除的元素个数 |
|
m.erase(p) |
从m中删除迭代器p所指向的元素。p必须指向m中确实存在的元素,而且不能等于m.end()。返回void类型 |
|
m.erase(b,e) |
从m中删除一段范围内的元素,该范围由迭代器对b和e标记。b和e必须标记m中的一段有效范围:即b和e都必须指向m中的元素或最后一个元素的下一个位置。而且,b和e要么相等(此时删除的范围为空),要么b所指向的元素必须出现在e所指向的元素之前。返回void类型 |
int remCount = 0;
if (remCount = word_cnt.erase("Dream"))
{
cout << "Have removed " << remCount << " words" << endl;
}
else
{
cout << "Not found!" << endl;
}
erase函数返回被删除元素的个数。对于map容器,该值必然是0或者1。如果返回0值,则表示该元素并不存在于map容器中。
八、map对象的迭代遍历
与其他容器一样,map同样提供了begin和end运算,以生成用于遍历整个容器的迭代器:
cout << "First:\t\tSecond:" << endl;
map<string,int>::iterator iter = word_cnt.begin();
while (iter != word_cnt.end())
{
cout << iter -> first << "\t\t" << iter -> second << endl;
++ iter;
}
或:
cout << "First:\t\tSecond:" << endl;
typedef map<string,int>::iterator mapIter;
for (mapIter iter = word_cnt.begin(); iter != word_cnt.end(); ++iter)
{
cout << iter -> first << "\t\t" << iter -> second << endl;
}
在使用迭代器遍历map容器时,迭代器指向的元素按照键的升序排列。
九、“单词转换”map对象
1、问题:
给出一个string对象,把它转换为另一个string对象。本程序的输入是两个文件。第一个文件包括了若干单词对,每对的第一个单词将出现在输入的字符串中,而第二个单词则是用于输出。本质上,这个文件提供的是单词转换的集合——在遇到第一个单词时,应该将之替换为第二个单词。第二个文件则提供了需要转换的文本。如果单词转换文件的内容是:
'em them cuz because gratz grateful i I nah no pos supposed sez said tanx thanks wuz was
2、测试样例
而要转换的文本是:
nah i sez tanx cuz i wuz pos to not cuz i wuz gratz
则程序将产生如下输出结果:
no I said thanks because I was supposed to not because I was grateful
4、思路:
将被替换的单词作为键,而用作的替换的单词则作为其相应要替换的值。
5、程序:
#include <iostream>
#include <sstream>
#include <fstream>
#include <map>
using namespace std;
int main()
{
ifstream inFile("input1");
string line,firWord,secWord;
map<string,string> convMap;
while (getline(inFile,line))
{
istringstream strItem(line);
while (strItem >> firWord >> secWord)
{
convMap.insert(make_pair(firWord,secWord));
}
}
inFile.close();
inFile.clear();
inFile.open("input2");
string word;
while (getline(inFile,line))
{
istringstream strItem(line);
while (strItem >> word)
{
map<string,string>::iterator iter = convMap.find(word);
if (iter != convMap.end())
{
cout << iter -> second << ' ';
}
else
{
cout << word << ' ';
}
}
cout << endl;
}
}
//附上书上的原程序
int main(int argc, char **argv)
{
map<string, string> trans_map;
string key, value;
if (argc != 3)
throw runtime_error("wrong number of arguments");
ifstream map_file;
if (!open_file(map_file, argv[1]))
throw runtime_error("no transformation file");
while (map_file >> key >> value)
trans_map.insert(make_pair(key, value));
ifstream input;
if (!open_file(input, argv[2]))
throw runtime_error("no input file");
string line;
while (getline(input, line))
{
istringstream stream(line);
string word;
bool firstword = true;
while (stream >> word)
{
map<string, string>::const_iterator map_it =
trans_map.find(word);
if (map_it != trans_map.end())
word = map_it->second;
if (firstword)
firstword = false;
else
cout << " ";
cout << word;
}
cout << endl;
}
return 0;
}
//P319 习题10.18
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
using namespace std;
int main()
{
typedef string Family;
typedef vector<string> Names;
map<Family,Names> pepMap;
Family first;
string second;
ifstream inFile("input");
while (inFile >> first >> second)
{
Names child;
pair<map<Family,Names>::iterator,bool> res = pepMap.insert(make_pair(first,child));
res.first -> second.push_back(second);
}
inFile.close();
inFile.clear();
inFile.open("test");
string searchFamily;
while (inFile >> searchFamily)
{
map<Family,Names>::iterator iter = pepMap.find(searchFamily);
if (iter == pepMap.end())
{
cout << "NO this Family name" << endl;
}
else
{
cout << iter -> first << ":" << endl;
Names::iterator itName = iter -> second.begin();
while (itName != iter -> second.end())
{
cout << *itName << endl;
++ itName;
}
cout << endl;
}
}
}C++ Primer 学习笔记_36_STL实践与分析(10)--map类型(下
标签:
原文地址:http://blog.csdn.net/selfi_xiaowen/article/details/51334687