C++における文字列操作
目次
C++を使っていて、文字列の処理についてのTipsです。 追記及び修正等を気付き次第行なっていきます。
文字列の操作
文字(列)の挿入・削除
文字列の挿入: std::vector::insert
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string str = "Farma_11"; str.insert(2, "***"); cout << str << endl; // Fa***rma_11 }
参考: vector::insert - cpprefjp C++日本語リファレンス
文字列の末尾に挿入: std::basic_string::append
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string str = "Farma_11"; str.append(3, '!'); cout << str << endl; // Farma_11!!! }
参考: basic_string::append - cpprefjp C++日本語リファレンス
文字列内の一部の削除: std::basic_string::erase
#include <iostream> #include <string> using namespace std; int main() { string str = "Farma_11"; str.erase(str.end()-1); //end()は最後の次を指す cout << "末尾削除: " << str << endl; // 末尾削除: Farma_1 str.erase(str.begin()+3); //begin()は先頭を指す cout << "4文字目削除: " << str << endl; // 4文字目削除: Fara_1 }
参考: basic_string::erase - cpprefjp C++日本語リファレンス
文字(列)の検索・置換
特定の文字(列)の検索: std::find
など
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string str = "Farma_11"; cout << "最初の ma: " << str.find("ma")+1 << "文字目から" << endl; // 最初の ma: 4文字目から cout << "最後の a: " << str.find_last_of('a')+1 << "文字目" << endl; // 最後の a: 5文字目 cout << "存在しない: " << str.find("e")+1 << endl; //string::npos // 存在しない: 0 }
std::find
: find - cpprefjp C++日本語リファレンスstd::basic_string::find_last_of
:
basic_string::find_last_of - cpprefjp C++日本語リファレンス
アスキーコードの変更
#include <iostream> #include <string> using namespace std; int main() { string str = "Farma_11"; char plus = 5; // C++ではアスキーコードがchar型 str[1] += plus; cout << str << endl; // Ffrma_11 }
文字(列)のカウント
文字のカウント
#include <iostream> #include <string> using namespace std; int main() { string str = "Farma_11"; int cnt = 0; for(unsigned int i = 0; i < str.size(); i++){ if(str[i] == 'a') cnt++; } cout << "カウント: a " << cnt << "個" << endl; // カウント: a 2個 }
文字列のカウント
#include <iostream> #include <string> using namespace std; int main() { string str = "Farma_11"; int cnt = 0; string s = "ma"; for(unsigned int i = 0; i < str.size() - s.size() + 1; i++){ if(str.substr(i, s.size()) == s) cnt++; //添え字iからs.size()分 } cout << "カウント: " << s << " " << cnt << "個" << endl; // カウント: ma 1個 }
文字列内の文字の並びを変更する
文字列内の文字の交換: std::swap
#include <iostream> #include <string> using namespace std; int main() { string str = "Farma_11"; swap(str[0], str[str.size()-1]); // size()は文字数 cout << "先頭と末尾の交換: " << str << endl; // 先頭と末尾の交換: 1arma_1F }
参考: swap (非メンバ関数) - cpprefjp C++日本語リファレンス
文字列の反転: std::reverse
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string str = "Farma_11"; reverse(str.begin(), str.end()); // イテレータを用いて範囲指定 cout << str << endl; // 出力: 11_amraF }
参考: reverse - cpprefjp C++日本語リファレンス
文字列内の文字のソート: std::sort
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string str = "Farma_11"; sort(str.begin(), str.end()); cout << "昇順: " << str << endl; // 昇順: 11F_aamr sort(str.begin(), str.end(), greater<char>()); cout << "降順: " << str << endl; // 降順: rmaa_F11 }
参考: sort - cpprefjp C++日本語リファレンス
文字列と数値の変換
文字列から整数へ変換
#include <iostream> #include <string> using namespace std; int main() { string num = "12345"; int n = atoi((const char*)num.c_str()); num += "-5"; n += -5; //共に-5を足す cout << "整数へ変換: " << num << " = " << n << endl; }
文字列から1桁ずつ整数へ変換
一桁の数値への変換では、'[0-9]の文字' - '0'
にて数値へ変換可能。
#include <iostream> #include <string> #include <cstring> using namespace std; int main() { string num = "12345"; char number[10]; int sum = 0; strcpy(number, num.c_str()); for(unsigned int i = 0; i < num.size(); i++){ sum += number[i] - '0'; } cout << "1〜5の和: " << sum << endl; }