使用向量返回std :: string

ray*_*ond 0 c++ iteration stdstring stdvector

我试图让"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);
   }
Run Code Online (Sandbox Code Playgroud)

但这是我得到的,2个条目,这是正确的,但条目的内容不是

初始化Arnold着色器†¡

初始化阿诺德着色器.

我究竟做错了什么

Mat*_*hen 7

您不能使用printf(或任何varargs方法)打印std :: string.g ++在这里发出警告:

warning: cannot pass objects of non-POD type ‘struct std::string’ through ‘...’; call will abort at runtime
Run Code Online (Sandbox Code Playgroud)

只需使用cout:

std::cout << "initialize shader " << *it << std::endl;
Run Code Online (Sandbox Code Playgroud)


jpa*_*cek 6

另一种可能性是打印对应于C-串std::stringprintf,像这样:

 printf("initialize shader %s\n", it->c_str());
Run Code Online (Sandbox Code Playgroud)