将字符串拆分为字符串向量的正确方法是什么?分隔符为空格或逗号。
一个方便的方法是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);
对于空格分隔的字符串,您可以这样做:
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
您可以将 getline 与分隔符一起使用:
string s, tmp;
stringstream ss(s);
vector<string> words;
while(getline(ss, tmp, ',')){
words.push_back(tmp);
.....
}
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"]
如果字符串同时包含空格和逗号,则可以使用字符串类函数
found_index = myString.find_first_of(delims_str, begin_index)
在一个循环中。检查 != npos 并插入到向量中。如果你更喜欢老派,你也可以使用C的
strtok()
方法。
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?"}
编辑:这是我做的一件事,这可以使用多字符分隔符,尽管我不是 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();
}
}
来自 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;
}
的变体,它的工作方式有点像 PHP 中的爆炸函数,我们提供给定的字符串和分隔符列表。
std::vector< std::string > explode(const std::string& data, const std::string& delimiters) {
auto is_delim = [&](auto & c) { return delimiters.find(c) != std::string::npos; };
std::vector< std::string > result;
for (std::string::size_type i(0), len(data.length()), pos(0); i <= len; i++) {
if (is_delim(data[i]) || i == len) {
auto tok = data.substr(pos, i - pos);
if ( !tok.empty() )
result.push_back( tok );
pos = i + 1;
}
} return result;
}
使用示例
std::string test_delimiters("hello, there is lots of, delimiters, that may be even together, ");
auto dem_res = explode(test_delimiters, " ,"); // space or comma
for (auto word : dem_res) {
std::cout << word << 'n';
} std::cout << "endn";
输出:
hello
there
is
lots
of
delimiters
that
may
be
even
together
end
我做了这个自定义函数,它将线转换为矢量
#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;
}
这是 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}
这是一个函数,它将string
拆分为vector
但它在输出vector
中不包含空字符串。
vector<string> split(string str, string token) {
vector<string> result;
while (str.size()) {
int index = str.find(token);
string substr;
if ((substr = str.substr(0, index)) == "") {
str = str.substr(index + token.size());
} else if (index != string::npos) {
result.push_back(substr);
str = str.substr(index + token.size());
} else {
result.push_back(str);
str = "";
}
}
return result;
}
注:以上内容改编自这个答案。
用法
void test() {
string a = "hello : world : ok : fine";
auto r = split(a, " : ", 2);
for (auto e: r) {
cout << e << endl;
}
}
static inline std::vector<std::string> split(const std::string &str, const std::string &delimiter = " ", const int max_elements = 0) {
std::vector<std::string> tokens;
std::string::size_type start_index = 0;
while (true) {
std::string::size_type next_index = str.find(delimiter, start_index);
if (next_index == std::string::npos) {
tokens.push_back(str.substr(start_index));
break;
} else {
tokens.push_back(str.substr(start_index, next_index - start_index));
start_index = next_index + delimiter.length();
}
if (max_elements > 0 && tokens.size() == max_elements - 1) {
tokens.push_back(str.substr(start_index));
break;
}
}
return tokens;
}