How to add a char to a char vector?

May 11, 2013 at 2:17pm
I'm trying to build words inside of vectors by adding one character at a time but it won't let me add the char. How can I bypass this? Also, is my use of vector correct? Will this vector variable be able to store 50 words rather then just 50 characters?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  void Translator::toElvish(char out_s[], const char s[])
{
	int wordCount = 0;
	vector <char> engW[50];
	dictionary(Dictionary);
	for (int i = 0; i < strlen(s); i++)
	{
		char c = s[i]; 
		if (c !=' '||'\t'||'\n'||','||'.')
		{
		 	engW[wordCount] + c;
		}
		if (c == ' ')
		{
			wordCount + 1;
		}
	}
May 11, 2013 at 2:22pm
http://cplusplus.com/reference/vector/vector/push_back/

1
2
3
// engW[wordCount] + c;

engW.push_back(c);


Edit: uhh...

engW[wordCount].push_back(c);
Last edited on May 11, 2013 at 2:46pm
May 11, 2013 at 2:48pm
Yeah, I copped the [wordCount], thanks for the help :)
May 11, 2013 at 2:55pm
Your use of vector is correct, you'll be able to store 50 words. But you can attain even more flexibility by doing this:

 
std::vector<std::vector<char>> engW;


Now you can have a dynamic amount of words, each with a dynamic amount of characters.
May 11, 2013 at 3:02pm
I have a suggestion: please use elements of modern C++ instead of leftovers from good old C.

Specifically, don't use char arrays if you are permitted to use std::string.

Furthermore, I don't truly understand what your code does; still my intuition tells me that you would be better off using std::string instead of std::vector<char>.

Note, std::string is in essence an alias for std::basic_string<char> so it's still a char container just like std::vector<char>.

http://cplusplus.com/reference/string/
Topic archived. No new replies allowed.