c++ステップアップ(15)set/multiset容器、map容器


文書ディレクトリ
  • set/multiset容器
  • set構造と付与
  • サイズおよび交換
  • の挿入と削除
  • 検索と統計
  • setとmultisetの違い
  • pairペア
  • setコンテナソート

  • map/multimap容器
  • map構造と付与
  • サイズおよび交換
  • の挿入と削除
  • 検索と統計
  • mapコンテナソート



  • set/multisetコンテナ
    すべての要素は、挿入時に自動的にソートされます.set/multisetは関連容器に属し、底層構造は二叉木で実現される.setmultisetの違い:
  • 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);
    }
    

    まとめ:
  • 統計サイズ--size
  • 空かどうかを判断--empty
  • 交換容器--swap
  • 挿入と削除
    関数のプロトタイプ:
  • 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);
    }
    

    まとめ:
  • 挿入--insert
  • 削除--erase
  • クリア--clear
  • 検索と統計
    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;
    }
    

    まとめ:
  • 検索--find(反復器を返します)
  • 統計--count(setの場合、結果は0または1)
  • setとmultisetの違い
  • setは重複データを挿入できないがmultisetは
  • である.
  • setデータを挿入すると同時に挿入結果が戻る、挿入が成功したかどうかを示す
  • .
  • multisetはデータを検出しないので、重複データ
  • を挿入することができる.
    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コンテナのデフォルトのソートルールは小さいものから大きいものまで、ソートルールを変更する方法を把握します.主にシミュレーション関数を用いてソート規則を変更する.
  • 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コンテナのソートルールを指定します.
  • 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);
    }
    

    まとめ:
  • 統計サイズ--size
  • 空かどうかを判断--empty
  • 交換容器--swap
  • 挿入と削除
    関数のプロトタイプ:
  • 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);
    }
    

    まとめ:
  • mapの挿入方式は多く、その1つを覚えれば
  • である.
  • 挿入--insert
  • 削除--erase
  • クリア--clear
  • 検索と統計
    関数のプロトタイプ:
  • 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;
    }
    

    まとめ:
  • 検索--find(反復器を返します)
  • 統計--count(mapの場合、結果は0または1)
  • mapコンテナ並べ替え
  • mapコンテナのデフォルトのソートルールはkey値に従って小さいから大きいまでソートするため、ソートルールを変更する方法を把握します.

  • シミュレーション関数を使用すると、ソートルールを変更できます.
    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;
    	}
    }
    

    まとめ:
  • シミュレーション関数を用いてmapコンテナの並べ替え規則
  • を指定することができる.
  • カスタムデータ型の場合、mapはsetコンテナ
  • と同じソートルールを指定する必要があります.