27

First of all, what is the main difference between them?

The only thing i've found is that unordered_set has no operator []. How should i access an element in unordered_set, since there is no []?

Which container is using random access to memory(or both)?

And which one of them faster in any sense or using less memory?

2
  • The're fullfilling different purposes, one is a map, one is a set. If you need a map, use the map. If you need a set, use the set. performance and memory are not the relevant differences. Commented Mar 9, 2019 at 1:15
  • Just to add up what 1201ProgramAlarm said, elements in unordered_set are also keys. So the values that user provides are elements and keys at the same time. Commented Aug 24, 2020 at 22:39

3 Answers 3

36

They are nearly identical. unordered_set only contains keys, and no values. There is no mapping from a key to a value, so no need for an operator[]. unordered_map maps a key to a value.

You can use the various find methods within unordered_set to locate things.

2
  • Why they are not created like std::queue and std::deque. I mean that queue is adapter for deque. Why unordered_set is not adapter of std::unordered map? Commented Sep 25, 2018 at 5:02
  • 3
    @GusevSlava: sets require the items to be immutable, and maps have mutable keys. Due to that, neither can be implemented by the other without wasting memory. Commented Mar 9, 2019 at 1:16
3

How should I access an element in unordered_set (C++17)?

In C++ 17 a new function extract is added to unordered_set. Specially, this is the only way to take move only object out of the set.

https://en.cppreference.com/w/cpp/container/unordered_set/extract

For example if you want third element of your unordered set. Advance the iterator

std::advance(it,2);

Then extarct the value

s.extract(it).value();

Here is the complete code. try on any C++17 compiler.

#include <iostream>
#include <string>
#include <unordered_set>
#include <iterator>

int main()
{
    //CREATE AN OBJECT
    std::unordered_set<std::string> s;

    //INSERT DATA
    s.insert("aee");
    s.insert("bee");
    s.insert("cee");
    s.insert("dee");

    //NEED TO INCLUDE "iterator" HEADER TO USE "std::advance"
    auto it = s.begin();
    std::advance(it,2);

    //USING EXTRACT
    std::string sval = s.extract(it).value();
    std::cout<<sval;
}

Note: if queried for out of bound index, nothing happens. No result. Try changing your code

 //ONLY FOUR ELEMENTS 
 std::advance(it,8);    
 //USING EXTRACT
 std::string sval = s.extract(it).value();
1
  • The insert data part can be done more concisely by using an initializer list (std::unordered_set<std::string> s{"aee", "bee", "cee", "dee"};). Also std::next(it, 2).
    – L. F.
    Commented Mar 30, 2020 at 5:59
2

you can use iterators to access elements.

unordered_set <string> u{
            "Dog",
            "Cat",
            "Rat",
            "Parrot",
            "bee"
};

for(auto& s:u){
     cout << s << ' ';    
} 

unordered_set<string>::const_iterator point = u.find("bee");

Not the answer you're looking for? Browse other questions tagged or ask your own question.