如何将空字符串存储到向量中 我是C++新手,我不知道用定界符分割一个字符串,把子串放进向量。< /P>
我的代码如下:如何将空字符串存储到向量中 我是C++新手,我不知道用定界符分割一个字符串,把子串放进向量。< /P>,c++,string,vector,C++,String,Vector,我的代码如下: vector<string> split(const string &s, const string &delim) { string::size_type pos = s.find_first_of(delim,0); int start = 0; vector<string> tokens; while(start < s.size()) { if(start
vector<string> split(const string &s, const string &delim)
{
string::size_type pos = s.find_first_of(delim,0);
int start = 0;
vector<string> tokens;
while(start < s.size())
{
if(start++ != pos + 1)
tokens.push_back(" ");
pos = s.find_first_of(delim, start);
tokens.push_back(s.substr(start, pos - start));
}
for(vector<string>::size_type i = 0; i != tokens.size(); ++i)
cout << tokens[i];
return tokens;
}
减去引号
但我的代码目前的输出是
ab b cd d
任何帮助都将不胜感激。您的循环似乎没有做正确的事情:您一个字符一个字符地行走,在每次迭代中一个字符地前进
开始。我怀疑您实际上想要一个当前位置,查找下一个分隔符,将当前位置和分隔符之间的字符串添加到结果中,并将当前位置设置为分隔符后面的字符:
for (std::string::size_type start(0); start != s.npos; )
{
std::string::size_type end(s.find_first_of(delim, start));
tokens.push_back(s.substr(start, end != s.npos? end - start: end));
start = end != s.npos? end + 1: s.npos;
}
你的循环似乎做得不对:你一个字符一个字符地走,在每次迭代中一个一个地前进start
。我怀疑您实际上想要一个当前位置,查找下一个分隔符,将当前位置和分隔符之间的字符串添加到结果中,并将当前位置设置为分隔符后面的字符:
for (std::string::size_type start(0); start != s.npos; )
{
std::string::size_type end(s.find_first_of(delim, start));
tokens.push_back(s.substr(start, end != s.npos? end - start: end));
start = end != s.npos? end + 1: s.npos;
}
这就是诀窍
#include <iostream>
#include <vector>
using namespace std;
vector<string> split(string record, string token) {
vector<string> results;
size_t startPos = 0;
size_t pos = 0;
// Step: If either argument is empty then return
// an empty vector.
if (record.length() == 0 || token.length() == 0) {
return results;
}
// Step: Go through the record and split up the data.
while(startPos < record.length()) {
pos = record.find(token, startPos);
if (pos == string::npos) {
break;
}
results.push_back(record.substr(startPos, pos - startPos));
startPos = pos + token.length();
}
// Step: Get the last (or only bit).
results.push_back(record.substr(startPos, record.length() - startPos));
// Step: Return the results of the split.
return results;
}
void printData(vector<string> list) {
for(vector<string>::iterator it = list.begin(); it < list.end(); it++) {
cout << *it << endl;
}
}
int main(int argc, char** argv) {
string record = "";
string delim = "";
if (argc == 3) {
record = argv[1];
delim = argv[2];
printData(split(record,delim));
} else {
string record = "comma,delimited,data";
string delim = ",";
printData(split(record,delim));
record = "One<--->Two<--->Three<--->Four";
delim = "<--->";
printData(split(record,delim));
}
}
#包括
#包括
使用名称空间std;
向量拆分(字符串记录、字符串标记){
矢量结果;
大小\u t startPos=0;
大小\u t pos=0;
//步骤:如果任一参数为空,则返回
//空向量。
if(record.length()==0 | | token.length()==0){
返回结果;
}
//步骤:检查记录并拆分数据。
while(startPos
#include <iostream>
#include <vector>
using namespace std;
vector<string> split(string record, string token) {
vector<string> results;
size_t startPos = 0;
size_t pos = 0;
// Step: If either argument is empty then return
// an empty vector.
if (record.length() == 0 || token.length() == 0) {
return results;
}
// Step: Go through the record and split up the data.
while(startPos < record.length()) {
pos = record.find(token, startPos);
if (pos == string::npos) {
break;
}
results.push_back(record.substr(startPos, pos - startPos));
startPos = pos + token.length();
}
// Step: Get the last (or only bit).
results.push_back(record.substr(startPos, record.length() - startPos));
// Step: Return the results of the split.
return results;
}
void printData(vector<string> list) {
for(vector<string>::iterator it = list.begin(); it < list.end(); it++) {
cout << *it << endl;
}
}
int main(int argc, char** argv) {
string record = "";
string delim = "";
if (argc == 3) {
record = argv[1];
delim = argv[2];
printData(split(record,delim));
} else {
string record = "comma,delimited,data";
string delim = ",";
printData(split(record,delim));
record = "One<--->Two<--->Three<--->Four";
delim = "<--->";
printData(split(record,delim));
}
}
#包括
#包括
使用名称空间std;
向量拆分(字符串记录、字符串标记){
矢量结果;
大小\u t startPos=0;
大小\u t pos=0;
//步骤:如果任一参数为空,则返回
//空向量。
if(record.length()==0 | | token.length()==0){
返回结果;
}
//步骤:检查记录并拆分数据。
while(startPos 这里有一些相关的问题可能会有所帮助:这里有一些相关的问题可能会有所帮助: