In boost, how to pass the boost iterator to a function somehow cast as std :: string

See the specific question as a comment at the end of the following code.

std::string s("my sample string \"with quotes\""); boost::escaped_list_separator<char> els(""," ","\"\'"); boost::tokenizer<boost::escaped_list_separator<char> >::iterator itr; boost::tokenizer<boost::escaped_list_separator<char> > tok(s, els); itr=tok.begin(); if (itr!=tok.end()) fn_that_receives_pointer_to_std_string(itr); // <---- IS IT POSSIBLE TO SEND POINTER AND NOT HAVE TO CREATE A NEW STRING ?? 
+4
source share
3 answers

boost::tokenizer<boost::escaped_list_separator<char> >::iterator not a pointer to std::string , but you can turn it into std::string const * using

 &(*itr) 

If the const pointer is not what you should pass, you can do

 std::string s(*itr); 

and pass &s , depending on the semantics of ownership of fn_that_receives_pointer_to_std_string . Boost Tokenizer does not distinguish between iterator and const_iterator , so the result of operator* always const .

+2
source

*itr will actually return basic_string instead of string , so you need to convert one to another:

 using namespace std; using namespace boost; void fn_that_receives_pointer_to_std_string(string* str) { cout << "str: " << *str << endl; } int main() { string s = "Field 1,\"putting quotes around fields, allows commas\",Field 3"; tokenizer<escaped_list_separator<char> > tok(s); for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end();++beg) { string tmp(*beg); fn_that_receives_pointer_to_std_string(&tmp); } } 

I don't like the idea of ​​passing the memory address of string another function. Consider transferring it with a copy or link.

+1
source

Sorry, this is not possible.

For this reason, the rule "accepts string parameters as std::string " is incorrect. boost::iterator_range<const char*> might be better if the template is not suitable (for example, a separate compilation).

0
source

All Articles