12

I have a Visual Studio 2008 C++ function where I'm given an array of null-terminated strings const char* and a count of the number of strings in that array.

I'm looking for a clever way of turning an array of const char* in to a std::vector< std::string >

/// @param count - number of strings in the array
/// @param array - array of null-terminated strings
/// @return - a vector of stl strings
std::vector< std::string > Convert( int count, const char* array[] );

Boost is fine, STL is fine.

7
  • 8
    That's not an array of const char*. Do you mean that the strings are stored sequentially and separated by NULLs? Commented Dec 16, 2010 at 18:38
  • 2
    There, I fixed it since the OP wasn't responding and the question doesn't make any sense otherwise. Commented Dec 16, 2010 at 18:45
  • Of course it made sense, just a different from one you are suggesting Commented Dec 16, 2010 at 18:55
  • 1
    Yes, that's what I meant. @Noah - Thanks for fixing it. Commented Dec 16, 2010 at 19:06
  • @Gene: The OP's code didn't match his question, since he said "I'm looking for a clever way of turning an array of const char* in to a std::vector<std::string>" (emphasis mine). He then used a single const char* as the function parameter. The question made far more sense than the code -- ergo the code was edited by Noah to reflect the question. To the extent that the OP meant the code instead, it's incumbent on him to say so -- not on you to downvote all the well-meaning answers to what was probably his question. Commented Dec 16, 2010 at 19:08

4 Answers 4

15

Something like this?:

vector< string > ret( array, array + count );
Sign up to request clarification or add additional context in comments.

7 Comments

It always amazes me the poeple that try and do it the hard-way. +1 for keeping it simple.
Of course, the great thing about this answer is that it is NOT clever.
@Noah - You're right - I should have said 'simple' not 'clever'.
The other great thing about this answer is that it's one line long :)
meh... One liners don't impress me unless they're truly the simplest method to perform the required task.
|
4

Try this:

#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>


int main(int argc,char* argv[])
{
    // Put it into a vector
    std::vector<std::string>    data(argv, argv + argc);

    // Print the vector to std::cout
    std::copy(data.begin(), data.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
}

Comments

3

Assuming that the signature of your function is inadvertently wrong, do you mean something like this?

#include <algorithm>
#include <iterator>
#include <string>
#include <vector>

std::vector<std::string> Convert(int count, const char **arr)
{
    std::vector<std::string> vec;
    vec.reserve(count);
    std::copy(arr, arr+count, std::back_inserter(vec));
    return vec;
}

int main()
{
    const char *arr[3] = {"Blah", "Wibble", "Shrug"};
    std::vector<std::string> vec = Convert(3, arr);
    return 0;
}

2 Comments

This is what I started with and why I decided to ask the question. I knew there was an easier way... My brain was just not bringing it to the surface.
@PaulH: Yup, it's very easy to forget that vector has a constructor designed for exactly this situation -- as I think I've proved in this case :)
0

I assume in your array strings are separated by 0s.

std::vector< std::string > Convert( int count, const char* array )
{
   std::vector< std::string > result;

   for(const char* begin = array; count; --count)
   {
    const char* end = begin;
    for(; *end; ++end);
    result.push_back(std::string(begin, end));
    begin = end + 1;
   }

   return result;
}

2 Comments

This code works for the original question, sort of. That is, originally the questioner's function had this signature and so the assumption that the strings are separated by \0 is reasonable. In that case, this code works. However, the question was edited.
Obviously not, since it was the old version I was commenting ON. But whatever.

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.