【c++primer読書ノート】【第11章】関連容器
7444 ワード
1、関連コンテナタイプ
キーワード順に要素を保存
map
≪関連配列|Association Array|ldap≫:キーワードの保存-値対応
set
キーワードの値とキーワードのみを保存するコンテナ
multimap
キーワードが繰り返されるmap
multiset
キーワードの繰り返し可能set
むじゅんしゅうごう
unordered_map
hash関数で組織されたmap
unordered_set
hash関数で組織されたset
unordered_multimap
hash組織のmap:キーワードが繰り返し表示されます
unordered_multiset
hash組織のset:キーワードが繰り返し表示されます
mapとsetの例を用いて、単語プログラムを統計する:
2、関連コンテナの定義
mapを定義する場合は、キーワードタイプと値タイプを指定する必要があります.setを定義する場合は、キーワードタイプを指定するだけです.
関連コンテナの初期化:
3、秩序化容器map、multimap、set及びmultisetの場合、キーワードタイプは要素の比較方法を定義しなければならない.デフォルトでは、標準ライブラリはキーワードタイプ<演算子を使用して2つのキーワードを比較します.
4、pairタイプ
1つのpairは2つのデータメンバーを保存し、1つはfirstで、1つはsecondで、pairのデータメンバーはpublicです.
pairのデフォルトコンストラクション関数は、データ・メンバーの値を初期化します.
5.関連コンテナの追加タイプ別名
key_type
このコンテナタイプのキーワードタイプ
mapped_type
各キーワード関連のタイプはmapにのみ適用されます
value_type
set、key_の場合typeはmapに対してpair
6、setの反復器はconstです.setタイプはiteratorとconst_を同時に定義していますがiteratorですが、両方のタイプはset内の要素に読み取り専用アクセスのみを許可します.
7、要素の追加、要素の削除
実行結果:
8、mapの下付き操作
mapとunordered_mapコンテナは、下付き演算子と対応するat関数を提供します.mapは他の下付き演算子とは異なり、キーワードがコンテナにない場合、要素が作成されmapに挿入され、関連値が初期化されます.
9、要素へのアクセス
lower_boundとupper_boundは無秩序容器には適用されません.10、c++11は、比較演算子を使用して要素を整理するのではなく、ハッシュ関数とキーワードタイプの==演算子を使用する4つの無秩序関連コンテナを定義します.
無秩序コンテナunordered_の使用map統計単語
無秩序容器は組織形態でバケツのセットであり、各バケツには0つ以上の要素が保存されている.ハッシュ関数を使用して要素をバケツにマッピングします.同じキーワードの要素はすべて同じバケツにあります.無秩序容器の質量はハッシュ関数の質量とバケツの大きさと数に依存する.
12、キーワードタイプに対するコンテナの要求がない
無秩序コンテナはキーワード==を使用して要素を比較し、hashタイプのオブジェクトを使用して、各要素のhash値を生成します.
キーワードタイプがカスタムクラスタイプの無秩序コンテナを定義することはできません.コンテナとは異なり、hashテンプレートを直接使用するのではなく、独自のhashテンプレートを提供する必要があります.
自分のクラスをキーワードとするために、==演算子とhash値計算関数を自分で提供します.
キーワード順に要素を保存
map
≪関連配列|Association Array|ldap≫:キーワードの保存-値対応
set
キーワードの値とキーワードのみを保存するコンテナ
multimap
キーワードが繰り返されるmap
multiset
キーワードの繰り返し可能set
むじゅんしゅうごう
unordered_map
hash関数で組織されたmap
unordered_set
hash関数で組織されたset
unordered_multimap
hash組織のmap:キーワードが繰り返し表示されます
unordered_multiset
hash組織のset:キーワードが繰り返し表示されます
mapとsetの例を用いて、単語プログラムを統計する:
#include<iostream>
#include<map>
#include<set>
#include<sstream>
#include<string>
using namespace std;
int main(){
map<string, size_t> word_count;
set<string> exclude{ "an", "the" };
string str = "although before an and aaa although the aaa";
stringstream ss(str);
string word;
while (ss >> word){
if (exclude.find(word) == exclude.end()) // exclude
++word_count[word];
}
for (const auto& w : word_count)
cout << w.first << " occurs " << w.second << ((w.second>1) ? " times" : " time") << endl;
system("pause");
return 0;
}
2、関連コンテナの定義
mapを定義する場合は、キーワードタイプと値タイプを指定する必要があります.setを定義する場合は、キーワードタイプを指定するだけです.
関連コンテナの初期化:
map<string,size_t> wor(word_count); //
map<string,string> authors={{"joyce","jamese"},{"Austen","jane"}};// ,VS2013
map<string,string> au(authors);// authors au
3、秩序化容器map、multimap、set及びmultisetの場合、キーワードタイプは要素の比較方法を定義しなければならない.デフォルトでは、標準ライブラリはキーワードタイプ<演算子を使用して2つのキーワードを比較します.
4、pairタイプ
1つのpairは2つのデータメンバーを保存し、1つはfirstで、1つはsecondで、pairのデータメンバーはpublicです.
pairのデフォルトコンストラクション関数は、データ・メンバーの値を初期化します.
5.関連コンテナの追加タイプ別名
key_type
このコンテナタイプのキーワードタイプ
mapped_type
各キーワード関連のタイプはmapにのみ適用されます
value_type
set、key_の場合typeはmapに対してpair
6、setの反復器はconstです.setタイプはiteratorとconst_を同時に定義していますがiteratorですが、両方のタイプはset内の要素に読み取り専用アクセスのみを許可します.
set<int> iset={1,2,3,4,5};
set<int>::iterator it=iset.begin();
if(it!=iset.end()){
*it=42; // :set
cout<<*it<<endl; //
}
7、要素の追加、要素の削除
#include<iostream>
#include<map>
#include<string>
#include<utility>
using namespace std;
int main(){
map<string, size_t> word_count;
map<string, size_t> wc = { { "ff", 2 }, { "tt", 1 } }; // VS2013
word_count.insert({ "aa", 1 }); //c.insert(v), VS2013
word_count.insert(make_pair("cc", 1)); //c.insert(v)
word_count.insert(pair<string, size_t>("bb", 1)); //c.insert(v)
word_count.insert(map<string, size_t>::value_type("aa", 1)); //c.insert(v)
word_count.insert(wc.begin(), wc.end());//c.insert(b,e),b e , c::value_type , void
word_count.emplace("qq", 5);//c.emplace(args), map set, c 。
// pair, , , bool
for (const auto& w : word_count)
cout << w.first << " " << w.second << endl;
cout << "------------------------------" << endl;
word_count.erase("bb"); //c.erase(k), c k , size_type ,
word_count.erase(word_count.begin());//c.erase(p), c p , p
for (const auto& w : word_count)
cout << w.first << " " << w.second << endl;
cout << "------------------------------" << endl;
word_count.erase(++word_count.begin(), --word_count.end());//c.erase(b,e), b e , e
for (const auto& w : word_count)
cout << w.first << " " << w.second << endl;
system("pause");
return 0;
}
実行結果:
8、mapの下付き操作
mapとunordered_mapコンテナは、下付き演算子と対応するat関数を提供します.mapは他の下付き演算子とは異なり、キーワードがコンテナにない場合、要素が作成されmapに挿入され、関連値が初期化されます.
map<string,size_t> word_count;
word_count["aa"]=1; // "aa" , , 1
cout<<word_count["aa"]<<endl; // "aa" , 1
++word_count["aa"]; // , 1
cout<<word_count["aa"]<<endl;// "aa" , 2
9、要素へのアクセス
#include<iostream>
#include<set>
#include<string>
#include<utility>
using namespace std;
int main(){
int ia[]={1,2,3,4,5,6,7,8,9};
set<int> iset(begin(ia),end(ia));
iset.find(1); // , key==1
iset.find(11); // , iset.end()
iset.count(1); //c.count(k), k , 1
iset.count(11); //c.count(k), k , 0
auto low=iset.lower_bound(2);//c.lower_bound(k), , k
auto upper=iset.upper_bound(5);//c.lower_bound(k), , k
while(low!=upper)
cout<<*low++<<" ";
auto range=iset.equal_range(3); //c.equal_range(k), pair, k
cout<<endl;
system("pause");
return 0;
}
lower_boundとupper_boundは無秩序容器には適用されません.10、c++11は、比較演算子を使用して要素を整理するのではなく、ハッシュ関数とキーワードタイプの==演算子を使用する4つの無秩序関連コンテナを定義します.
無秩序コンテナunordered_の使用map統計単語
#include<iostream>
#include<unordered_map>
#include<set>
#include<sstream>
#include<string>
using namespace std;
int main(){
unordered_map<string, size_t> word_count; // ,
set<string> exclude{ "an", "the" };
string str = "although before an and aaa although the aaa";
stringstream ss(str);
string word;
while (ss >> word){
if (exclude.find(word) == exclude.end()) // exclude
++word_count[word];
}
for (const auto& w : word_count)
cout << w.first << " occurs " << w.second << ((w.second>1) ? " times" : " time") << endl;
system("pause");
return 0;
}
11、管理バケツ無秩序容器は組織形態でバケツのセットであり、各バケツには0つ以上の要素が保存されている.ハッシュ関数を使用して要素をバケツにマッピングします.同じキーワードの要素はすべて同じバケツにあります.無秩序容器の質量はハッシュ関数の質量とバケツの大きさと数に依存する.
#include<iostream>
#include<unordered_set>
using namespace std;
int main(){
unordered_multiset<int> iset{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 4, 5, 6, 7 };
cout << iset.bucket_count() << endl; //
cout << iset.max_bucket_count() << endl;//
cout << iset.bucket_size(1) << endl; // 1
cout << iset.bucket(4) << endl; // 4
cout << iset.load_factor() << endl; // , float
cout << iset.max_load_factor() << endl;// , float 。 load_factor <= max_load_factor
iset.rehash(20); // , bucket_count>=20 bucket_count>size/max_load_factor()
iset.reserve(20);// , iset 20 rehash
system("pause");
return 0;
}
12、キーワードタイプに対するコンテナの要求がない
無秩序コンテナはキーワード==を使用して要素を比較し、hash
キーワードタイプがカスタムクラスタイプの無秩序コンテナを定義することはできません.コンテナとは異なり、hashテンプレートを直接使用するのではなく、独自のhashテンプレートを提供する必要があります.
自分のクラスをキーワードとするために、==演算子とhash値計算関数を自分で提供します.