In article <fv**********@solaris.cc.vt.edu>,
br**@16systems.com says...
New to C++. Just ran into a need to store three things in a map. Before
trying it, I thought I'd post here and ask for advice. Here is an
example of what I need to store:
map<string name, string prefix, int length>
Both name and prefix would be unique for each map entry. I was wondering
if the the first entry could be an array that holds the two items (name
and prefix), but then I wondered how an iterator would behave when
it->first is called.
It can't be an array -- it has to be copyable and assignable. It can be
a pair<string, stringthough, in which case you'd do something like
it->first.first or it->first.second, to access the name and prefix
respectively.
Of course, you could also create a class of your own to hold the two
strings, something like:
class key {
string name;
string prefix;
public:
key(string n, string p) : name(n), prefix(p) {}
key(key const &other) : name(other.name), prefix(other.prefix) {}
key &operator=(key const &other) {
name = other.name;
prefix = other.prefix;
return *this;
}
bool operator<(key const &other) const {
if (name < other.name)
return true;
if (name other.name)
return false;
if (prefix < other.prefix)
return true;
return false;
}
};
--
Later,
Jerry.
The universe is a figment of its own imagination.