Iterating over a list of Strings in C++, what's going wrong?
I'm trying to print out a list of strings thus:
std::list<String> const &prms = (*iter)->getParams();
std::list<String>::const_iterator i;
for(i = prms.begin(); i != prms.end(); ++i){
log.debug(" Param: %s",*i);
开发者_开发知识库}
But my program crashes saying Illegal Instruction
. What am I doing wrong?
*i
is a String
, not a char *
. If log.debug()
is a function of the printf
family, you want a zero-terminated string. Depending on how your String
class is implemented you might have a function that returns a const char *
.
For example with std::string
that function is c_str
:
for(std::list<std::string>::const_iterator i = my_list.begin(); i != my_list.end(); ++i)
{
printf("%s\n", i->c_str());
}
精彩评论