Jump to content
  • Advertisement
Sign in to follow this  
alex_and_his_towel

.

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!