multiset

[機能]

  同じ値を持つ要素許す set です.Comp に対しては,比較を行う二項関数オブジェクト名を指定します.指定しないと less (昇順)が選択されます.( STL )
template <class Key, class Comp = less<Key>, class Allocator = allocator<Key> > class multiset
		

[使用方法]
#include <set>
using namespace std;
multiset <T> 変数名;
		
[メンバー関数等]

[演算子の多重定義]
    =  ==  <  <=  !=  >  >=
		

[使用例]

  1. multiset の使用方法です.
    #include <stdio.h>
    #include <set>
    using namespace std;
    
    void print(char *str, multiset<int> &s) {
    	if (s.empty())
    		printf("  コンテナ %s は空です\n", str);
    	else {
    		multiset<int>::iterator it;
    		printf("%s の要素数: %d\n", str, s.size());
    		for (it = s.begin(); it != s.end(); it++)
    			printf("  %d", *it);
    		printf("\n");
    	}
    }
    
    int main()
    {
    	multiset<int> s1, s2;
    	multiset<int>::iterator it;
    					// 要素を追加(サイズは自動的に増加)
    	printf("**初期設定**\n");
    	s1.insert(2);
    	s1.insert(0);
    	s1.insert(0);
    	s1.insert(0);
    	s1.insert(2);
    	print("s1", s1);
    					// 2 番目の要素の前に要素を追加
    	printf("**2 番目の要素の前に要素を追加**\n");
    	it = s1.begin();
    	it++;
    	s1.insert(it, 5);
    	print("s1", s1);
    					// 3 番目の要素,及び,要素 2 を削除
    	printf("**3 番目の要素,及び,要素 2 を削除**\n");
    	it = s1.begin();
    	it++;
    	it++;
    	s1.erase(it);
    	s1.erase(2);
    	print("s1", s1);
    					// 要素 0 の位置
    	printf("**要素 0 の位置**\n");
    	int n = s1.count(0);
    	printf("0 の検索: 数 = %d\n", n);
    	for (it = s1.lower_bound(0); it != s1.upper_bound(0); it++)
    		printf("  %d", *it);
    	printf("\n");
    					// 交換
    	printf("**交換**\n");
    	print("入れ替え前:s1", s1);
    	print("入れ替え前:s2", s2);
    	s1.swap(s2);
    	print("s1", s1);
    	print("s2", s2);
    					// 演算子で比較
    	printf("**比較**\n");
    	s1.insert(0);
    	s1.insert(5);
    	s1.insert(0);
    	print("s1", s1);
    	if (s1 == s2)
    		printf("  2 つのコンテナ内の要素はすべて等しい\n");
    					// すべての要素を削除
    	printf("**s1 のすべての要素を削除**\n");
    	s1.clear();
    	print("s1", s1);
    
    	return 0;
    }
    
    (出力)
    
    **初期設定**
    s1 の要素数: 5
      0  0  0  2  2
    **2 番目の要素の前に要素を追加**
    s1 の要素数: 6
      0  0  0  2  2  5
    **3 番目の要素,及び,要素 2 を削除**
    s1 の要素数: 3
      0  0  5
    **要素 0 の位置**
    0 の検索: 数 = 2
      0  0
    **交換**
    入れ替え前:s1 の要素数: 3
      0  0  5
      コンテナ 入れ替え前:s2 は空です
      コンテナ s1 は空です
    s2 の要素数: 3
      0  0  5
    **比較**
    s1 の要素数: 3
      0  0  5
      2 つのコンテナ内の要素はすべて等しい
    **s1 のすべての要素を削除**
      コンテナ s1 は空です
    			
  2. multiset の使用方法です(クラスオブジェクトを要素とした場合).
    #include <stdio.h>
    #include <set>
    using namespace std;
    
    class Test {
    	public:
    		char *str;
    		int n;
    		Test (char *str1, int n1) {
    			str = new char [strlen(str1)+1];
    			strcpy(str, str1);
    			n = n1;
    		}
    };
    
    bool operator< (Test t1, Test t2)
    {
    	return strcmp(t1.str, t2.str) < 0 ? true : false;
    }
    
    int main()
    {
    	Test t1("abc", 20), t2("xyz", 30), t3("ghq", 40);
    	multiset<Test> s;
    	multiset<Test>::iterator it;
    					// 要素を追加
    	s.insert(t1);
    	s.insert(t2);
    	s.insert(t2);
    	s.insert(t3);
    					// 出力
    	for (it = s.begin(); it != s.end(); it++)
    		printf("%s %d\n", (*it).str, (*it).n);
    
    	return 0;
    }
    
    (出力)
    
    abc 20
    ghq 40
    xyz 30
    xyz 30
    			
[参照]

setmultimap

ホームページ 目次 演習解答例目次 付録目次 索引