It sounds like you need/want either an std::set<std::string>
or an std::unordered_set<std::string>
.
When you receive an item, attempt to insert it into the [unordered_]set. Check the return value to see if that succeeded or not.
Note that searching for the item first, then attempting to insert if it's not present is fairly wasteful. You normally just want to attempt to insert it, and then check the return value to see if that succeeded:
class whatever {
std::set<std::string> strings;
public:
bool itemseen(std::string const &input) {
return !strings.insert(input).second;
}
};
If you do a search and then insert, you're forcing it to search the collection twice when/if it inserts a new object. Using the return value instead allows you to do the search only once. IOW, you can expect it to be roughly twice as fast (though caching is likely to make the second search faster, so the measured difference may be smaller than that).