So I was implementing a TCP-server in c++ and was storing all the users in a vector. Now I need a more generalized function to search for multiple different properties, How do I improve this code?
struct client {
std::string ip_address = "";
int socket_id = 0;
bool blocking = false;
};
enum client_codes {
ip_address,
socket_id,
blocking,
};
template<typename T>
std::vector<client>::iterator search_vector(std::vector<client> &list, int type, T query) {
std::vector<std::function<bool(client)>> comparators;
comparators.push_back([&](client ob) {return ob.ip_address == std::to_string(query); });
comparators.push_back([&](client ob) {return ob.socket_id == query; });
comparators.push_back([&](client ob) {return ob.blocking == query; });
return std::find_if(std::begin(list), std::end(list), [&](client obj) {return comparators[type](obj); });
}
Aucun commentaire:
Enregistrer un commentaire