C++のstd::stringの使い方

C++のstd::stringの使い方

std::stringとは

std::stringはC++標準ライブラリで提供される文字列型で、C言語のchar配列よりも柔軟に使えます。可変長であり、メモリ管理を自動的に行ってくれるため、安全かつ便利です。

基本的な使い方

文字列の宣言と初期化

std::stringの基本的な使い方として、文字列の宣言方法をいくつか紹介します。


#include <iostream>
#include <string>

int main() {
    std::string str1 = "Hello";
    std::string str2("World");
    std::string str3(5, 'A');  // "AAAAA"
    
    std::cout << str1 << " " << str2 << std::endl;
    std::cout << str3 << std::endl;
    return 0;
}

標準入力からの文字列の取得

std::cinを使ってユーザーから文字列を入力できます。


#include <iostream>
#include <string>

int main() {
    std::string name;
    std::cout << "名前を入力してください: ";
    std::cin >> name;
    std::cout << "こんにちは, " << name << "!" << std::endl;
    return 0;
}

ただし、std::cinは空白で区切られた最初の単語しか取得しません。全文字列を取得するにはstd::getlineを使います。


#include <iostream>
#include <string>

int main() {
    std::string fullName;
    std::cout << "フルネームを入力してください: ";
    std::getline(std::cin, fullName);
    std::cout << "ようこそ, " << fullName << "!" << std::endl;
    return 0;
}

文字列の操作

文字列の結合

+演算子やappend関数を使って文字列を結合できます。


std::string s1 = "Hello";
std::string s2 = " World";
std::string result = s1 + s2;  // "Hello World"
s1.append("!!!");  // "Hello!!!"

部分文字列の取得

substr関数を使うと、部分文字列を取得できます。


std::string str = "abcdef";
std::string sub = str.substr(2, 3);  // "cde"

文字のアクセス

文字列の特定の文字にアクセスするには、[]またはat()を使います。


std::string str = "Hello";
char c1 = str[1];  // 'e'
char c2 = str.at(2);  // 'l'

findを使った検索

find関数を使って文字列を検索できます。


std::string str = "abcdefg";
size_t pos = str.find("cd");
if (pos != std::string::npos) {
    std::cout << "見つかりました: " << pos << std::endl;
}

rfindで後方検索

最後に出現する位置を検索するにはrfindを使います。


std::string str = "abc def abc";
size_t pos = str.rfind("abc");  // 最後の "abc" の位置

文字列の比較

std::stringの比較は==やcompare()関数を使います。


std::string s1 = "apple";
std::string s2 = "banana";

if (s1 == s2) {
    std::cout << "同じ文字列です";
} else {
    std::cout << "違う文字列です";
}

文字列と数値の変換

数値を文字列に変換


int num = 42;
std::string str = std::to_string(num);  // "42"

文字列を数値に変換


std::string str = "123";
int num = std::stoi(str);  // 123
double d = std::stod(str);  // 123.0

応用的な使い方

文字列の分割

std::stringには直接分割機能はないため、std::stringstreamを使います。


#include <sstream>
#include <vector>
#include <iostream>

std::vector<std::string> split(const std::string& str, char delimiter) {
    std::vector<std::string> tokens;
    std::stringstream ss(str);
    std::string token;
    while (std::getline(ss, token, delimiter)) {
        tokens.push_back(token);
    }
    return tokens;
}

int main() {
    std::string text = "apple,banana,grape";
    std::vector<std::string> result = split(text, ',');
    for (const auto& word : result) {
        std::cout << word << std::endl;
    }
    return 0;
}

文字列の置換

std::stringには直接置換機能がないため、findとreplaceを組み合わせます。


std::string str = "Hello world";
size_t pos = str.find("world");
if (pos != std::string::npos) {
    str.replace(pos, 5, "C++");
}
std::cout << str << std::endl;  // "Hello C++"

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です