C++Primer学習ノート——関連容器初識
7209 ワード
関連コンテナ内の要素はキーワードに従ってアクセスおよび保存されます.
map key-valueペア(辞書の単語はkey、単語の意味はvalue)--関連配列、setはキーワードmultimap multisetのみを保存し、複数の要素が同じキーワードを持つことを許可する
map
set
関連コンテナの定義
map key-valueペア(辞書の単語はkey、単語の意味はvalue)--関連配列、setはキーワードmultimap multisetのみを保存し、複数の要素が同じキーワードを持つことを許可する
map
map
map<string, size_t> words;
string word;
while (cin >> word)
{
++words[word];//word
}
for (auto &s : words)
{
cout << s.first << "occurs" << s.second << endl;
//first ,second 。 pair
}
set
set
map<string, size_t> words;
set<string> exclude = { "i", "a", "is" };
string word;
while (cin >> word)
{
if (exclude.find(word) == exclude.end())
//find() ,
++words[word];
}
for (auto &s : words)
{
cout << s.first << "occurs" << s.second << endl;
}
//11.4
#include
#include
#include
#include
using namespace std;
int main()
{
map<string, size_t> words;
set<string> exclude = { "i", "a", "is" };
string word;
int index = 0;
while (cin >> word)
{
for (auto &s : word)
{
s=tolower(s);
if (ispunct(s))
{
s = '\0';// 、、、??????
}
}
if (exclude.find(word) == exclude.end())
++words[word];
}
for (auto &s : words)
{
cout << s.first << "occurs" << s.second << endl;
}
return 0;
}
関連コンテナの定義
map<string, size_t> words;
map<string,string> name={{"af","advs",
{"dsvsfda","fsa"}};
//{key,value}
set<string> exclude = { "i", "a", "is" };
//11.7
#include
#include
#include
#include
using namespace std;
int main()
{
map<string, vector<string>> familys = {
{ "shen", {"hongwei","faf"} }
};
string xingshi;
string name1, name2;
while (cin >> xingshi)
{
cin >> name1;
cin >> name2;
familys[xingshi].push_back(name1);
familys[xingshi].push_back(name2);
}
for (auto &s : familys)
{
vector<string> name;
name = s.second;
for (auto &c : name)
cout << s.first << " " << c << " " << endl;
}
return 0;
}