.

Started by
4 comments, last by Will F 18 years, 4 months ago
[Edited by - alex_and_his_towel on December 1, 2005 6:18:10 PM]
Advertisement
You could use sizeof(COLOURS) / sizeof(string).
Have a look at std::vector. With this you can just inspect COLOURS.size()

It also makes adding/removing so much easier.

Good Luck
Quote:Original post by alex_and_his_towel
Quote:Original post by SiCrane
You could use sizeof(COLOURS) / sizeof(string).


Thanks. That's odd though... I would have expected sizeof(COLOURS) to give the number of elements, but instead it gives me 4 x (number of colours). Does '4' have to do with the number of bytes a string uses in memory?


Yes; it's the sizeof(string) - on your compiler's implementation of it.
The sizeof() an array is the total size taken up by the array contents.

Note that in this case, that is *not* the total amount of memory used to represent the text data (as obviously you can't fit "PURPLE", for example, in 4 bytes). That's because the string objects can contain pointers (in your case, it appears that the data layout consists of a single pointer to a bunch of data somewhere else) to additional memory "somewhere else". The semantics of std::string are such that all "outside" memory it points to (in every implementation this at least includes a dynamic buffer into which characters may be written; in some - like yours, apparently - it includes the rest of the structure, hidden behind a pointer at the "top level" of the struct; in some there is a static buffer as well, which may or may not be "outside") are "owned" by that std::string - noone else is supposed to know it is there (although you can access it by the operator[], but saving a pointer into there is a Bad Idea(TM)), and the object will delete[] the allocations when it is destroyed (because nothing else is supposed to depend on them).

Quote:
Quote:Original post by Simian Man
Have a look at std::vector. With this you can just inspect COLOURS.size()

It also makes adding/removing so much easier.


Heh, okay. I actually put my book aside to start this Mastermind game right before the chapter on vectors ^_^ . I think I'll try to finish it with arrays so that I get familiar with them, then try another game with vectors/pointers etc.

Alex.


Yep. You wouldn't be able to add to the illustrated array at all; and if you specified it as a string* rather than a string[], then (a) the sizeof trick wouldn't work (the sizeof a pointer is the size of, well, the actual pointer) and (b) it would still be surprisingly tricky to handle the memory allocation.

vectors are not hard to work with; a string is not much different from a vector of chars, in terms of implementation - except that it provides extra functionality that's specific to interpreting the contents as text data, and is optimized in favour of situations where the contents are considered as text data.
Quote:Original post by alex_and_his_towel
I'm writing a Mastermind game in C++ and have the following code near the top:
const string COLOURS[] = {"Black", "White", "Blue", "Green",                          "Red", "Yellow", "Pink", "Purple"};


Here's an example of one way to do this with a std::vector

#include <string>#include <vector>#include <iostream>using namespace std;int main(){    // create a vector of strings    vector<string> colours;    // add some strings to the vector    colours.push_back("Black");    colours.push_back("White");    colours.push_back("Blue");    colours.push_back("Green");    colours.push_back("Red");    colours.push_back("Yellow");    colours.push_back("Pink");    colours.push_back("Purple");    // output the contents of the vector to the console    for (int i = 0; i < colours.size(); ++i)        cout << colours << endl;    // remove the last element of the vector    colours.pop_back();    // output the contents of the vector to the console    for (int i = 0; i < colours.size(); ++i)        cout << colours << endl;}


Program's output:
Black
White
Blue
Green
Red
Yellow
Pink
Purple

Black
White
Blue
Green
Red
Yellow
Pink
Quote:Original post by alex_and_his_towel
Does that mean there's no way to simply assign a list of elements to a vector straight off?


Try this:
char *words[] = {"Black", "White", "Blue", "Green", "Red", "Yellow", "Pink", "Purple"};size_t words_size = sizeof(words) / sizeof(char *);vector<string> colours(words, words + words_size);


Personally I try to avoid character arrays when possible, but the above code should work for you.

This topic is closed to new replies.

Advertisement