-3

This is my split function:

std::vector<std::string> split(std::string& s, const char delimiter, bool ignore_empty = false){

      std::vector<std::string> result;
      std::string tmp = s;

      while(tmp.find(delimiter) != std::string::npos) {
            std::string new_part = tmp.substr(0, tmp.find(delimiter));
            tmp = tmp.substr(tmp.find(delimiter)+1, tmp.size());

            if(not (ignore_empty and new_part.empty())) {
                  result.push_back(new_part);
            }
      }
      if(not (ignore_empty and tmp.empty())){
      result.push_back(tmp);
      }
return result; }

I'm calling the split function like this:

vector<std::string> tiedot = split(line, ";", true);

Where the line is: S-Market;Hervantakeskus;sausage;3.25

I need to split the string to strings and add them to a vector but I get this

Error: Invalid conversion from const char * to char

Do you know how to fix this?

O'Neil
  • 3,790
  • 4
  • 16
  • 30
Teemu
  • 49
  • 4

2 Answers2

2

Here is a split function I found on stackoverflow some months ago.

std::vector<std::string> fSplitStringByDelim(const std::string &text, char sep) 
{    
std::vector<std::string> tokens;
size_t                   start = 0, end = 0;

//until u find no more delim
while ((end = text.find(sep, start)) != std::string::npos)
{
    //if string not empty
    if(text.substr(start, end - start) != "")
    {   
        //push string
        tokens.push_back(text.substr(start, end - start));
    }

    start = end + 1;
}
//avoid empty string
if(text.substr(start) != "")
{
    tokens.push_back(text.substr(start));
}
return tokens;
}

You can call this function with:

vector<std::string> tiedot = fSplitStringByDelim(line, ';');
1

Try this

vector<std::string> tiedot = split(line, ';', true);
O'Neil
  • 3,790
  • 4
  • 16
  • 30
voltento
  • 833
  • 10
  • 26