Map sorting

May 29, 2010 at 3:02pm
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!
May 29, 2010 at 3:25pm
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.
May 29, 2010 at 4:08pm
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;
    }
}
May 29, 2010 at 4:12pm
I already had something like that, I guess I'll just go back to doing that and make the other stuff work later.
May 29, 2010 at 4:49pm
Need to see example , maps have weak strict order , so this is what i came up with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

class sort_map
{
  public:
	string key;
	int val;
};

bool Sort_by(const sort_map& a ,const sort_map& b)
{
	return a.val < b.val;
}
	
int main()
{
	map<string,int> d;
	map<string,int>::iterator it;
	vector< sort_map > v;
	vector< sort_map >::iterator itv;
	sort_map sm;
	
	d["a"] = 5; d["b"] = 3; d["c"] = 9; d["d"] = 7; d["e"] = 1; d["f"] = 13; d["g"] = 11;
	
	for (it = d.begin(); it != d.end(); ++it)
	{
		sm.key = (*it).first; sm.val = (*it).second;
		v.push_back(sm);
	}
	for (itv = v.begin(); itv != v.end(); ++itv)
	{
		cout << (*itv).key << " : " << (*itv).val << endl;
	}
	
	sort(v.begin(),v.end(),Sort_by);
	
	cout << "sorted" << endl;
	for (itv = v.begin(); itv != v.end(); ++itv)
	{
		cout << (*itv).key << " : " << (*itv).val << endl;
	}
	
	return 0;
}
Topic archived. No new replies allowed.