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

时间:2011-04-09 20:13:37

标签: c++ string

  

可能重复:
  How to split a string?

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

10 个答案:

答案 0 :(得分:113)

方便的方式是boost's string algorithms library

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

答案 1 :(得分:76)

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

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

答案 2 :(得分:9)

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

found_index = myString.find_first_of(delims_str, begin_index) 

循环。检查!= npos并插入向量。如果你喜欢老学校,你也可以使用C的

strtok() 

方法

答案 3 :(得分:6)

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”,“,”)==&gt; [ “1”, “2”, “3”]

     

split(“1,2,”,“,”)==&gt; [ “1”, “2”, “”]

     

split(“1token2token3”,“token”)==&gt; [ “1”, “2”, “3”]

答案 4 :(得分:2)

来自Techie Delight的调整后的版本:

<kendo-grid
  [data]="gridData"
  [selectable]="false"
  [kendoGridSelectBy]="'Id'"
  [selectedKeys]="selectedIds"
  [pageable]="{ pageSizes: gridPageSizes }"
  [pageSize]="state.take"
  [skip]="state.skip"
  [scrollable]="'none'"
  [groupable]="false"
  [group]="state.group"
  (dataStateChange)="dataStateChange($event)"
  [filterable]="false"
  [filter]="state?.filter"
  [sortable]="true"
  [sort]="state?.sort"
  [rowClass]="rowCallBack"
>
<kendo-grid-column
    field="invoiceNumber"
    title="Invoice Number"
  >
  </kendo-grid-column>
 <kendo-grid-column
    title="PO"
  >
    <ng-template kendoGridCellTemplate let-dataItem="dataItem">
      {{dataItem.PO}}
    </ng-template>
  </kendo-grid-column>

 <kendo-grid-column
    field="StatusChangedDate"
    title="StatusChangedDate"
    filter="date"
    format="{0:d}"
  >
    <ng-template kendoGridCellTemplate let-dataItem>
      <time>{{ dataItem.StatusChangedDate | l10nDate: language:'short' }}</time>
    </ng-template>
  </kendo-grid-column>
</kendo-grid>

答案 5 :(得分:1)

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

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

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

答案 6 :(得分: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;
}

答案 7 :(得分: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?"}

答案 8 :(得分:0)

这里是 roach 解决方案的修改版本,它基于一串单字符分隔符进行拆分 + 支持压缩重复分隔符的选项。

std::vector<std::string> split(std::string text, std::string delim, bool compress) 
{
    std::vector<std::string> vec;
    size_t pos = 0, prevPos = 0;
    while (1) 
    {
        pos = text.find_first_of(delim, prevPos);

        while(compress) 
        {
            if( prevPos == pos )
                prevPos++;
            else
                break;

            pos = text.find_first_of(delim, prevPos);
        }

        if (pos == std::string::npos) {
            if(prevPos != text.size())
                vec.push_back(text.substr(prevPos));
            return vec;
        }

        vec.push_back(text.substr(prevPos, pos - prevPos));
        prevPos = pos + 1;
    }
}

无压缩示例:

std::string s = "  1.2  foo@foo . ";
auto res = split(s, ".@ ", false);
    for(auto i : res)
        std::cout << "string {" << i << "}" << std::endl;

输出:

string {}
string {}
string {1}
string {2}
string {}
string {foo}
string {foo}
string {}
string {}

使用压缩 split(s, ".@ ", true);

string {1}
string {2}
string {foo}
string {foo}

答案 9 :(得分:-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);