[C++] splitないので自前スペース区切りする (std::string)

1 min read
hiroweb developer

Java とかだとナチュラルにsplitで区切ってたんだけれど、C++にはそんなの無いみたい。つらい。

方法

コード

strには"MADE IN JAPAN"みたいな感じでスペースが入った文字列が入ってる。

vector<string> list;
stringstream ss(str);
string item;
while (getline(ss, item, ' ') && !item.empty()) {
    list.push_back(item);
}

これもincludeしておく。

#include <vector>
#include <string>
#include <sstream>

これを char で取り出すのは、こんな感じ。

list[0].c_str();