可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
回答1:
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:
struct S {
char a[10];
};
and then create a vector of structs:
vector <S> v;
S s;
s.a[0] = 'x';
v.push_back( s );
回答2:
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.
回答3:
Use std::string
instead of char-arrays
std::string k ="abcde";
std::vector<std::string> v;
v.push_back(k);
回答4:
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])));
回答5:
You can directly define a char type vector as below.
vector<char> c = {'a', 'b', 'c'};
vector < vector<char> > t = {{'a','a'}, 'b','b'};
回答6:
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.
https://wandbox.org/permlink/wcasstoY56MWbHqd
回答7:
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;
}