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
This question is related to
c++
arrays
collections
FFWD to 2019. Although this code worketh in 2011 too.
// g++ prog.cc -Wall -std=c++11
#include <iostream>
#include <vector>
using namespace std;
template<size_t N>
inline
constexpr /* compile time */
array<char,N> string_literal_to_array ( char const (&charrar)[N] )
{
return std::to_array( charrar) ;
}
template<size_t N>
inline
/* run time */
vector<char> string_literal_to_vector ( char const (&charrar)[N] )
{
return { charrar, charrar + N };
}
int main()
{
constexpr auto arr = string_literal_to_array("Compile Time");
auto cv = string_literal_to_vector ("Run Time") ;
return 42;
}
Advice: try optimizing the use of std::string
. For char buffering std::array<char,N>
is the fastest, std::vector<char>
is faster.
Use std::string
instead of char-arrays
std::string k ="abcde";
std::vector<std::string> v;
v.push_back(k);
What I found out is that it's OK to put char* into a std::vector:
// 1 - A std::vector of char*, more preper way is to use a std::vector<std::vector<char>> or std::vector<std::string>
std::vector<char*> v(10, "hi!"); // You cannot put standard library containers e.g. char[] into std::vector!
for (auto& i : v)
{
//std::cout << i << std::endl;
i = "New";
}
for (auto i : v)
{
std::cout << i << std::endl;
}
You can directly define a char type vector as below.
vector<char> c = {'a', 'b', 'c'};
vector < vector<char> > t = {{'a','a'}, 'b','b'};
You need
char test[] = "abcde"; // This will add a terminating \0 character to the array
std::vector<std::string> v;
v.push_back(test);
Of if you meant to make a vector of character instead of a vector of strings,
std::vector<char> v(test, test + sizeof(test)/sizeof(*test));
The expression sizeof(test)/sizeof(*test)
is for calculating the number of elements in the array test.
You can use boost::array to do that:
boost::array<char, 5> test = {'a', 'b', 'c', 'd', 'e'};
std::vector<boost::array<char, 5> > v;
v.push_back(test);
Edit:
Or you can use a vector of vectors as shown below:
char test[] = {'a', 'b', 'c', 'd', 'e'};
std::vector<std::vector<char> > v;
v.push_back(std::vector<char>(test, test + sizeof(test)/ sizeof(test[0])));
In fact technically you can store C++ arrays in a vector, and it makes a lot of sense. Not directly, but by a simple workaround, wrapping in a class, will meet exactly all the requirements of a multidimensional array. As the question is already answered by anon. Some explanations steel needed. STL already provides std::array for these purposes.
Is an unpleasant surprise to fall in the trap of not understanding clearly the difference between arrays and pointers, between multidimensional arrays and arrays of arrays, and so on and so on. Vectors of vectors contains vectors as elements. Each element containing a copy of size, capacity and maybe other things, meanwhile the vector datas for elements will be placed in different random places in memory. But a vector of arrays will contain a contiguous segment of memory with all data, which is identical to multidimensional array. Also there is no good reason to keep the size of each array element while it is known to be the same for all elements.
So, making a vector of array, you can't do it directly. But you can workaround it easily by wrapping the array in a class, and in this sample the memory will be identical to the memory of a bidimensional array. This approach is already widely used by many libraries. At low level it will be easily interoperable with APIs that are not C++ vector aware. So without using std::array it will look like this:
int main()
{
struct ss
{
int a[5];
int& operator[] (const int& i) { return a[i]; }
} a{ 1,2,3,4,5 }, b{ 9,8,7,6,5 };
vector<ss> v;
v.resize(10);
v[0] = a;
v[1] = b;
v.push_back(a); // will push to index 10, with reallocation
v.push_back(b); // will push to index 11, with reallocation
auto d = v.data();
// cin >> v[1][3]; //input any element from stdin
cout << "show two element: "<< v[1][2] <<":"<< v[1][3] << endl;
return 0;
}
Since C++11 STL contains std::array for these purposes, so no need to reinvent it:
....
#include<array>
....
int main()
{
vector<array<int, 5>> v;
v.reserve(10);
v.resize(2);
v[0] = array<int, 5> {1, 2, 3, 4, 5};
v[1] = array<int, 5> {9, 8, 7, 6, 5};
v.emplace_back(array<int, 5>{ 7, 2, 53, 4, 5 });
///cin >> v[1][1];
auto d = v.data();
Now look how looks in memory
Now, this is why vectors of vectors is not the answer. Supposing following code
int main()
{
vector<vector<int>> vv = { { 1,2,3,4,5 }, { 9,8,7,6,5 } };
auto dd = vv.data();
return 0;
}
Guess what it looks like in the memory now
Source: Stackoverflow.com