map
map
能够存储<key, value>
对的信息C++primer 5th
练习11.7
- 输入字符串,忽略
大小写
和点
代码
#include<iostream> #include<exception> #include<fstream> #include<sstream> #include<string> #include<vector> #include<iterator> #include<list> #include<deque> #include<stack> #include<queue> #include<concurrent_priority_queue.h> #include<algorithm> #include<numeric> #include<functional> //bind #include<map> using namespace std; bool Smaller(const int &a, const int &b) { return a <= b; } int main() { cout << "start!" << endl; map<string, size_t> word_cnt; string bword, aword; while (cin >> bword) { aword.clear(); for (auto iter = bword.begin(); iter != bword.end(); iter++) { if (*iter != '.') { aword.push_back(tolower(*iter)); } } ++word_cnt[ aword ]; } for (const auto &w : word_cnt) { cout << w.first << " occurs " << w.second << " times" << endl; } cout << "end!" << endl; system("pause"); return EXIT_SUCCESS; }
insert的使用
- 可以插入
pair
的数据类型 - 如果插入的
key
之前容器中已经有的话,则插入失败 - 如果每个
<key,value>
的first
值无法修改,second
值可以修改。 代码
#include<iostream> #include<exception> #include<fstream> #include<sstream> #include<string> #include<vector> #include<iterator> #include<list> #include<deque> #include<stack> #include<queue> #include<concurrent_priority_queue.h> #include<algorithm> #include<numeric> #include<functional> //bind #include<map> #include<set> using namespace std; int main() { cout << "start!" << endl; map<string, string> aus = { {"11", "22"}, {"33", "44"}}; aus.insert(pair<string,string>("55", "66")); aus.insert(pair<string, string>("55", "77")); auto m = aus.begin(); //m->first = "sdddd"; m->second = 455; cout << "end!" << endl; system("pause"); return EXIT_SUCCESS; }
- 可以插入
set
set
可以存储非重复的元素multiset
可以存储重复元素set
和multiset
是按照一定的存储顺序存储的代码
#include<iostream> #include<exception> #include<fstream> #include<sstream> #include<string> #include<vector> #include<iterator> #include<list> #include<deque> #include<stack> #include<queue> #include<concurrent_priority_queue.h> #include<algorithm> #include<numeric> #include<functional> //bind #include<map> #include<set> using namespace std; bool Smaller(const int &a, const int &b) { return a <= b; } int main() { cout << "start!" << endl; vector<int> vi; for (int i = 0; i < 10; i++) { vi.push_back( i ); vi.push_back(i); } set<int> si(vi.begin(), vi.end()); multiset<int> msi(vi.begin(), vi.end()); cout << vi.size() << endl; cout << si.size() << endl; cout << msi.size() << endl; cout << "end!" << endl; system("pause"); return EXIT_SUCCESS; }
pair
- pair可以存储两组数据成员。
- 这个例子是在
C++primer 5th
的练习11.12
代码
#include<iostream> #include<exception> #include<fstream> #include<sstream> #include<string> #include<vector> #include<iterator> #include<list> #include<deque> #include<stack> #include<queue> #include<concurrent_priority_queue.h> #include<algorithm> #include<numeric> #include<functional> //bind #include<map> #include<set> using namespace std; int main() { cout << "start!" << endl; vector<pair<string, int>> vp; int index = 0; int num; string str; string input; while (cin >> input) { if (index == 0) { str = input; index++; } else { num = atoi(input.c_str()); vp.push_back(pair<string, int>(str, num)); index = 0; } } for (int i = 0; i < vp.size(); i++) { cout << "first : " << vp[i].first << ", second : " << vp[i].second << endl; } cout << "end!" << endl; system("pause"); return EXIT_SUCCESS; }