问题描述
我有一系列存储在单个数组中的字符串,用空格分隔(例如['f','o','o','\0','b','a','r ','\0'...]),我需要将它拆分成 std :: vector< std :: string>
>
我可以写一个10行循环,使用 std :: find
或 strlen
(实际上我只是做了),但我想知道是否有一个更简单/更优雅的方式来做,例如一些STL算法我忽略了,可以哄骗做这个。
这是一个相当简单的任务,它不会让我惊讶,如果有一些聪明的STL欺骗,可以应用,使它更简单。
任何takers?
我的两分钱:
const char * p = str;
std :: vector< std :: string>向量;
do {
vector.push_back(std :: string(p));
p + = vector.back()。size()+ 1;
} while(//适用任何条件);
I have a series of strings stored in a single array, separated by nulls (for example ['f', 'o', 'o', '\0', 'b', 'a', 'r', '\0'...]), and I need to split this into a std::vector<std::string>
or similar.
I could just write a 10-line loop to do this using std::find
or strlen
(in fact I just did), but I'm wondering if there is a simpler/more elegant way to do it, for example some STL algorithm I've overlooked, which can be coaxed into doing this.
It is a fairly simple task, and it wouldn't surprise me if there's some clever STL trickery that can be applied to make it even simpler.
Any takers?
My two cents :
const char* p = str;
std::vector<std::string> vector;
do {
vector.push_back(std::string(p));
p += vector.back().size() + 1;
} while ( // whatever condition applies );
这篇关于在C ++中拆分空值字符串序列的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!