My C++11 is too weak to find a solution. I have lot of std::vector<std::pair<const char *, int>>
variables in my project and therefore the code to check if an entry does exist repeats:
std::vector<std::pair<const char *, RunningProgramMode>> vProgramMode =
{
{ "server", RunningProgramModeServer },
{ "shell", RunningProgramModeShell },
{ "client", RunningProgramModeClient },
};
// the following code repeats for each variable
for ( auto const &it : vProgramMode )
{
if ( !strcmp(sParameter, it.first) )
{
programParameters->requestedProgramMode = it.second;
}
}
Of course I can write a function [which receives std::vector<std::pair<..>>
as parameter] which iterates through the vector but I think it would be more elegant when I can extend the std::vector
template with my find_member()
function which checks with !strcmp(sParameter, it.first)
if the vector has the requested entry and returns then .second
value.
Something like this:
std::my_vector<std::pair<const char *, RunningProgramMode>> vProgramMode =
{
{ "server", RunningProgramModeServer },
{ "shell", RunningProgramModeShell },
{ "client", RunningProgramModeClient },
};
result = vProgramMode.find_member("shell");
For the moment there is no need to check if the value does exist. I want to keep the example simple and focus on the problem.