The easy way is with something like this:
Code:
struct name_value_pair {
std::string name;
int value;
};
ostream & operator << (std::ostream &os, const name_value_pair &p) {
return os << p.name << " = " << p.value;
}
std::istream & operator >> (std::istream &is, name_value_pair &p) {
std::string name;
int value;
if(std::getline(is,name,'=') >> value) {
p.name.swap(name); p.value = value;
}
return is;
}
This lets you use name_value_pairs more or less like you would a simple int. Both operators will work just as well on text files or string streams or whatever decends from istream. Some things you might want to do are define a < operator so that you can have a std::set of name_value_pairs (really just a std::map the hard way). You also may want to make name private and normalize it to make comparisons case insensitive and ignore whitespace. Currently "Age=22" is different from "age = 22". Normalizing is the process of converting something that has many different, but semantically identical forms to one particular form. For example, all lower case with no leading or trailing whitespace.