C++標準ライブラリ <string>
C++標準ライブラリ(Standard Template Library, STL)はC++の主要な構成要素の一つであり、豊富なデータ構造とアルゴリズムを提供します。
<string>は、C++標準ライブラリで文字列を処理するためのヘッダーファイルです。
C++では、文字列は文字で構成されるシーケンスです。<string>ヘッダーファイルは、...を提供します。std::stringクラスは、Cスタイル文字列をカプセル化したもので、より安全で使いやすい文字列操作機能を提供します。
C++プログラムで使用するには<string>ライブラリを使用するには、まずこのヘッダーファイルをインクルードする必要があります:
#include <iostream> #include <string>
基本構文
std::stringクラスの基本構文は次のとおりです:
-
文字列変数の宣言:
std::string str;
-
文字列の初期化:
std::string str = "Hello, World!";
-
使用
+文字列の連結:std::string str1 = "Hello, "; std::string str2 = "World!"; std::string result = str1 + str2;
一般的なメンバ関数
std::stringクラスには文字列を操作するためのメンバ関数が多数用意されています。以下は一般的なメンバ関数です:
size():文字列の長さを返します。empty():文字列が空かどうかをチェックします。operator[]:インデックスを使用して文字列内の文字にアクセスします。substr():部分文字列を取得します。find():メイン文字列内の部分文字列の位置を検索します。replace():文字列内の一部の文字を置換します。
例
以下は、使用する<string>ライブラリの簡単な例で、出力結果を含みます:
例
#include <iostream>
#include <string>
int main() {
// 文字列の宣言と初期化
std::string greeting = "Hello, World!";
std::cout << "Greeting: " << greeting << std::endl;
// size() を使用して文字列の長さを取得
std::cout << "Length of the greeting: " << greeting.size() << std::endl;
// empty() を使用して文字列が空かどうかをチェック
std::cout << "Is the greeting empty? " << (greeting.empty() ? "Yes" : "No") << std::endl;
// operator[] を使用して特定の位置の文字にアクセス
std::cout << "Character at position 7: " << greeting[7] << std::endl;
// substr() を使用して部分文字列を取得
std::string sub = greeting.substr(7, 5);
std::cout << "Substring from position 7 with length 5: " << sub << std::endl;
// find() を使用して部分文字列を検索
std::cout << "Position of 'World' in the greeting: " << greeting.find("World") << std::endl;
// replace() を使用して文字列の一部を置換
// 'World' を 'C++' に置換
std::string modified = greeting;
std::string::size_type pos = modified.find("World");
if (pos != std::string::npos) {
modified.replace(pos, 5, "C++"); // 位置 pos から開始して、5文字を "C++" に置換
}
std::cout << "Modified greeting: " << modified << std::endl;
return 0;
}
#include <string>
int main() {
// 文字列の宣言と初期化
std::string greeting = "Hello, World!";
std::cout << "Greeting: " << greeting << std::endl;
// size() を使用して文字列の長さを取得
std::cout << "Length of the greeting: " << greeting.size() << std::endl;
// empty() を使用して文字列が空かどうかをチェック
std::cout << "Is the greeting empty? " << (greeting.empty() ? "Yes" : "No") << std::endl;
// operator[] を使用して特定の位置の文字にアクセス
std::cout << "Character at position 7: " << greeting[7] << std::endl;
// substr() を使用して部分文字列を取得
std::string sub = greeting.substr(7, 5);
std::cout << "Substring from position 7 with length 5: " << sub << std::endl;
// find() を使用して部分文字列を検索
std::cout << "Position of 'World' in the greeting: " << greeting.find("World") << std::endl;
// replace() を使用して文字列の一部を置換
// 'World' を 'C++' に置換
std::string modified = greeting;
std::string::size_type pos = modified.find("World");
if (pos != std::string::npos) {
modified.replace(pos, 5, "C++"); // 位置 pos から開始して、5文字を "C++" に置換
}
std::cout << "Modified greeting: " << modified << std::endl;
return 0;
}
出力結果:
Length of the greeting: 13 Is the greeting empty? No Character at position 7: W Substring from position 7 with length 5: World Position of 'World' in the greeting: 7 Modified greeting: Hello, C++!
std::string メンバ関数一覧表
以下は、一般的な std::string メンバ関数の一覧です:| 関数名 | 説明 | サンプルコード |
|---|---|---|
size() | 文字列の長さ(文字数)を返します。 | std::cout << str.size(); |
length() | とsize()同じく、文字列の長さを返します。 | std::cout << str.length(); |
empty() | 文字列が空かどうかを判定します。 | std::cout << (str.empty() ? "Yes" : "No"); |
operator[] | 文字列内の指定位置の文字にアクセスします。 | std::cout << str[0]; |
at() | 文字列内の指定位置の文字にアクセスします(境界チェック付き)。 | std::cout << str.at(0); |
substr() | 指定位置から始まる部分文字列を返します。 | std::string sub = str.substr(0, 5); |
find() | 文字列内の部分文字列の位置を検索します。 | std::cout << str.find("sub") << std::endl; |
rfind() | 文字列の末尾から部分文字列の位置を検索します。 | std::cout << str.rfind("sub") << std::endl; |
replace() | 文字列内の一部の内容を置換します。 | str.replace(pos, length, "new_substring"); |
append() | 文字列の末尾に内容を追加します。 | str.append(" more"); |
insert() | 指定位置に内容を挿入します。 | str.insert(pos, "inserted"); |
erase() | 指定位置の文字または部分文字列を削除します。 | str.erase(pos, length); |
clear() | 文字列をクリアします。 | str.clear(); |
c_str() | Cスタイルの文字列(null終端)を返します。 | const char* cstr = str.c_str(); |
data() | 文字データへのポインタを返します(C++11以降のバージョン)。 | const char* data = str.data(); |
compare() | 2つの文字列を比較します。 | int result = str.compare("other"); |
find_first_of() | 任意の文字に一致する最初の位置を検索します。 | size_t pos = str.find_first_of("aeiou"); |
find_last_of() | 任意の文字に一致する最後の位置を検索します。 | size_t pos = str.find_last_of("aeiou"); |
find_first_not_of() | 任意の文字に一致しない最初の位置を検索します。 | size_t pos = str.find_first_not_of("aeiou"); |
find_last_not_of() | 任意の文字に一致しない最後の位置を検索します。 | size_t pos = str.find_last_not_of("aeiou"); |
例
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
// size()
std::cout << "Length: " << str.size() << std::endl;
// empty()
std::cout << "Is empty? " << (str.empty() ? "Yes" : "No") << std::endl;
// operator[]
std::cout << "First character: " << str[0] << std::endl;
// at()
std::cout << "Character at position 7: " << str.at(7) << std::endl;
// substr()
std::string sub = str.substr(7, 5);
std::cout << "Substring from position 7 with length 5: " << sub << std::endl;
// find()
size_t pos = str.find("World");
std::cout << "Position of 'World': " << pos << std::endl;
// replace()
str.replace(pos, 5, "C++");
std::cout << "Modified string: " << str << std::endl;
// append()
str.append(" How are you?");
std::cout << "Appended string: " << str << std::endl;
// insert()
str.insert(7, " Beautiful");
std::cout << "String after insert: " << str << std::endl;
// erase()
str.erase(7, 10);
std::cout << "String after erase: " << str << std::endl;
// clear()
str.clear();
std::cout << "String after clear: " << (str.empty() ? "Empty" : "Not empty") << std::endl;
// c_str()
str = "Hello, C++!";
const char* cstr = str.c_str();
std::cout << "C-style string: " << cstr << std::endl;
// compare()
int cmp = str.compare("Hello, C++!");
std::cout << "Comparison result: " << cmp << std::endl;
// find_first_of()
size_t pos_first_vowel = str.find_first_of("aeiou");
std::cout << "First vowel at position: " << pos_first_vowel << std::endl;
// find_last_of()
size_t pos_last_vowel = str.find_last_of("aeiou");
std::cout << "Last vowel at position: " << pos_last_vowel << std::endl;
// find_first_not_of()
size_t pos_first_non_vowel = str.find_first_not_of("aeiou");
std::cout << "First non-vowel at position: " << pos_first_non_vowel << std::endl;
// find_last_not_of()
size_t pos_last_non_vowel = str.find_last_not_of("aeiou");
std::cout << "Last non-vowel at position: " << pos_last_non_vowel << std::endl;
return 0;
}
#include <string>
int main() {
std::string str = "Hello, World!";
// size()
std::cout << "Length: " << str.size() << std::endl;
// empty()
std::cout << "Is empty? " << (str.empty() ? "Yes" : "No") << std::endl;
// operator[]
std::cout << "First character: " << str[0] << std::endl;
// at()
std::cout << "Character at position 7: " << str.at(7) << std::endl;
// substr()
std::string sub = str.substr(7, 5);
std::cout << "Substring from position 7 with length 5: " << sub << std::endl;
// find()
size_t pos = str.find("World");
std::cout << "Position of 'World': " << pos << std::endl;
// replace()
str.replace(pos, 5, "C++");
std::cout << "Modified string: " << str << std::endl;
// append()
str.append(" How are you?");
std::cout << "Appended string: " << str << std::endl;
// insert()
str.insert(7, " Beautiful");
std::cout << "String after insert: " << str << std::endl;
// erase()
str.erase(7, 10);
std::cout << "String after erase: " << str << std::endl;
// clear()
str.clear();
std::cout << "String after clear: " << (str.empty() ? "Empty" : "Not empty") << std::endl;
// c_str()
str = "Hello, C++!";
const char* cstr = str.c_str();
std::cout << "C-style string: " << cstr << std::endl;
// compare()
int cmp = str.compare("Hello, C++!");
std::cout << "Comparison result: " << cmp << std::endl;
// find_first_of()
size_t pos_first_vowel = str.find_first_of("aeiou");
std::cout << "First vowel at position: " << pos_first_vowel << std::endl;
// find_last_of()
size_t pos_last_vowel = str.find_last_of("aeiou");
std::cout << "Last vowel at position: " << pos_last_vowel << std::endl;
// find_first_not_of()
size_t pos_first_non_vowel = str.find_first_not_of("aeiou");
std::cout << "First non-vowel at position: " << pos_first_non_vowel << std::endl;
// find_last_not_of()
size_t pos_last_non_vowel = str.find_last_not_of("aeiou");
std::cout << "Last non-vowel at position: " << pos_last_non_vowel << std::endl;
return 0;
}
出力結果:
Length: 13 Is empty? No First character: H Character at position 7: W Substring from position 7 with length 5: World Position of 'World': 7 Modified string: Hello, C++! Appended string: Hello, C++! How are you? String after insert: Hello, BeautifulC++! How are you? String after erase: Hello, C++! How are you? String after clear: Empty C-style string: Hello, C++! Comparison result: 0 First vowel at position: 1 Last vowel at position: 4 First non-vowel at position: 0 Last non-vowel at position: 10その他の拡張