-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?

6
  • 1
    What is not and what is and? This is not valid C++. Commented Feb 16, 2018 at 11:21
  • @Michael Walz That's what I thought, but I found alternative operators at en.cppreference.com/w/cpp/language/operator_alternative I hope it's standard Commented Feb 16, 2018 at 11:23
  • @MichaelWalz He's just calling his function wrong. Commented Feb 16, 2018 at 11:26
  • 2
    No his problem is he provided a const char * string literal to an argument taking a const char. Isn't this interesting... en.cppreference.com/w/cpp/keyword/not Commented Feb 16, 2018 at 11:26
  • Where did you learn "not" keyword? I've never seen it but I like it. Commented Feb 16, 2018 at 11:27

2 Answers 2

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, ';'); 
Sign up to request clarification or add additional context in comments.

1 Comment

That does not answer the question at all.
1

Try this

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.