Skip to content

Instantly share code, notes, and snippets.

@Const-me
Created February 3, 2024 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Const-me/56ad35a6ad3b65665dc79423a9270fde to your computer and use it in GitHub Desktop.
Save Const-me/56ad35a6ad3b65665dc79423a9270fde to your computer and use it in GitHub Desktop.
std::vector<std::string> someFunction( const Invocation& invocation )
{
// Define hash and comparison for string pointers, by value
struct StringPtrTraits
{
size_t operator()( const std::string* rsi ) const
{
return std::hash<std::string>()( *rsi );
}
bool operator()( const std::string* a, const std::string* b ) const
{
return ( *a == *b );
}
};
using TagsCollection = std::unordered_set<const std::string*, StringPtrTraits, StringPtrTraits>;
TagsCollection tags;
// Add input strings to the hash set
// This loop doesn't copy any strings, the hash set references the original pointers
for( const auto& error : invocation.errors() )
for( const std::string& s : error.tags() )
tags.insert( &s );
// And here we make copies
std::vector<std::string> result;
result.reserve( tags.size() );
for( const std::string* e : tags )
result.push_back( *e );
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment