//Chapter 10
#if 0
//Chapter 10 Exercise 10.1~2
#include <iostream>
#include <cstdlib>
#include <utility>
#include <vector>
#include <string>
using namespace std;
int main()
{
#if 0
//version 1
string sdata;
int idata;
pair<string, int> sidata;
vector< pair<string, int> > vsidata;
cout << "please input some data: string int" << endl;
while(cin >> sdata >> idata)
{
sidata = make_pair(sdata, idata);//此种方法更能表明生成的是pair对象,更容易阅读理解
//cout << sidata.first << " " <<sidata.second << endl;
vsidata.push_back(sidata);
}
#endif
#if 0
//version 2
string sdata;
int idata;
pair<string, int> sidata;
vector< pair<string, int> > vsidata;
cout << "please input some data: string int" << endl;
while(cin >> sdata >> idata)
{
sidata = pair<string, int> (sdata, idata);
vsidata.push_back(sidata);
}
#endif
#if 1
//version 3
typedef pair<string, int> NameAge;//此种方法少了对局部变量的定义
NameAge sidata;
vector< NameAge > vsidata;
cout << "please input some data: string int" << endl;
while (cin >> sidata.first >> sidata.second)
{
vsidata.push_back(sidata);
}
#endif
for (vector< pair<string, int> >::iterator iter = vsidata.begin(); iter != vsidata.end(); iter++)
{
cout << iter->first << " " << iter->second << endl;
}
return 0;
}
#endif
#if 0
//Chapter 10 Exercise 10.9
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
map<string, int> word_count;
string word;
while(cin >> word)
{
++word_count[word];
}
for(map<string, int>::iterator iter = word_count.begin(); iter != word_count.end(); ++iter)
{
cout << iter->first << " " << iter->second << endl;//这边的iter解引用后返回的是pair<string, int>对象
}
}
#endif
#if 0
//Chapter 10 Exercise 10.12
//下标操作的程序更为简洁,更容易编写和阅读,
//而insert函数的返回值的使用比较复杂。
//但使用insert函数可以避免使用下标操作所带来的副作用,
//即避免对新插入元素不必要的值初始化,
//而且可以显示表示元素的插入(下标操作是隐式表示元素的插入),
//各有优缺点。
#include <iostream>
#include <string>
#include <map>
#include <utility>
using namespace std;
int main()
{
map<string, int> word_count;
string word;
cout << "please input some words(Ctr + Z to end)" << endl;
while (cin >> word)
{
pair< map<string, int>::iterator, bool > ret = word_count.insert(make_pair(word, 1));
if (!ret.second)
{
++(ret.first->second);
}
}
for (map<string, int>::iterator iter = word_count.begin(); iter != word_count.end(); ++iter)
{
cout << iter->first << " " << iter->second << endl;
}
return 0;
}
#endif
#if 0
//Chapter 10 Exercise 10.17
//在dos下到test.exe的目录下输入test.exe transfile.txt inputfile.txt
//其中transfile.txt是单词对转换文件,inputfile.txt是待转换的文本文件
#include <iostream>
#include <string>
#include <map>
#include <utility>
#include <fstream>
#include <sstream>
using namespace std;
ifstream& open_file(ifstream& in, const string& file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}
int main(int argc, char **argv)
{
if (argc != 3)
{
throw runtime_error("wrong number of arguments");
}
ifstream trans_file;
if (!open_file(trans_file, argv[1]))
{
throw runtime_error("no transformation file");
}
map<string, string> trans_map;
string key, val;
while (trans_file >> key >> val)
{
trans_map.insert(make_pair(key, val));
}
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;
}
#endif
#if 0
//Chapter 10 Exercise 10.18
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <cstdlib>
using namespace std;
int main()
{
string surname, childname;
map< string, vector<string> > children;
do
{
cout << "Enter surname(Ctrl + z to end)" << endl;
cin >> surname;
if (!cin)
{
break;
}
vector<string> chd;
pair< map<string, vector<string> >::iterator, bool > ret = children.insert(make_pair(surname, chd));
if (!ret.second)
{
cout << "repeated surname: " << surname << endl;
continue;
}
cout << "Enter children's name(Ctrl + z to end)" << endl;
while (cin >> childname)
{
ret.first->second.push_back(childname);
}
cin.clear();//使得输入流重新有效
} while (cin);
cin.clear();
cout << "Enter a surname you want to search" << endl;
cin >> surname;
map< string, vector<string> >::const_iterator map_it = children.find(surname);
if (map_it != children.end())
{
for (vector<string>::const_iterator iter = map_it->second.begin(); iter != map_it->second.end(); ++iter)
{
cout << *iter << endl;
}
}
else
cout << "the surname you search is not in the map" << endl;
system("pause");
return 0;
}
#endif
#if 0
//Chapter 10 Exercise 10.19
//map可以用在字典、电话薄、商品价目表中,
//可以使用下标操作或者insert函数插入元素,使用find函数读取元素
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <utility>
using namespace std;
int main()
{
string surname, childname, childbirth;
typedef pair<string, string> ChildType;
map< string, vector< ChildType > > children;
do
{
cout << "please input surname(Ctrl + z to end)" << endl;
cin >> surname;
if (!cin)
{
break;
}
vector< ChildType > chd;
pair< map< string, vector< ChildType > >::iterator, bool > ret = children.insert(make_pair(surname, chd));
if (!ret.second)
{
cout << "Repeat surname:" << surname << endl;
continue;
}
cout << "Enter childname and birthday(Ctrl + z to end)" << endl;
while(cin >> childname >> childbirth)
{
ret.first->second.push_back(make_pair(childname, childbirth));
}
cin.clear();
} while (cin);
cin.clear();
cout << "Enter the surname you wanna search" << endl;
cin >> surname;
map< string, vector< ChildType > >::const_iterator map_it = children.find(surname);
if (map_it != children.end())
{
for (vector< ChildType >::const_iterator iter = map_it->second.begin(); iter != map_it->second.end(); ++iter)
{
cout << iter->first << " " << iter->second << endl;
}
}
else
cout << "the surname you wanna search is not exist" << endl;
}
#endif
C++ Primer Chapter 10 关联容器之map
最新推荐文章于 2021-12-24 18:08:16 发布