Re: Using own class as key in map causes segmentation fault
* nw:
Hi,
I'm trying to use my own class as a map key. However something strange
seems to be happening to the map which causes my program to segfault.
I'm at a loss as to what this might be. Can anyone help?
The complete example below segfaults for me under g++ 4.1.2. Using a
debugger it seems that one of the MultiDimKey objects the map is
holding becomes corrupt.
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
class MultiDimKey {
public:
MultiDimKey() : keys() {
}
MultiDimKey(const vector<string> keys_in) : keys(keys_in) {
}
bool operator<(const MultiDimKey &other) const {
const vector<string> other_keys = other.keys;
for(size_t n=0;n<keys.size();n++) {
if(keys[n] < other_keys[n]) return true;
}
By this criterion ["3","6"] < ["5","3"] and ["5","3"] < ["3","6"].
std::map will have a really hard time ordering those keys...
return false;
}
vector<string> keys;
};
Cheers & hth.,
- Alf