简体   繁体   中英

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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM