I am making a stack-like system where it stores classes by a specific member in least to greatest order. At first, I used a vector. But found that it wasn't easy to access a specific member just by knowing an aspect about them. So I tried to implement Maps (obviously for the identification Keys) but found that when I used them, they didn't quite have the array-like format that vectors do.
I don't know much about the sorting function, but I want to sort the map from least to greatest by a specific member of the stored class, not just by it's key (because the member could be the same as other members, which is why I don't use it as a key. The key will be used to delete the members and edit their properties so a Multimap might not work for what I want to do).
The way the system worked with the vector was that, when a specific event occurred, the stack deleted the first member and caused all of the other members to move down. And then when a function was called, I wanted a variable to be set in a specific member of the vector, but I was having trouble with identification.
I want the map key functionality of a map, and the stack (or array) - like functionality of a vector (it moves members down when an element is deleted and shifts members up when something is inserted)
I don't know if it's possible to make a custom map sorting function that sorts the map not by keys, but by a member of an element stored in the map, as well as act like a vector, so any help would be appreciated.
There might even be a different storage container that does this!
I was thinking that for the class that I am storing in the map, I could make comparison functions that compare the specific member, and then use the compare function in the map.
But I still want to know how to make custom map comparison functions and what mine should look like if I want it to sort from least to greatest using comparison functions of the stored class.
The fact that map is sorted by it's keys is why you can access elements by them so quickly. Also, note that, unlike vectors, maps do not preserve the order in which they were pushed.
I suggest that you simply use vectors. And then write a function like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
for(vector<myclass>::iterator i = myvector.begin(); i != myvector.end();i++){
if(i->member == something){
//if there was only one element that needed to be deleted
myvector.erase(i);
break;
//if there are more
i = myvector.erase(i);
//now i points to the next element
//if i already was the last element you'll get an error
//so you have to handle that case with
if(i == myvector.end()) break;
//or let the first line handle it for you with
i--;
continue;
}
}