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


74

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

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


逗号和空格都是定界符的分割,还是根据参数在空格或逗号上分割的函数?
史蒂夫·杰索普

stackoverflow.com/questions/236129/how-to-split-a-string的一些答案可以很容易地调整为与多个定界符一起使用。
Gareth McCaughan

Answers:


93

对于以空格分隔的字符串,则可以执行以下操作:

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"));

输出:

What
is
the
right
way
to
split
a
string
into
a
vector
of
strings

同时具有逗号和空格的字符串

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"));

输出:

right
way
wrong
way
correct
way

std::vector<std::string> vstrings(begin, end);IMO会更好,但是我想我们不知道发问者是否正在构建矢量,还是希望填充先前存在的矢量。
史蒂夫·杰索普

不错,但是错了。OP的特殊之处在于空格和逗号都是分号。在这种情况下,您不能做同样的事情,对吗?
Armen Tsirunyan

1
这是一个了不起的答案,需要以某种方式突出显示。
Samveen

2
+1非常好。但是,您是否不必删除在ss.imbue(std::locale(std::locale(), new tokens()))某处创建的令牌结构?
kafman 2015年

1
@是的,我认为阅读可以做到这一点。使用auto loc = std::make_shared<tokens>(),然后通过ss.imbue(..., loc.get()));。那应该工作。
纳瓦兹

129

一种方便的方法是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);

13
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;
}

split(“ 1,2,3”,“,”)==> [“ 1”,“ 2”,“ 3”]

split(“ 1,2,”,“,”)==> [“ 1”,“ 2”,“”]

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


12

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

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

while(getline(ss, tmp, ',')){
    words.push_back(tmp);
    .....
}

9

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

found_index = myString.find_first_of(delims_str, begin_index) 

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

strtok() 

方法。


2

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;
}

0

我做了这个自定义函数,它将线转换为矢量

#include <iostream>
#include <vector>
#include <ctime>
#include <string>

using namespace std;

int main(){

    string line;
    getline(cin, line);
    int len = line.length();
    vector<string> subArray;

    for (int j = 0, k = 0; j < len; j++) {
        if (line[j] == ' ') {
            string ch = line.substr(k, j - k);
            k = j+1;
            subArray.push_back(ch);
        }
        if (j == len - 1) {
            string ch = line.substr(k, j - k+1);
            subArray.push_back(ch);
        }
    }

    return 0;
}

0
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;
}

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

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


-1

我编写此自定义函数将为您提供帮助。但是讨论一下时间复杂度。

std::vector<std::string> words;
std::string s;
std::string separator = ",";

while(s.find(separator) != std::string::npos){
   separatorIndex = s.find(separator)
   vtags.push_back(s.substr(0, separatorIndex ));
   words= s.substr(separatorIndex + 1, s.length());
}

words.push_back(s);

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.