0

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

1
  • 1
    You'll have to show us the code in listArnoldNodes(). Commented Jul 30, 2010 at 22:11

4 Answers 4

7

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;
Sign up to request clarification or add additional context in comments.

Comments

6

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

 printf("initialize shader %s\n", it->c_str());

Comments

4

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).

Comments

0

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.