I am trying to write a function that will split a string based on a given character and return a vector of the resulting strings but I am getting a compilation error at the line of my for loop. Any ideas why? I should be able to assign astring[0] to a char pointer correct?
/*
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;
}
error message:
pgm5.cpp: In function ‘std::vector > split_string(std::__cxx11::string, char)’:
pgm5.cpp:257:34: error: invalid conversion from ‘__gnu_cxx::__alloc_traits >::value_type {aka char}’ to ‘char*’ [-fpermissive] for (char c = string_to_split[0]; c == split_char; c++) ^
pgm5.cpp:257:40: error: expected primary-expression before ‘==’ token for (char c = string_to_split[0]; c == split_char; c++) ^~
pgm5.cpp:269:34: error: invalid conversion from ‘__gnu_cxx::__alloc_traits >::value_type {aka char}’ to ‘char*’ [-fpermissive] for (char c = string_to_split[0]; c; c++) ^
pgm5.cpp:269:39: error: expected primary-expression before ‘;’ token
for (char c = string_to_split[0]; c; c++) ^Compilation failed.