Let's say I have a struct defined as follows:
struct Record
{
string a;
string b;
string c;
};
I also have a vector<Record>
containing some data. I am writing a function which will take a Record
and a vector<Record>
as input, and I have to return all entries in the vector
which match with the Record
.
However, in the Record
that will be passed as a parameter to the function, some of the entries will be initialised to ""
. This means that those entries should not be considered while comparing with entries in the vector
.
I have currently written the function as follows:
vector<Record> SearchQuery (vector<Record> data, Record tosearch)
{
if (tosearch.a != "")
{
for (int i = 0; i < data.size(); i++)
{
if (data[i].entryno != tosearch.a)
{
data.erase(data.begin() + i);
i--;
}
}
}
.
. // I can duplicate the above part for entry b and c as well
}
However, this doesn't seem like a good manner to write the code, as there will be a lot of redundancy. Also if Record
has a lot of member variables, then the code will be huge. My question is that is there a better way to fulfill the requirement I have mentioned above?
I have looked at accessing member variables through macros, but it seems that won't work out. Accessing member variables through an index for the ith variable in a struct also seems problematic. Thanks.