c++ステップアップ(15)set/multiset容器、map容器
85046 ワード
文書ディレクトリ set/multiset容器 set構造と付与 サイズおよび交換 の挿入と削除検索と統計 setとmultisetの違い pairペア setコンテナソート
map/multimap容器 map構造と付与 サイズおよび交換 の挿入と削除検索と統計 mapコンテナソート
set/multisetコンテナ
すべての要素は、挿入時に自動的にソートされます. . を有することを可能にする.
set構造と付与
構築: 割り当て:
サイズとスワップ
統計
まとめ:統計サイズ--size 空かどうかを判断--empty 交換容器--swap 挿入と削除
関数のプロトタイプ:
まとめ:挿入--insert 削除--erase クリア--clear 検索と統計
setコンテナのデータおよび統計の検索
関数プロトタイプ に戻る.
まとめ:検索--find(反復器を返します) 統計--count(setの場合、結果は0または1) setとmultisetの違い setは重複データを挿入できないがmultisetは である. setデータを挿入すると同時に挿入結果が戻る、挿入が成功したかどうかを示す . multisetはデータを検出しないので、重複データ を挿入することができる.
pairペア
ペアで表示されるデータは、ペアで2つのデータを返すことができます.
2つの作成方法:
setコンテナ並べ替え
setコンテナのデフォルトのソートルールは小さいものから大きいものまで、ソートルールを変更する方法を把握します.主にシミュレーション関数を用いてソート規則を変更する. set内蔵データ型:
まとめ:シミュレーション関数を使用してsetコンテナのソートルールを指定します. setカスタムデータ型:
まとめ:カスタムデータ型の場合、setはデータを挿入するためにソートルールを指定する必要があります.
map/multimap容器 である. である.すべての要素は、要素のキー値に基づいて に自動的にソートされます.
本質:
メリット:は、 を迅速に見つけることができる.
mapとmultimapの違い: を繰り返すことを許さない. を繰り返すことができる.
map構造と付与
関数のプロトタイプ:
構築: 割り当て:
まとめ:mapのすべての要素はペアで表示され、データを挿入するときにペアを使用します.
サイズとスワップ
関数のプロトタイプ:
まとめ:統計サイズ--size 空かどうかを判断--empty 交換容器--swap 挿入と削除
関数のプロトタイプ:
まとめ: mapの挿入方式は多く、その1つを覚えれば である.挿入--insert 削除--erase クリア--clear 検索と統計
関数のプロトタイプ:
まとめ:検索--find(反復器を返します) 統計--count(mapの場合、結果は0または1) mapコンテナ並べ替え mapコンテナのデフォルトのソートルールはkey値に従って小さいから大きいまでソートするため、ソートルールを変更する方法を把握します.
シミュレーション関数を使用すると、ソートルールを変更できます.
まとめ:シミュレーション関数を用いてmapコンテナの並べ替え規則 を指定することができる.カスタムデータ型の場合、mapはsetコンテナ と同じソートルールを指定する必要があります.
set/multisetコンテナ
すべての要素は、挿入時に自動的にソートされます.
set/multiset
は関連容器に属し、底層構造は二叉木で実現される.set
とmultiset
の違い:set
容器に重複する元素があることを許さないmultiset
は、容器内に重複する元素set構造と付与
set
コンテナの作成と割り当て構築:
set st;
//デフォルトコンストラクタ:set(const set &st);
//コピーコンストラクタset& operator=(const set &st);
//リロード等号オペレータ#include
using namespace std;
#include
//set
void printSet(set<int>&s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s1;
// insert
s1.insert(10);
s1.insert(40);
s1.insert(30);
s1.insert(20);
s1.insert(30);
//
//set :
//set
printSet(s1);
//
set<int>s2(s1);
printSet(s2);
//
set<int>s3;
s3 = s2;
printSet(s3);
}
サイズとスワップ
統計
set
容器サイズおよび交換set
容器size();
//戻り容器中の元素の数empty();
//容器が空かどうかを判断swap(st);
//2つの集合容器を交換void printSet(set<int>&s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//
void test01()
{
set<int>s1;
//
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
//
printSet(s1);
//
if (s1.empty())
{
cout << "s1 " << endl;
}
else
{
cout << "s1 " << endl;
cout << "s1 : " << s1.size() << endl;
}
}
void test02()
{
set<int>s1;
//
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
set<int>s2;
//
s2.insert(100);
s2.insert(300);
s2.insert(200);
s2.insert(400);
cout << " : " << endl;
printSet(s1);
printSet(s2);
cout << " : " << endl;
s1.swap(s2);
printSet(s1);
printSet(s2);
}
まとめ:
関数のプロトタイプ:
insert(elem);
//容器に元素を挿入します.clear();
//すべての要素を消去erase(pos);
//pos反復器が指す要素を削除し、次の要素の反復器を返します.erase(beg, end);
//区間[beg,end)
のすべての要素を削除し、次の要素の反復器に戻ります.erase(elem);
//コンテナのelem値の要素を削除します.void printSet(set<int>&s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s1;
//
s1.insert(30);
s1.insert(10);
s1.insert(20);
s1.insert(40);
//
printSet(s1);
//
s1.erase(s1.begin());
printSet(s1);
//
s1.erase(30);
printSet(s1);
//
//s1.erase(s1.begin(), s1.end());
s1.clear();
printSet(s1);
}
まとめ:
setコンテナのデータおよび統計の検索
関数プロトタイプ
find(key);
//key
が存在するかどうかを検索し、存在する場合は、そのキーの要素反復器を返します.存在しない場合は、set.end();
count(key);
//統計key
の要素個数void test01()
{
//
set<int>s1;
//
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
set<int>::iterator pos = s1.find(30);
if (pos != s1.end())
{
cout << " :" << *pos << endl;
}
else
{
cout << " " << endl;
}
}
//
void test02()
{
//
set<int>s1;
//
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
// 30
int num = s1.count(30);
// set 0 1
cout << "num = " << num << endl;
}
まとめ:
void test01()
{
set<int>s;
pair<set<int>::iterator ,bool> ret = s.insert(10);
if (ret.second)
{
cout << " " << endl;
}
else
{
cout << " " << endl;
}
ret = s.insert(10);
if (ret.second)
{
cout << " " << endl;
}
else
{
cout << " " << endl;
}
multiset<int>ms;
//
ms.insert(10);
ms.insert(10);
ms.insert(10);
ms.insert(10);
for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
pairペア
ペアで表示されるデータは、ペアで2つのデータを返すことができます.
2つの作成方法:
pair p(value1, value2)
pairp = make_pair(value1, value2)
void test01()
{
//
pair<string, int>p("Tom", 20);
cout << " : " << p.first << " : " << p.second << endl;
//
pair<string, int>p2 = make_pair("Jerry", 30);
cout << " : " << p2.first << " : " << p2.second << endl;
}
setコンテナ並べ替え
setコンテナのデフォルトのソートルールは小さいものから大きいものまで、ソートルールを変更する方法を把握します.主にシミュレーション関数を用いてソート規則を変更する.
class MyCompare
{
public:
bool operator()(int v1,int v2)
{
return v1 > v2;
}
};
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(40);
s1.insert(20);
s1.insert(50);
s1.insert(30);
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//
set<int, MyCompare>s2;
s2.insert(10);
s2.insert(40);
s2.insert(20);
s2.insert(50);
s2.insert(30);
for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
まとめ:シミュレーション関数を使用してsetコンテナのソートルールを指定します.
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class comparePerson
{
public:
bool operator()(const Person&p1 , const Person&p2)
{
//
return p1.m_Age > p2.m_Age;
}
};
void test01()
{
//
set<Person, comparePerson>s;
// Person
Person p1(" ", 24);
Person p2(" ", 28);
Person p3(" ", 25);
Person p4(" ", 21);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
for (set<Person, comparePerson>::iterator it = s.begin(); it != s.end(); it++)
{
cout << " : " << it->m_Name << " : " << it->m_Age << endl;
}
}
まとめ:カスタムデータ型の場合、setはデータを挿入するためにソートルールを指定する必要があります.
map/multimap容器
map
のすべての要素はpair
pair
のうち第1の要素はkey
(キー値)であり、インデックスとして機能し、第2の要素はvalue
(実値)本質:
map/multimap
は関連容器に属し、底層構造は二叉木で実現される.メリット:
key
の値に基づいて、value
の値mapとmultimapの違い:
map
容器内にkey
値元素multimap
容器内にkey
値元素map構造と付与
関数のプロトタイプ:
構築:
map mp;
//mapデフォルトコンストラクタmap(const map &mp)
//コピーコンストラクタmap& operator=(const map &mp);
//リロード等号オペレータ#include
using namespace std;
#include
//map
void printMap(map<int, int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << (*it).first << " value = " << it->second << endl;
}
cout << endl;
}
void test01()
{
// map
map<int, int> m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(4, 40));
printMap(m);
//
map<int, int>m2(m);
printMap(m2);
//
map<int, int>m3;
m3 = m2;
printMap(m3);
}
int main() {
test01();
system("pause");
return 0;
}
まとめ:mapのすべての要素はペアで表示され、データを挿入するときにペアを使用します.
サイズとスワップ
関数のプロトタイプ:
size();
//戻り容器中の元素の数empty();
//容器が空かどうかを判断swap(st);
//2つの集合容器を交換void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
if (m.empty())
{
cout << "m " << endl;
}
else
{
cout << "m " << endl;
cout << "m : " << m.size() << endl;
}
}
void printMap(map<int, int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value = " << it->second << endl;
}
cout << endl;
}
//
void test02()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
map<int, int>m2;
m2.insert(pair<int, int>(4, 100));
m2.insert(pair<int, int>(5, 200));
m2.insert(pair<int, int>(6, 300));
cout << " : " << endl;
printMap(m);
printMap(m2);
m.swap(m2);
cout << " : " << endl;
printMap(m);
printMap(m2);
}
まとめ:
関数のプロトタイプ:
insert(elem);
//容器に元素を挿入します.clear();
//すべての要素を消去erase(pos);
//pos反復器が指す要素を削除し、次の要素の反復器を返します.erase(beg, end);
//区間[beg,end)
のすべての要素を削除し、次の要素の反復器に戻ります.erase(key);
//コンテナのkey値の要素を削除します.void printMap(map<int, int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value = " << it->second << endl;
}
cout << endl;
}
void test01()
{
map<int, int>m;
//
//
m.insert(pair<int, int>(1, 10));
//
m.insert(make_pair(2, 20));
//
m.insert(map<int, int>::value_type(3, 30));
//
m[4] = 40;
//[] , key value
//cout << m[4] << endl;
printMap(m);
//
m.erase(m.begin());
printMap(m);
m.erase(3); // key
printMap(m);
//
//m.erase(m.begin(), m.end());
m.clear();
printMap(m);
}
まとめ:
関数のプロトタイプ:
find(key);
//keyが存在するかどうかを検索し、存在する場合は、そのキーの要素の反復器を返します.存在しない場合はsetに戻る.end(); count(key);
//統計keyの要素個数void test01()
{
//
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(3, 40));
map<int,int>::iterator pos = m.find(3);
if (pos != m.end())
{
cout << " key = " << (*pos).first << " value = " << pos->second << endl;
}
else
{
cout << " " << endl;
}
//
//map key ,count 0 1
//multimap count 1
int num = m.count(3);
cout << "num = " << num << endl;
}
まとめ:
シミュレーション関数を使用すると、ソートルールを変更できます.
class MyCompare
{
public:
bool operator()(int v1,int v2)
{
//
return v1 > v2;
}
};
//map
void test01()
{
map<int, int , MyCompare>m;
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(5, 50));
m.insert(make_pair(3, 30));
m.insert(make_pair(4, 40));
for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value = " << it->second << endl;
}
}
まとめ: