0

I want to split given string with delimiter "," but its give error

"a value of type \"char *\" cannot be assigned to an entity of type \"std::string *\"

hear is my code

      #include<iostream>
      #include<string>
      int main()
      {
      std::string str = "1,2,3,4,5,6,7";
      std::string* ptr;          
      ptr=strtok(str,",");
      while(ptr!=NULL)
      {
         std::cout<<ptr<<std::endl;
         ptr=strtok(NULL,",");
      }
    }
  • For the C++ way to split a string, see: https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c – NathanOliver Apr 07 '20 at 14:38
  • 1
    Of course you can do: const std::string test{ "1,2,3,4,5,6,7,8,9" }; std::vector data{}; for (char* token = std::strtok(const_cast(test.data()), ","); token != nullptr; token = std::strtok(nullptr, ",")) { data.push_back(token); } – A M Apr 07 '20 at 14:45

0 Answers0