6と7のリビジョン間の差分
2011-01-28 04:37:18時点のリビジョン6
サイズ: 1200
編集者: y3eadgbe
コメント:
2011-01-28 05:19:57時点のリビジョン7
サイズ: 2622
編集者: y3eadgbe
コメント:
削除された箇所はこのように表示されます。 追加された箇所はこのように表示されます。
行 7: 行 7:

'''目次'''
<<TableOfContents(2)>>
行 63: 行 66:
== string ==
 * 主な用途
  * 可変長の文字列として
 * 使い方

{{{#!highlight cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
 /*
  * stringの宣言
  */
 string str; //空文字列
 string str2("Hello World!"); //初期値あり

 /*
  * stringの演算子
  */
 str = "I'm hungry"; //代入
 cout << str << endl;

 char ch = str[2]; //要素(char)へのアクセス(str.at(2)でも可)
 putchar(ch);
 cout << endl;

 str = str + " and thirsty"; //結合(str += " and thirsty"; でも可)
 cout << str << endl;

 if (str == str2) //比較(完全一致)
 {
  cout << "str is equal to str2" << endl;
 } else {
  cout << "str is NOT equal to str2" << endl;
 }

 str = "abc";
 str2 = "bac";
 if (str < str2) //比較(辞書順)
 {
  cout << "str is less than str2" << endl;
 } else {
  cout << "str is NO less than str2" << endl;
 }

 /*
  * stringの文字数
  * str.size()
  */
 cout << "str contains " << str.size() << " characters" << endl; //str.length()も使える

 /*
  * その他、文字列の出現位置の検索、トリムなどの機能もある(詳細は割愛)
  */
 str = "abcdefg";
 str2 = "cde";
 cout << "found str2 in str (from index of " << str.find(str2) << ")" << endl;

 cout << str.substr(3,3) << endl;
 
 return 0;
}
}}}

【C++】STL for TopCoder

TopCoderを始めて、最初にSTLの使い方でつまづいたので、最低限必要なものを備忘録を兼ねて簡単に解説します。 初心者向けです。

目次

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 }


Category読み物

y3eadgbe/【C++】STL for TopCoder (最終更新日時 2014-12-07 04:47:10 更新者 y3eadgbe)