C++ で文字列をスペースで分割
- 
          
            C++ で std::string::findおよびstd::string::substr関数を使用して文字列をスペースで分割する
- 
          
            C++ で std::istringstreamとstd::copyを使用して文字列をスペースで分割する
- 
          
            C++ で std::getlineとerase-removeIdiom を使用して文字列をスペースで分割
 
この記事では、C++ でスペース区切り文字で文字列を分割する方法に関する複数の方法を示します。
C++ で std::string::find および std::string::substr 関数を使用して文字列をスペースで分割する
    
find と substr は std::string の組み込み関数であり、文字列値または単一文字で指定された任意の区切り文字で文字列を分割するために使用できます。find 関数は string 引数を取り、指定された部分文字列が始まる位置を返します。それ以外の場合、見つからない場合は、string::npos が返されます。したがって、find 関数が npos を返すまで、while ループを繰り返します。一方、substr メソッドを使用して、区切り文字の前の文字列の部分にアクセスできます。この場合は単一のスペース文字であり、後で使用するために vector に格納されます。その後、erase 関数を呼び出して、区切り文字を含む最初のシーケンスを削除します。その時点で、新しい反復が続行され、操作が繰り返される場合があります。
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::istringstream;
using std::string;
using std::stringstream;
using std::vector;
int main() {
  string text =
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
      "Sed laoreet sem leo, in posuere orci elementum.";
  string space_delimiter = " ";
  vector<string> words{};
  size_t pos = 0;
  while ((pos = text.find(space_delimiter)) != string::npos) {
    words.push_back(text.substr(0, pos));
    text.erase(0, pos + space_delimiter.length());
  }
  for (const auto &str : words) {
    cout << str << endl;
  }
  return EXIT_SUCCESS;
}
出力:
Lorem
ipsum
dolor
sit
amet,
consectetur
adipiscing
elit.
Sed
laoreet
sem
leo,
in
posuere
orci
C++ で std::istringstream と std::copy を使用して文字列をスペースで分割する
または、string ベースのストリームの入出力操作を提供する istringstream クラスを使用してコードを再実装することもできます。分割する必要のある string 値で istringstream オブジェクトを初期化したら、std::copy アルゴリズムを呼び出して、スペースで区切られた各文字列値を cout ストリームに出力できます。このメソッドは、スペース区切り文字の分割のみをサポートしていることに注意してください。これは、istringstream クラスの実装が提供するものだからです。
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::istringstream;
using std::string;
using std::stringstream;
using std::vector;
int main() {
  string text =
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
      "Sed laoreet sem leo, in posuere orci elementum.";
  vector<string> words{};
  istringstream iss(text);
  copy(std::istream_iterator<string>(iss), std::istream_iterator<string>(),
       std::ostream_iterator<string>(cout, "\n"));
  return EXIT_SUCCESS;
}
C++ で std::getline と erase-remove Idiom を使用して文字列をスペースで分割
前のソリューションの 1つの欠点は、解析された単語とともに格納される句読記号です。これは、基本的に特定の範囲の条件付き削除操作である消去削除イディオムで解決できます。この場合、std::getline によって取得された各単語でこのメソッドを呼び出して、その中のすべての句読記号をトリミングします。ispunct 関数オブジェクトは、句読文字をチェックするために remove_if アルゴリズムの 3 番目の引数として渡されることに注意してください。
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::istringstream;
using std::string;
using std::stringstream;
using std::vector;
int main() {
  string text =
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
      "Sed laoreet sem leo, in posuere orci elementum.";
  char space_char = ' ';
  vector<string> words{};
  stringstream sstream(text);
  string word;
  while (std::getline(sstream, word, space_char)) {
    word.erase(std::remove_if(word.begin(), word.end(), ispunct), word.end());
    words.push_back(word);
  }
  for (const auto &str : words) {
    cout << str << endl;
  }
  return EXIT_SUCCESS;
}
出力:
Lorem
ipsum
dolor
sit
amet
consectetur
adipiscing
elit
Sed
laoreet
sem
leo
in
posuere
orci
elementum
