Vector of character arrays

I have declared a vector by "std::vector<char[20]> token;".

I expect this to create an empty vector of tokens, each 20 characters long.

When I then add an item to the vector with "token.push_back(" ");" the compiler complains:

"error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'const char [2]' to 'const char (&)[20]'"

I do not understand this. My aim is to create an entry so that I can then plug characters into it by something like token[iToken][iChar] = "X".
A vector is not able to hold arrays like you are trying to do.

I suggest you just use strings and limit the length of them instead if you always want them less than 20 characters.
Thank you. I'll try that.

I was using char because I am using atoi and atof later to parse some numbers from text, and I think they only work with char arrays.

Is there some way of parsing from strings?
I expect this to create an empty vector of tokens, each 20 characters long.


you can try this:

1
2
3
4
int numbers_of_data;
std::cout << "how many data you want?: ";
std::cind >> numbers_of_data;
char * ori_data = new [numbers_of_data] [20] char;


btw, it's not tested yet, but i think it will works...
Thanks to both of you for getting me thinking.

I have now found that I can do exactly what I wanted to do by using a structure (or class) when declaring the vector. The following works just fine.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <vector>
#include <iostream>

int main ()
{
	struct token
	{
		char ch[10];
	};

	std::vector<token> tokens;
	token empty;

	// There has to be a neater way of doing this!
	for (int i = 0; i < 9 ; i = i + 1)
		empty.ch[i] = "123456789"[i];
	empty.ch[9] = 0;

	// Add three tokens to the vector
	tokens.push_back(empty);
	tokens.push_back(empty);
	tokens.push_back(empty);

	// Make each token unique
	tokens[0].ch[1] = '*';
	tokens[1].ch[4] = '*';
	tokens[2].ch[7] = '*';

	// Output the tokens
	std::cout << tokens[0].ch << "\n";
	std::cout << tokens[1].ch << "\n";
	std::cout << tokens[2].ch << "\n";

	std::cout << "Press enter to end:";
	std::cin.get();

	return 0;
}
Topic archived. No new replies allowed.