Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify value in unordered_set

Tags:

c++

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.

like image 310
Sam Avatar asked Feb 05 '18 00:02

Sam


People also ask

Is unordered_set faster than set?

unordered_set time was better than set (15494846) : 93<155. Only with adding this line: s. max_load_factor(0.25);s. reserve(500); .

Can unordered_set have duplicate values?

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.

What is the difference between unordered_set and set?

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).

Can we use pair in unordered_set?

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++.

Can you change the value of an element in an unordered set?

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.

What is unordered set in Java?

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.

What is the difference between set and unordered set in Python?

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.

How to change the key of an unordered_set?

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.


1 Answers

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});
like image 115
Sid S Avatar answered Oct 03 '22 19:10

Sid S