Et par eksempler på std::map, hvis ikke du forstår, så fortvivl ikke, men læs:
http://www.sgi.com/tech/stl/stl_index.html(eller vent indtil du ved mere om c++)
#include <iostream>
#include <map>
#include <string>
void foo(void)
{
std::cout << "foo" << std::endl;
}
void bar(void)
{
std::cout << "bar" << std::endl;
}
class Index
{
public:
Index(const char *aStr, int aNr)
: Str(aStr), Nr(aNr)
{}
std::string Str;
int Nr;
};
class Value
{
public:
Value(double aD, int aI) : D(aD), I(aI)
{}
Value() : D(0), I(0)
{}
double D;
int I;
};
bool operator < (const Index& lhs, const Index& rhs)
{
if(lhs.Str != rhs.Str)
return lhs.Str < rhs.Str;
return lhs.Nr < rhs.Nr;
}
std::ostream& operator << (std::ostream& os, const Value& aVal)
{
os << aVal.D << ", " << aVal.I;
return os;
}
int main()
{
std::map<std::string, void (*)(void)> MyMap1;
MyMap1["Ole"] = foo;
MyMap1["Peter"] = bar;
MyMap1["Ole"]();
MyMap1["Peter"]();
std::map<Index, Value> MyMap2;
MyMap2[Index("Ole", 2)] = Value(3.123, 123);
MyMap2[Index("Ole", 3)] = Value(4.123, 321);
std::cout << MyMap2[Index("Ole", 2)] << std::endl;
std::cout << MyMap2[Index("Ole", 3)] << std::endl;
std::map<int, std::map<std::string, int> > MyMap3;
MyMap3[3]["Ole"] = 123;
MyMap3[2]["Peter"] = 121;
std::cout << MyMap3[3]["Ole"] << std::endl;
std::cout << MyMap3[2]["Peter"] << std::endl;
}