将std :: string拆分为vector <string>的正确方法

dev*_*ull 61 c++ string

可能重复:
如何拆分字符串?

将字符串拆分为字符串向量的正确方法是什么.分隔符是空格或逗号.

Unc*_*ens 117

一种方便的方法是boost的字符串算法库.

#include <boost/algorithm/string/classification.hpp> // Include boost::for is_any_of
#include <boost/algorithm/string/split.hpp> // Include for boost::split
// ...

std::vector<std::string> words;
std::string s;
boost::split(words, s, boost::is_any_of(", "), boost::token_compress_on);
Run Code Online (Sandbox Code Playgroud)


Naw*_*waz 83

对于空格分隔的字符串,您可以这样做:

std::string s = "What is the right way to split a string into a vector of strings";
std::stringstream ss(s);
std::istream_iterator<std::string> begin(ss);
std::istream_iterator<std::string> end;
std::vector<std::string> vstrings(begin, end);
std::copy(vstrings.begin(), vstrings.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
Run Code Online (Sandbox Code Playgroud)

输出:

What
is
the
right
way
to
split
a
string
into
a
vector
of
strings
Run Code Online (Sandbox Code Playgroud)

在线演示:http://ideone.com/d8E2G


具有逗号和空格的字符串

struct tokens: std::ctype<char> 
{
    tokens(): std::ctype<char>(get_table()) {}

    static std::ctype_base::mask const* get_table()
    {
        typedef std::ctype<char> cctype;
        static const cctype::mask *const_rc= cctype::classic_table();

        static cctype::mask rc[cctype::table_size];
        std::memcpy(rc, const_rc, cctype::table_size * sizeof(cctype::mask));

        rc[','] = std::ctype_base::space; 
        rc[' '] = std::ctype_base::space; 
        return &rc[0];
    }
};

std::string s = "right way, wrong way, correct way";
std::stringstream ss(s);
ss.imbue(std::locale(std::locale(), new tokens()));
std::istream_iterator<std::string> begin(ss);
std::istream_iterator<std::string> end;
std::vector<std::string> vstrings(begin, end);
std::copy(vstrings.begin(), vstrings.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
Run Code Online (Sandbox Code Playgroud)

输出:

right
way
wrong
way
correct
way
Run Code Online (Sandbox Code Playgroud)

在线演示:http://ideone.com/aKL0m

  • +1 非常好。但是您不必在某处删除在 `ss.imbue(std::locale(std::locale(), new tokens()))` 中创建的令牌结构吗? (2认同)

Tod*_*Tod 9

如果字符串同时包含空格和逗号,则可以使用字符串类函数

found_index = myString.find_first_of(delims_str, begin_index) 
Run Code Online (Sandbox Code Playgroud)

在一个循环中.检查!= npos并插入向量.如果你喜欢老学校,你也可以使用C.

strtok() 
Run Code Online (Sandbox Code Playgroud)

方法.


小智 8

vector<string> split(string str, string token){
    vector<string>result;
    while(str.size()){
        int index = str.find(token);
        if(index!=string::npos){
            result.push_back(str.substr(0,index));
            str = str.substr(index+token.size());
            if(str.size()==0)result.push_back(str);
        }else{
            result.push_back(str);
            str = "";
        }
    }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

split("1,2,3",",")==> ["1","2","3"]

split("1,2,",",")==> ["1","2",""]

split("1token2token3","token")==> ["1","2","3"]


wco*_*ran 6

Techie Delight的调整版本:

#include <string>
#include <vector>

std::vector<std::string> split(const std::string& str, char delim) {
    std::vector<std::string> strings;
    size_t start;
    size_t end = 0;
    while ((start = str.find_first_not_of(delim, end)) != std::string::npos) {
        end = str.find(delim, start);
        strings.push_back(str.substr(start, end - start));
    }
    return strings;
}
Run Code Online (Sandbox Code Playgroud)


roa*_*ach 6

std::vector<std::string> split(std::string text, char delim) {
    std::string line;
    std::vector<std::string> vec;
    std::stringstream ss(text);
    while(std::getline(ss, line, delim)) {
        vec.push_back(line);
    }
    return vec;
}
Run Code Online (Sandbox Code Playgroud)

split("String will be split", ' ') -> {"String", "will", "be", "split"}

split("Hello, how are you?", ',') -> {"Hello", "how are you?"}

编辑:这是我做的一件事,它可以使用多字符分隔符,尽管我不是 100% 确定它是否总是有效:

std::vector<std::string> split(std::string text, std::string delim) {
    std::vector<std::string> vec;
    size_t pos = 0, prevPos = 0;
    while (1) {
        pos = text.find(delim, prevPos);
        if (pos == std::string::npos) {
            vec.push_back(text.substr(prevPos));
            return vec;
        }

        vec.push_back(text.substr(prevPos, pos - prevPos));
        prevPos = pos + delim.length();
    }
}
Run Code Online (Sandbox Code Playgroud)


Jam*_*mes 5

您可以将getline与分隔符一起使用:

string s, tmp; 
stringstream ss(s);
vector<string> words;

while(getline(ss, tmp, ',')){
    words.push_back(tmp);
    .....
}
Run Code Online (Sandbox Code Playgroud)