[英]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 *
. *i
是String
,而不是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.