【C++】STL for TopCoder

目次

vector

   1 #include <iostream>
   2 #include <vector>
   3 using namespace std;
   4 
   5 int main() {
   6         /*
   7          * vector<>の宣言
   8          * 空のvector<>が作られる
   9          */
  10         vector<int> v;
  11 
  12         /*
  13          * 要素の追加
  14          * v.push_back(val)
  15          * vectorの最後尾に値が追加される
  16          */
  17         v.push_back(10);
  18         for (int i = 0; i < 20; i++)
  19         {
  20                 v.push_back(i);
  21         }
  22 
  23         /*
  24          * vectorの要素数
  25          * v.size()
  26          */
  27         cout << "v contains " << v.size() << " integers" << endl;
  28 
  29         /*
  30          * 要素へのアクセス
  31          * v.at(i)
  32          * nのi番目の要素が取り出される
  33          */
  34         int x = v.at(0);
  35         cout << x << endl;
  36 
  37         /*
  38          * 配列のような書き方も出来る(が、atの方が早いらしい)
  39          */
  40         for (int i = 1; i < v.size(); i++)
  41         {
  42                 cout << v[i] << " ";
  43         }
  44         cout << endl;
  45         
  46         return 0;
  47 }

string

   1 #include <iostream>
   2 #include <string>
   3 using namespace std;
   4 
   5 int main() {
   6         /*
   7          * stringの宣言
   8          */
   9         string str;             //空文字列
  10         string str2("Hello World!");    //初期値あり
  11 
  12         /*
  13          * stringの演算子
  14          */
  15         str = "I'm hungry";             //代入
  16         cout << str << endl;
  17 
  18         char ch = str[2];               //要素(char)へのアクセス(str.at(2)でも可)
  19         putchar(ch);
  20         cout << endl;
  21 
  22         str = str + " and thirsty";     //結合(str += " and thirsty"; でも可)
  23         cout << str << endl;
  24 
  25         if (str == str2)        //比較(完全一致)
  26         {
  27                 cout << "str is equal to str2" << endl;
  28         } else {
  29                 cout << "str is NOT equal to str2" << endl;
  30         }
  31 
  32         str = "abc";
  33         str2 = "bac";
  34         if (str < str2)         //比較(辞書順)
  35         {
  36                 cout << "str is less than str2" << endl;
  37         } else {
  38                 cout << "str is NO less than str2" << endl;
  39         }
  40 
  41         /*
  42          * stringの文字数
  43          * str.size()
  44          */
  45         cout << "str contains " << str.size() << " characters" << endl; //str.length()も使える
  46 
  47         /*
  48          * その他、文字列の出現位置の検索、トリムなどの機能もある(詳細は割愛)
  49          */
  50         str = "abcdefg";
  51         str2 = "cde";
  52         cout << "found str2 in str (from index of " << str.find(str2) << ")" << endl;
  53 
  54         cout << str.substr(3,3) << endl;
  55         
  56         return 0;
  57 }

map

   1 #include <iostream>
   2 #include <string>
   3 #include <map>
   4 using namespace std;
   5 
   6 int main() {
   7         /*
   8          * map<,>の宣言
   9          * 平衡二分木で実装されているらしい(のでけっこう遅い)
  10          */
  11         map<string,int> m;      //空のmap(この場合stringからintへの対応)
  12 
  13         /*
  14          * 要素の追加
  15          * おそらくメソッドを使ったほうが早いが、めんどくさいので[]演算子を使う
  16          */
  17         m["one"] = 1;
  18         m["two"] = 2;
  19         m["three"] = 3;
  20 
  21         /*
  22          * 要素へのアクセス
  23          */
  24         cout << m["two"] << endl;
  25 
  26         /*
  27          * 要素の削除
  28          * m.erase(key);
  29          */
  30         m.erase("one");
  31 
  32         /*
  33          * mapの要素数
  34          * m.size();
  35          */
  36         cout << "m contains " << m.size() << " elements" << endl;
  37 
  38         /*
  39          * 要素の検索
  40          * m.find(key);
  41          * keyが存在しない場合に、mapの最後尾へのイテレータが返ってくるので、
  42          * keyの有無を確認する場合に使える
  43          */
  44         if (m.find("one") == m.end())
  45         {
  46                 cout << "one does not exist as a key" << endl;
  47         }
  48 
  49         /*
  50          * 要素の列挙
  51          * イテレータを使用して、mapの要素を列挙する
  52          */
  53         m["five"] = 5;
  54         m["four"] = 4;
  55         m["ten"] = 10;
  56         m["eight"] = 8;
  57         map<string,int>::iterator it;   //イテレータの宣言
  58         for (it = m.begin(); it != m.end(); ++it)
  59         {
  60                 cout << it->first << " -> " << it->second << endl;      //ポインタのように使う
  61         }
  62 
  63         return 0;
  64 }

stack,queue,priority_queue

   1 #include <iostream>
   2 #include <stack>
   3 #include <queue>
   4 using namespace std;
   5 
   6 int main() {
   7         /**
   8          * stack<>,queue,<>priority_queue<>の宣言
   9          */
  10         stack<int> st;
  11         queue<int> qu;
  12         priority_queue<int> pqu_desc;   //デフォルトでは降順
  13         priority_queue<int, vector<int>, greater<int> > pqu_asc;        //昇順のpriority_queue
  14 
  15         /*
  16          * 要素の追加
  17          */
  18         st.push(1);
  19         st.push(2);
  20         st.push(3);
  21 
  22         qu.push(1);
  23         qu.push(2);
  24         qu.push(3);
  25 
  26         pqu_desc.push(2);
  27         pqu_desc.push(3);
  28         pqu_desc.push(1);
  29 
  30         pqu_asc.push(2);
  31         pqu_asc.push(3);
  32         pqu_asc.push(1);
  33 
  34         /**
  35          * 一番上の要素にアクセス
  36          * st.top();
  37          * qu.front();
  38          * pqu_desc.top();
  39          *
  40          * 一番上の要素を取り除く
  41          * pop();
  42          *
  43          * 要素が空か判定(空ならtrue)
  44          * empty();
  45          */
  46         cout << "stack (First In Last Out)" << endl;
  47         while (!(st.empty()))
  48         {
  49                 cout << st.top() << endl;
  50                 st.pop();
  51         }
  52 
  53         cout << "queue (First In First Out)" << endl;
  54         while (!(qu.empty()))
  55         {
  56                 cout << qu.front() << endl;
  57                 qu.pop();
  58         }
  59 
  60         cout << "priority_queue (DESC)" << endl;
  61         while (!(pqu_desc.empty()))
  62         {
  63                 cout << pqu_desc.top() << endl;
  64                 pqu_desc.pop();
  65         }
  66 
  67         cout << "priority_queue (ASC)" << endl;
  68         while (!(pqu_asc.empty()))
  69         {
  70                 cout << pqu_asc.top() << endl;
  71                 pqu_asc.pop();
  72         }
  73         return 0;
  74 }

algorithm

   1 #include <iostream>
   2 #include <vector>
   3 #include <algorithm>
   4 using namespace std;
   5 
   6 int main() {
   7         int array[8]={4,7,1,2,6,5,8,3};
   8         vector<int> v;
   9         for (int i=0; i<8; i++)
  10         {
  11                 v.push_back(array[i]);
  12         }
  13         /**
  14          * min,max関数
  15          */
  16         cout << min(5,10) << endl;
  17         cout << max(5,10) << endl;
  18 
  19         /**
  20          * 区間の中で最大/最小の値のイテレータを返す
  21          * max_element(from, to)
  22          * min_element(from, to)
  23          * 区間の最初のイテレータ(ポインタ)と、最後(ソートする最後の要素の次)のイテレータを指定する。
  24          */
  25         cout << *(max_element(array, array + 8)) << endl;
  26         cout << *(min_element(v.begin(), v.end())) << endl;
  27 
  28         /**
  29          * 値を(破壊的に)ソートする
  30          * sort(from, to(, comp))
  31          * 区間の最初のイテレータと最後のイテレータ(と比較関数のポインタ)を指定する。(デフォルトは昇順)
  32          */
  33         sort(array, array + 8);
  34         cout << "sort array" << endl;
  35         for (int i=0; i<8; i++)
  36         {
  37                 cout << array[i] << " ";
  38         }
  39         cout << endl;
  40 
  41         cout << "sort vector" << endl;
  42         sort(v.begin(), v.end());       //vectorもok
  43         for (int i=0; i<v.size(); i++)
  44         {
  45                 cout << v[i] << " ";
  46         }
  47         cout << endl;
  48 
  49         cout << "sort array (DESC)" << endl;
  50         sort(array, array + 8, greater<int>()); //降順にする
  51         for (int i=0; i<8; i++)
  52         {
  53                 cout << array[i] << " ";
  54         }
  55         return 0;
  56 }


Category読み物