8

How would one go about converting a C char** to a C++ vector? Is there some built-in functionality one can utilize to do this, or is it better to accomplish it through a series of iterative steps?

EDIT: For various reasons, the number of elements in the C array is unknown. It is possible I could pass that as another parameter, but is that absolutely necessary?

MirroredFate
  • 12,396
  • 14
  • 68
  • 100
  • Also see [copying c array of strings into vector of std::string](http://stackoverflow.com/q/6307584/1168156) – LihO Mar 29 '13 at 19:02
  • And [C++ copying a string array to a `vector`](http://stackoverflow.com/q/5839499/1168156) – LihO Mar 29 '13 at 19:05
  • And [converting an array of null terminated const char* strings to a `std::vector`](http://stackoverflow.com/q/4464208/1168156) – LihO Mar 29 '13 at 19:06
  • Wow! Those are very similar. However, they all assume the number of elements is known. – MirroredFate Mar 29 '13 at 19:13
  • 2
    Of course they assume the size of an array (that you have in form of simple pointer) to be known. How else do you want to find out how many strings are stored in your `char**`? – LihO Mar 29 '13 at 19:15
  • I was hoping that there existed some way of finding that out. Since the ubiquitous assumption has been that no such method exists, I suppose there really is no way to determine the number of elements just given a char**... – MirroredFate Mar 29 '13 at 19:19
  • @MirroredFate: there is indeed no way. Unless your array terminates with a sentinel value (e.g. a null pointer). – Oliver Charlesworth Mar 29 '13 at 19:21

4 Answers4

19

You can simply use the constructor of std::vector that takes two iterators:

const char* arr[] = {"Hello", "Friend", "Monkey", "Face"};
std::vector<std::string> v(std::begin(arr), std::end(arr));

Or if you really have a const char**:

const char** p = arr;
std::vector<std::string> v(p, p + 4);

Which will also work with directly using arr instead of p due to array-to-pointer conversion.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
8
char** c;
vector<string> v(c, c + 10);

Will construct elements from element of given range. 10 is number of elements here

RiaD
  • 46,822
  • 11
  • 79
  • 123
  • The assumption here, and with all the answers, is that the number of elements is known... – MirroredFate Mar 29 '13 at 19:12
  • 1
    @MirroredFate, you should always know number of elements in c-style array(or pointer points to consecutive elements). In other case you can't do anything, (except deleting if you know it was new'ed and freening if it was malloc'ed) – RiaD Mar 29 '13 at 19:14
1

You can use the constructor of std::vector that takes two iterators, a.k.a. the range constructor:

char* strings[] = {"aaa", "bbb", "ccc", "ddd"};
std::vector<std::string> v(strings, strings + 4);

where 4 is the size of your array. In this concrete example, the calculation of the size of the strings array would be also possible by using sizeof operator:

int len = sizeof(strings)/sizeof(char*);
std::vector<std::string> v2(strings, strings + len);

which wouldn't be possible with pure char** though since the size of the array can not be directly retrieved from a pointer in any way (also worth to read something about array decaying).

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
0

This one-liner is useful for capturing command line arguments...

int
main(int argc, char ** argv)
{
  std::vector<std::string> arguments(argv, argv + argc);
}
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
emsr
  • 15,539
  • 6
  • 49
  • 62