C++ vector of char array

后端 未结 7 876
梦谈多话
梦谈多话 2020-12-09 16:06

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

        
相关标签:
7条回答
  • 2020-12-09 17:01

    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.

    0 讨论(0)
提交回复
热议问题