Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am using a library which accepts data as a vector of char s. I need to pass a string to the library.

I think about using std::vector constructor which accepts iterators to carry out the conversion - but wondered if there is a better way of doing it?

/*Note: json_str is of type std::string*/
const std::vector<char> charvect(json_str.begin(), json_str.end()); 
                Are you just curious, or did you actually measure and determine that a significant part of your program is spent on copying strings?
– Kerrek SB
                Nov 23, 2011 at 19:29

Nope, that's the way to do it, directly initializing the vector with the data from the string.

As @ildjarn points out in his comment, if for whatever reason your data buffer needs to be null-terminated, you need to explicitly add it with charvect.push_back('\0').

Also note, if you want to reuse the buffer, use the assign member function which takes iterators.

Note that if the data needs to be null-terminated, the terminating null will have to be pushed back manually. – ildjarn Nov 23, 2011 at 19:15 @ildjarn: Good point. However, it would be strange for a data buffer to be null terminated, but you never know... – Xeo Nov 23, 2011 at 19:17 Note that since C++11 std::string is forced to use null terminated strings in its internal buffer. So that push_back is no longer necessary. – Manu343726 Apr 6, 2014 at 16:26 @Manu343726: It still is. Just because it's mandated to be internally null-terminated doesn't change the fact that the '\0' is not part of the string elements. If you iterate over the string, you won't get a '\0'. – Xeo Apr 6, 2014 at 18:42 I concur about using the vector member function assign for setting a new value. In general, it is best to make use of provided member functions of a class since library developers have the opportunity of optimizing the implementation with the class in mind. – Eric Feb 16, 2021 at 2:39

Your method of populating the vector is fine -- in fact, it's probably best in most cases.

Just so that you know however, it's not the only way. You could also simply copy the contents of the string in to the vector<char>. This is going to be most useful when you either have a vector already instantiated, or if you want to append more data to the end -- or at any point, really.

Example, where s is a std::string and v is a std::vector<char>:

std::copy( s.begin(), s.end(), std::back_inserter(v));

As with the constructor case, if you need a null-terminator then you'll need to push that back yourself:

v.push_back('\0');

You can do it in this way.

  std::string s = "Hello World!";
    std::vector<char> v(s.begin(), s.end());
    for (const char &c: v)
        std::cout << c;

An alternative that might be worth considering if you need the terminating null is:

std::vector<char> charvect(json_str.c_str(), json_str.c_str() + json_str.size() + 1); 

and if charvect already exists you can do:

charvect.assign(json_str.c_str(), json_str.c_str() + json_str.size() + 1);

This might be quicker than doing a push_back('\0') particularly if the push_back triggers a memory reallocation.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.