C++ vector of char array

2020-02-05 01:58发布

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

7条回答
走好不送
2楼-- · 2020-02-05 02:36

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;
}
查看更多
登录 后发表回答