Просто не используйте strtok.
Используйте оператор потока C ++.
Функцию getline () можно использовать с дополнительным параметром, который определяет маркер конца строки.
#include <string>
#include <sstream>
#include <vector>
int main()
{
std::string text("This is text; split by; the semicolon; that we will split into bits.");
std::stringstream textstr(text);
std::string line;
std::vector<std::string> data;
while(std::getline(textstr,line,';'))
{
data.push_back(line);
}
}
Проделав чуть больше работы, мы даже можем заставить алгоритмы STL оплачивать свою работу, нам просто нужно определить, как токен передается в потоковом режиме. Для этого просто определите класс токена (или структуру), а затем определите оператор >>, который читает до разделителя токена.
#include <string>
#include <sstream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>
struct Token
{
std::string data;
operator std::string() const { return data;}
};
std::istream& operator>>(std::istream& stream,Token& data)
{
return std::getline(stream,data.data,';');
}
int main()
{
std::string text("This is text; split by; the semicolon; that we will split into bits.");
std::stringstream textstr(text);
std::vector<std::string> data;
// This statement does the work of the loop from the last example.
std::copy(std::istream_iterator<Token>(textstr),
std::istream_iterator<Token>(),
std::back_inserter(data)
);
// This just prints out the vector to the std::cout just to illustrate it worked.
std::copy(data.begin(),data.end(),std::ostream_iterator<std::string>(std::cout,"\n"));
}