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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
PaulH
  • 7,759
  • 8
  • 66
  • 143
  • 8
    That's not an array of `const char*`. Do you mean that the strings are stored sequentially and separated by NULLs? – Ben Voigt Dec 16 '10 at 18:38
  • 2
    There, I fixed it since the OP wasn't responding and the question doesn't make any sense otherwise. – Edward Strange Dec 16 '10 at 18:45
  • Of course it made sense, just a different from one you are suggesting – Gene Bushuyev Dec 16 '10 at 18:55
  • 1
    Yes, that's what I meant. @Noah - Thanks for fixing it. – PaulH Dec 16 '10 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`" (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. – Stuart Golodetz Dec 16 '10 at 19:08
  • Stuart, so you are saying that array of const char* cannot be passed to a function by pointer to the first element? – Gene Bushuyev Dec 16 '10 at 19:24
  • @Gene: A pointer to the first element of an array of const char pointers has type `const char **`, since the first element is a `const char *`. You can certainly encode a sequence of null-terminated strings in a char array (and then go on to solve the question from that assumption, as you did) -- and I wasn't arguing otherwise -- but that is not *an array of* `const char *`, which it seems was what the OP meant (it was a simple typo). Anyway, let's move on. – Stuart Golodetz Dec 17 '10 at 11:00

4 Answers4

15

Something like this?:

vector< string > ret( array, array + count );
genpfault
  • 51,148
  • 11
  • 85
  • 139
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"));
}
Martin York
  • 257,169
  • 86
  • 333
  • 562
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;
}
Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
  • 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 Dec 16 '10 at 19:21
  • @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 :) – Stuart Golodetz Dec 16 '10 at 19:24
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;
}
Gene Bushuyev
  • 5,512
  • 20
  • 19
  • 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. – Chris Hopman Dec 16 '10 at 19:41
  • Obviously not, since it was the old version I was commenting ON. But whatever. – Edward Strange Dec 17 '10 at 17:19