我正在尝试编写一个函数,它将根据给定的字符拆分字符串并返回结果字符串的向量,但我在for循环的行中收到编译错误。有什么想法吗?我应该能够将astring [0]分配给char指针正确吗?
/*
splits string by a given character and returns a vector of each segment
if string = "ab cd ef" and split_char = " " it will return a vector with
"ab" in first location "cd" in second location and "ef" in third location
*/
vector<string> split_string(string string_to_split, const char split_char)
{
//deletes leading split characters
int num_leading_split_char = 0;
for (char * c = string_to_split[0]; c* == split_char; c++)
{
num_leading_split_char++;
}
string_to_split.erase(0, num_leading_split_char);
//makes the split string vector
vector<string> split_string;
string temp_string = "";
for (char * c = string_to_split[0]; c*; c++)
{
if (*c == split_char)
{
split_string.push_back(temp_string); //add string to vector
temp_string = ""; //reset temp string
}
else
{
temp_string += *c; //adds char to temp string
}
}
return split_string;
}
错误消息:
pgm5.cpp:在函数'std :: vector&gt;中 split_string(std :: __ cxx11 :: string,char)':
pgm5.cpp:257:34:错误:转换无效 '__gnu_cxx :: __ alloc_traits&gt; :: value_type {aka char}'to'char *'[-fpermissive] for(char c = string_to_split [0]; c == split_char; C ++) ^
pgm5.cpp:257:40:错误:在'=='标记之前预期的primary-expression for(char c = string_to_split [0]; c == split_char; c ++) ^〜
pgm5.cpp:269:34:错误:转换无效 '__gnu_cxx :: __ alloc_traits&gt; :: value_type {aka char}'to'char *'[-fpermissive] for(char c = string_to_split [0]; ç的; C ++) ^
pgm5.cpp:269:39:错误:在';'标记之前预期的主要表达式 for(char c = string_to_split [0]; c ; c ++) ^
编译失败。
答案 0 :(得分:0)
我应该可以将
std::string str[0]
分配给char*
指针正确吗?
没有。 str[0]
是char
字面值而不是char*
指针。你的编译器给你这个确切的错误。既然你已经在使用std::string
,为什么不只是简单地利用它为你提供的一些不错的操作,比如substr
和find
,所以你不需要重新发明轮子或者它可能是在你的情况下轮胎(开玩笑)。此外,最好将未修改的非POD类型作为const
引用传递,以避免不必要的副本,即const std::string &
。我知道在你的代码中有一个擦除操作,但在这个例子中,没有必要修改传入的字符串。
std::vector<std::string> split_string(const std::string &string_to_split, char delim)
{
std::vector<std::string> results;
size_t match = 0;
size_t found = 0;
// match each part of string_to_split on delim and tokenize into results
// if delim char is never found then return empty vector
while ((found = string_to_split.find(delim, match)) != std::string::npos)
{
results.push_back(string_to_split.substr(match, found - match));
match = found + 1; // start again at next character
}
// after the loop, if any match was found store the last token
if (match != 0)
{
results.push_back(string_to_split.substr(match));
}
return results;
}
如果你在空格上进行标记,你可以像这样使用它。
std::string test("This is a test.");
std::vector<std::string> tokenized = split_string(test, ' ');
for (const auto& s : tokenized)
{
std::cout << "s=" << s << std::endl;
}
将产生以下结果。
s=This s=is s=a s=test.
答案 1 :(得分:0)
试试这个你可以在http://cpp.sh/8r6ze尝试:
#include <sstream> // std::istringstream
#include <string>
#include <vector>
#include <iostream>
std::vector<std::string> split_string(const std::string string_to_split,
const char delimiter)
{
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(string_to_split);
while (std::getline(tokenStream, token, delimiter))
{
tokens.push_back(token);
}
return tokens;
}
int main ()
{
const std::string theString{"thy with this"};
for (const auto &s:split_string(theString,' ')){
std::cout << s <<std::endl;
}
return 0;
}