[英]returning a std::string with an vector
I'm trying to get "CMtoaPlugin::listArnoldNodes()" to return an "array" of strings 我正在尝试获取“ CMtoaPlugin :: listArnoldNodes()”以返回字符串的“数组”
std::vector<std::string> ArnoldNodes = CMtoaPlugin::listArnoldNodes();
std::vector<std::string>::iterator it;
for ( it=ArnoldNodes.begin() ; it < ArnoldNodes.end(); it++ )
{
printf("initialize shader %s\n", *it);
}
but this is what i get, 2 entries, that's correct but the content of the entry is not 但这是我得到的2个条目,这是正确的,但条目的内容不是
initialize Arnold shader †¡/ 初始化Arnold着色器†¡/
initialize Arnold shader. 初始化Arnold着色器。
what am i doing wrong 我究竟做错了什么
You can not print a std::string with printf (or any varargs method). 您不能使用printf(或任何varargs方法)打印std :: string。 g++ gives a warning here:
g ++在这里给出警告:
warning: cannot pass objects of non-POD type ‘struct std::string’ through ‘...’; call will abort at runtime
Just use cout: 只需使用cout:
std::cout << "initialize shader " << *it << std::endl;
另一种可能性是使用printf
打印与std::string
对应的C字符串,如下所示:
printf("initialize shader %s\n", it->c_str());
Try it like this: 像这样尝试:
for (it = ArnoldNodes.begin() ; it != ArnoldNodes.end(); ++it)
{
std::cout << "initialize shader " << *it << std::endl;
}
printf
doesn't work with std::string
, you need to use cout
(or pass it it->c_str()
) printf
不适用于std::string
,您需要使用cout
(或将它传递给it->c_str()
) it != vec.end()
(since you only need to check for equality, not compare), and ++it
to increment (post-increment can be less efficient for some iterators). it != vec.end()
(因为您只需要检查相等性,而不是比较),而++it
可以递增(某些迭代器的后递增效率可能较低) )。 When you for-loop across your iterator range, you should be performing it using : 在迭代器范围内进行循环时,应使用以下命令执行:
for ( it = ArnoldNodes.begin() ; it != ArnoldNodes.end(); it++ )
{ /*...*/ }
the difference is that the comparison is !=
instead of <
, because container.end()
iterators return one-past-the-end of the container. 区别在于比较是
!=
而不是<
,因为container.end()
迭代器返回的是容器的最后一点。 It's not necessarily more "correct", but it is more idiomatic. 它不一定更“正确”,但是更惯用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.