stl sample

// 集合の簡単な使用例
#include < iostream>
#include < set>
#include < string>
using namespace std;

int main()
{
  set< string> s;

  // 文字列の集合を作成する
  s.insert("Telescope");
  s.insert("House");
  s.insert("Computer");
  s.insert("Mouse");

  set< string>::iterator p = s.begin();

  do {
    cout << *p << " ";
    p++;
  } while(p != s.end());
  cout << endl;

  p = s.find("Telescope");
  if(p != s.end())
    cout << "Found Telescope\n";

  return 0;
}

// setにクラスオブジェクトを格納する
#include < iostream>
#include < set>
#include < string>
using namespace std;

// 州と州都を格納するクラス
class state {
  string name;
  string capital;
public:
  state() { name = capital = ""; }

  // 州の名前だけを持つ一時的なオブジェクトを構築する
  state(string s) { name = s; capital = ""; }

  // 完全なstateオブジェクトを構築する
  state(string n, string c)
  {
    name = n;
    capital = c;
  }

  string get_name() { return name; }

  string get_capital() { return capital; }
};

// 州の名前でオブジェクトを比較する
bool operator<(state a, state b) 
{
  return a.get_name() < b.get_name();
}

// stateに対する<<演算子を定義する
ostream &operator<<(ostream &s, state &o)
{
  s << o.get_name() << "'s capital is ";
  s << o.get_capital() << "." << endl;
  
  return s;
} 

int main()
{
  set< state> states;

  // 集合を初期化する
  states.insert(state("Illinois", "Springfield"));
  states.insert(state("Wisconsin", "Madison"));
  states.insert(state("Missouri", "Jefferson City"));
  states.insert(state("California", "Sacramento"));
  states.insert(state("Nevada", "Carson City"));
  states.insert(state("Arkansas", "Little Rock"));
  states.insert(state("Alaska", "Juneau"));
  states.insert(state("West Virginia", "Charleston"));

  // 集合の要素を表示する
  set< state>::iterator p = states.begin();
  do {
    cout << *p;
    p++;
  } while(p != states.end());
  cout << endl;

  // 特定の州を検索する
  cout << "Looking for Wisconsin.\n";
  p = states.find(state("Wisconsin"));
  if(p != states.end()) {
    cout << "Found.\n";
    cout << *p;
  }
  else 
    cout << "State not in list.\n";

  return 0;
}

// multisetにクラスオブジェクトを格納する
#include < iostream>
#include < set>
#include < string>
using namespace std;

// 著者名をインデックスとして書籍に関する情報を格納するクラス
class book {
  string author;
  string title;
  string publisher;
  string date;
public:
  book() { title = author = publisher = date = ""; }

  // キーの著者名だけを持つ一時的なオブジェクトを構築する
  book(string a) { author = a;
                   title = publisher = date = ""; }

  // 完全なbookオブジェクトを格納する
  book(string t, string a, string p, string d)
  {
    title = t;
    author = a;
    publisher = p;
    date = d;
  }

  // 著者の名前だけを返す
  string get_author() { return author; }

  // 書籍に関するすべての情報を取得する
  void get_info(string &t, string &a, string &p, string &d)
  {
    t = title;
    a = author;
    p = publisher;
    d = date;
  }

};

// 著者名でオブジェクトを比較する
bool operator<(book a, book b) 
{
  return a.get_author() < b.get_author();
}

// bookに対する<<演算子を定義する
ostream &operator<<(ostream &s, book &o)
{
  string t, a, p, d;

  o.get_info(t, a, p, d);
  s << a << endl;
  s << t << endl;
  s << p << endl;
  s << d << endl;
  
  return s;
} 

int main()
{
  multiset< book> booklist;
  string a;

  // 集合を初期化する
  booklist.insert(book("C++: The Complete Reference",
                       "Schildt", "Osborne/McGraw-Hill",
                       "1998"));
  booklist.insert(book("C++ From The Ground Up",
                       "Schildt", "Osborne/McGraw-Hill",
                       "1998"));
  booklist.insert(book("Teach Yourself C++",
                       "Schildt", "Osborne/McGraw-Hill",
                       "1997"));
  booklist.insert(book("Executive Orders",
                       "Clancy", "Putnam",
                       "1996"));
  booklist.insert(book("The Hunt for Red October",
                       "Clancy", "Berkeley",
                       "1985"));
  booklist.insert(book("Red Storm Rising",
                       "Clancy", "Berkeley",
                       "1987"));
  booklist.insert(book("Sphere",
                       "Crichton", "Ballantine",
                       "1987"));
  booklist.insert(book("Jurassic Park",
                       "Crichton", "Ballantine",
                       "1990"));
  booklist.insert(book("The Lost World",
                       "Crichton", "Knopf",
                       "1995"));

  // 集合の要素を表示する
  multiset< book>::iterator p = booklist.begin();
  do {
    cout << *p << endl;
    p++;
  } while(p != booklist.end());
  cout << endl;

  // 特定のbookを探す
  cout << "Enter author: ";
  cin >> a;
  p = booklist.find(book(a));
  if(p != booklist.end()) {
    do {
      cout << *p << endl;
      p++;
    } while(p != booklist.upper_bound(a));
  }

  return 0;
}

// 選択形式の質問への回答をmultisetを使って記録する
#include < iostream>
#include < set>
using namespace std;

enum response { strong_agree, agree, disagree,
                strong_disagree, no_opinion };

int main()
{
  multiset< response> question;

  // 回答を作成する
  question.insert(response(strong_agree));
  question.insert(response(agree));
  question.insert(response(strong_agree));
  question.insert(response(strong_disagree));
  question.insert(response(disagree));
  question.insert(response(disagree));
  question.insert(response(no_opinion));
  question.insert(response(agree));
  question.insert(response(strong_agree));

  // 結果を表示する
  cout << "まったくそう思う: ";
  cout << question.count(strong_agree) << endl;

  cout << "そう思う: ";
  cout << question.count(agree) << endl;

  cout << "そう思わない: ";
  cout << question.count(disagree) << endl;

  cout << "まったくそう思わない: ";
  cout << question.count(strong_disagree) << endl;

  cout << "どちらとも言えない: ";
  cout << question.count(no_opinion) << endl;

  return 0;
}
1