-
boost tokenizer problem
Hello..
Whenever I compile this code, I get boost compile error, though I have no idea what could be wrong.
Code:
#include <string>
#include <iostream>
#include <boost/tokenizer.hpp>
using namespace std;
int parse(string data) {
typedef boost::tokenizer <boost::escaped_list_separator<char> > tokenizer;
tokenizer tokens(data);
for (tokenizer::iterator it = tokens.begin(); it != tokens.end(); it++) {
cout << *it << endl;
cout << *(it - 1) << endl; //here i want to print the previous iterator (if it exists)
}
return 0;
}
Thanks a lot for help
-
You can't do (it - 1) in the code.
The iterator you are working with will ONLY support ++. Not --, not +, not -.
One solution is to copy the elements out of the tokenizer into a standard container. For example:
Code:
#include <string>
#include <vector>
#include <iostream>
#include <boost/tokenizer.hpp>
int parse(std::string data) {
typedef boost::tokenizer <boost::escaped_list_separator<char> > tokenizer;
tokenizer tokens(data);
std::vector<std::string> vectorTokens;
std::copy(tokens.begin(),tokens.end(),std::back_inserter(vectorTokens));
for (std::vector<std::string>::iterator it = vectorTokens.begin(); it != vectorTokens.end(); ++it) {
std::cout << *it << std::endl;
std::cout << *(it - 1) << std::endl; //here i want to print the previous iterator (if it exists)
}
return 0;
}
Of course, you need to do something special to handle the very first iteration through the loop, or you'll try to walk off the beginning of the vector with the it-1.
-
Easier to just save the token for one iteration and print it again.
-
One more thing guys...
This is the code for escaped_list_separator tokenizer, but I would like to have first escaped list separator and then char separator. What would be the best way to use both one after another?
Lets say I have string "here is \"my string\" some,text".
I should get <here> <is> <my string> <some> <text>