I have the following structure:
struct User
{
string name;
bool flag;
int score;
bool operator==(const User& user) const
{
return name == user.name;
}
}
namespace std {
template<>
struct hash<User>
{
size_t operator()(const User& user) const
{
return hash<string>{}(user.m_nickname);
}
};
}
After that I want to use User with unordered_set container, but after reading another portion of data I want to modify user score\flag etc, therefore it wont affect the hash value, because I know that all users will have unique names beforehand.
In my program I use something like that:
unordered_set<User> set;
while(true)
{
User user = *set.find({name});
user.score++;
}
But after my user object goes out of scope and I find it one more time values remain the same as they were before modifying. How I can solve this problem and maybe I should use other container? I have chosen this one because of O(1) complexity.
unordered_set time was better than set (15494846) : 93<155. Only with adding this line: s. max_load_factor(0.25);s. reserve(500); .
Keys are immutable, therefore, the elements in an unordered_set cannot be modified once in the container - However, they can be inserted and removed. Unordered sets do not allow duplicates and are initialized using comma-delimited values enclosed in curly braces.
Sets vs Unordered SetsSet is an ordered sequence of unique keys whereas unordered_set is a set in which key can be stored in any order, so unordered. Set is implemented as a balanced tree structure that is why it is possible to maintain order between the elements (by specific tree traversal).
We can use it to hash integers, floats, pointers, strings, arrays, pairs, and standard containers. That's all about using pair as a key in std::unordered_set in C++.
The element’s value cannot be changed after it has been added to the set, but the element’s modified value can be removed and added again. A hash table is used to implement an unordered set, with keys hashed into hash table indices to ensure that insertion is always random.
Unordered set is an associative container that contains a set of unique objects of type Key. Search, insertion, and removal have average constant-time complexity. Internally, the elements are not sorted in any particular order, but organized into buckets. Which bucket an element is placed into depends entirely on the hash of its value.
Sets vs Unordered Sets. Set is an ordered sequence of unique keys whereas unordered_set is a set in which key can be stored in any order, so unordered.
In an unordered_set<> each object's value is its key. Hence, the objects can't change. One way you can get around this is by making the non-key members mutable. Also, you're modifying a copy. Show activity on this post. Use Map, unordered_map, where the key is the name and the value is User.
In an unordered_set<>
each object's value is its key. Hence, the objects can't change.
One way you can get around this is by making the non-key members mutable.
That is, change
bool flag;
int score;
to
mutable bool flag;
mutable int score;
Also, you're modifying a copy.
Change
User user = *set.find({name});
to
auto &user = *set.find({name});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With