I am trying to write a program that has a vector of char arrays and am have some problems.
char test [] = { 'a', 'b', 'c', 'd', 'e' };
vector<char[]> v;
v.push_back(test);
Sorry this has to be a char array because I need to be able to generate lists of chars as I am trying to get an output something like.
a a a b a c a d a e b a b c
Can anyone point me in the right direction?
Thanks
You can use boost::array to do that:
Edit:
Or you can use a vector of vectors as shown below:
You can directly define a char type vector as below.
Use
std::string
instead of char-arraysYou need
Of if you meant to make a vector of character instead of a vector of strings,
The expression
sizeof(test)/sizeof(*test)
is for calculating the number of elements in the array test.You cannot store arrays in vectors (or in any other standard library container). The things that standard library containers store must be copyable and assignable, and arrays are neither of these.
If you really need to put an array in a vector (and you probably don't - using a vector of vectors or a vector of strings is more likely what you need), then you can wrap the array in a struct:
and then create a vector of structs:
FFWD to 2019. Although this code worketh in 2011 too.
Advice: try optimizing the use of
std::string
. For char bufferingstd::array<char,N>
is the fastest,std::vector<char>
is faster.https://wandbox.org/permlink/wcasstoY56MWbHqd