OK here's my attempt to solve the problem. In non technical terms, I want to:
* maintain a sorted list of options, which must always remain unique (according to a certain predicate)
* insert, and find options in this sorted list
From what I understood googling around, it seems that std::set is more appropriate than std::vector for that. Here is my attempt:
Code: Select all
#include <iostream>
#include <string>
#include <set>
struct Option {
std::string name;
enum Type {Boolean, Integer};
Type type;
int value, min, max;
bool operator < (const Option& o) const
{ return type < o.type && name < o.name; }
};
std::set<Option> options;
int main()
{
// define a few options
Option o1 = {"Hash", Option::Boolean, true, false, true};
Option o2 = {"Hash", Option::Integer, 32, 1, 8192};
Option o3 = {"Verbose", Option::Boolean, true, false, true};
// insert them in the set
options.insert(o1);
options.insert(o2);
options.insert(o3);
// search for this one
Option o = {"Hash", Option::Integer, 0, 0, 0};
auto i = options.find(o);
// display the found option
std::cout << i->name << '\t'
<< i->min << '\t'
<< i->max << '\t'
<< (i->type == Option::Boolean ? "bool" : "integer")
<< std::endl;
}
And the output is:
while I would expect to find the Integer Hash option instead. Could anyone help me fix this code ?
Thank you
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.