简体   繁体   English

遍历C ++中的字符串列表,出了什么问题?

[英]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 . 但是我的程序崩溃,提示Illegal Instruction What am I doing wrong? 我究竟做错了什么?

*i is a String , not a char * . *iString ,而不是char * If log.debug() is a function of the printf family, you want a zero-terminated string. 如果log.debug()printf系列的函数,则需要一个以零结尾的字符串。 Depending on how your String class is implemented you might have a function that returns a const char * . 根据String类的实现方式,您可能具有返回const char *const char *

For example with std::string that function is c_str : 例如,使用std::string函数是c_str

for(std::list<std::string>::const_iterator i = my_list.begin(); i != my_list.end(); ++i)
{
     printf("%s\n", i->c_str());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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