This program confused me!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstring>
#include <vector>

using namespace std;

int main() {
	char *words[]={"stately", "plump", "buck", "mulligan"};/* words is array of pointers to 
c_string, words is the very beginning pointer of the array */

	size_t words_size =sizeof(words)/sizeof(char *);/* sizeof(words) will give the size of the array,
 which is 4, right? sizeof(char *) will give what result? each word in words are different size string! 
And what is this words_size? */

	vector<string> words2(words, words+words_size); /*because I don't understand words_size, 
so I don't understand this expression either! */

	for(vector<string>::iterator it=words2.begin(); it<words2.end(); it++) {
		cout<<*it<<endl;
	}

	return 0;
}

This program will output
1
2
3
4
stately
plump
buck
mulligan

to the screen
My question are in the code comments, please , someone explain this for me! Thanks.
Last edited on
> sizeof(words) will give the size of the array, which is 4, right?
wrong, ┬┐why don't just simply observe what the value holds?

By the way, it should be const char* words[]
Topic archived. No new replies allowed.