Jump to content
  • Advertisement
Sign in to follow this  
gohkgohk

Address problem

This topic is 3789 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

struct coordinate {
  int x, y, z;
  coordinate(int x, int y, int z): x(x), y(y), z(z) {}
};

typedef std::vector<coordinate> triangle_list;
typedef std::map<coordinate, triangle_list> space_mapping;
space_mapping base_array;

triangle_list& t = base_array[coordinate(3, 5, 5)]; 
t.push_back(coordinate(100, 250, 300));

///continuous here




what code i need to add in "///continuous here",so that i can change the value of t,and make it target to base_array[coordinate(1, 2, 2)]? i get error when i add the following in there: t = base_array[coordinate(1, 2, 3)]; And i can add value in the map continuously. THX

Share this post


Link to post
Share on other sites
Advertisement
Now I could be wrong, so someone else please correct me if it is so, but I do believe that because you make t a reference, you are unable to reassign it. Try making t a pointer. Sure you'll have to use -> instead of . to access the data, but you'll be able to change the object to which t is pointing to.

Share this post


Link to post
Share on other sites
No. You're passing a constructor for a struct to a bracket argument that expects an integer. You need to convert your x,y,z coordinates into a single array index; dynamic arrays (and all arrays really) are just a single line of data.

The formula for multi-dimensional array indexing is something like:


//assuming:
// x = width
// y = height
// z = depth
int index = x + y*width + z*width*height;



I think I have that right...

-me

Share this post


Link to post
Share on other sites
The problem is that std::map works by comparing whether one key is less-than another key using a Strict-Weak-Ordering predicate function.

In this case a key is an instance of coordinate and you have not provided a way to say whether one coordinate is less-than another coordinate.
With normal scalar numbers you can easily say the 2.5 < 9.9 is true without much trouble, but there is no existing predicate function in C++ that'll say (2, 3, 4) < (4, 3, 2) is true or false for example.

There are 3 solutions:

1) Overload the less-than operator of the coordinate class to return a strict-weak-ordering comparison:
bool operator < (const coordinate& c) const { return blah; }

2) Add a free function which takes two coordinate classes and returns a strict-weak-ordering comparison:
bool operator < (const coordinate& a, const coordinate& b) const { return blah; }

3) Write a comparison functor and pass it as the third template parameter to the map:
struct coordinateLessThan
{
bool operator()(const coordinate& a, const coordinate& b) const
{
return blah;
}
};

typedef std::map<coordinate, triangle_list, coordinateLessThan> space_mapping;

Incidentally, you might want to know why I've always returned 'blah', it is because it's a tad complicated to give/remember the strict-weak-ordering of a vector quantity with 3 components. I think there's maybe 13 such orders, take a look at wikipedia.

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
No. You're passing a constructor for a struct to a bracket argument that expects an integer.

The std::map operator[] doesn't expect an integer, it expects a key_type which in this case is a coordinate.

Share this post


Link to post
Share on other sites
Yes, references can only be initialized, never reassigned.

If you switch 't' to a pointer, make sure you delete the old contents of 't' somewhere or you'll have a memory leak.

Share this post


Link to post
Share on other sites
Quote:
Original post by ravyne2001
If you switch 't' to a pointer, make sure you delete the old contents of 't' somewhere or you'll have a memory leak.
He's only using the pointer to reference an existing object, so no deletion is necessary.

@The OP: A reference really would be more appropriate here, so you might consider re-arranging your code to make the use of a reference possible (for example, by finding a way to add the data within a loop of some sort).

Share this post


Link to post
Share on other sites
Quote:
Original post by dmatter
The std::map operator[] doesn't expect an integer, it expects a key_type which in this case is a coordinate.


Yeah, my bad. I misread the code

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
Yeah, my bad. I misread the code

And I misread the question, I thought the OP was asking why his posted code won't work. [rolleyes]

Share this post


Link to post
Share on other sites
Yeah, my bad for not catching that.

OP: My advice only applies for dynamically allocated memory.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!