views:

88

answers:

4

hi, I'm trying to get "CMtoaPlugin::listArnoldNodes()" to return an "array" of strings

   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

initialize Arnold shader †¡/

initialize Arnold shader.

what am i doing wrong

+7  A: 

You can not print a std::string with printf (or any varargs method). g++ gives a warning here:

warning: cannot pass objects of non-POD type ‘struct std::string’ through ‘...’; call will abort at runtime

Just use cout:

std::cout << "initialize shader " << *it << std::endl;
Matthew Flaschen
+6  A: 

Another possibility is to print the C-string corresponding to std::string with printf, like this:

 printf("initialize shader %s\n", it->c_str());
jpalecek
+4  A: 

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())
  • In an iterator for-loop, it's preferable to use 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).
tzaman
A: 

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. It's not necessarily more "correct", but it is more idiomatic.

Shamster
True; the reason, however, is not that `container.end()` is past-the-end, but because loops written with `<` only work for RandomAccessIterators, whereas `!=` works for all of them.
jpalecek