株式会社エス・スリー・フォー

STL Samples : <set>

重複を許さない集合

classset

重複を許す集合

classmultiset

source

#include <iostream>
#include <string>
#include <set>
#include "to_string.h"

using namespace std;

void std_set() {
  cout << "set" << endl;
  set<int> s;
  int i;
  for ( i = 0; i < 4; ++i )
    s.insert(i);

  cout << to_string(s.begin(),s.end()) << endl;

  for ( i = 2; i < 5; ++i ) {
    set<int>::iterator it = s.find(i);
    if ( it != s.end() )
      cout << "found " << i << " at:" << to_string(s.begin(), it, s.end()) << endl;
    else
      cout << i << " not found." << endl;  
  }
}

void std_multiset() {
  cout << "multiset" << endl;

  multiset<int> s;
  int i;
  for ( i = 0; i < 4; ++i ) s.insert(i);
  for ( i = 0; i < 3; ++i ) s.insert(i);
  for ( i = 0; i < 2; ++i ) s.insert(i);

  cout << to_string(s.begin(),s.end()) << endl;

  typedef multiset<int>::iterator iter;
  pair<iter,iter> range;
  for ( i = 0; i < 6; ++i ) {
    range = s.equal_range(i);
    if ( range.first != range.second ) {
      cout << "key:" << i << " at:" 
           << to_string(s.begin(), range, s.end()) << endl;
    } else {
      cout << i << " not found." << endl;  
    }
  }  
}