unordered_set<int> s( argument1, argument2 );

I'm trying to find more documentation related to this code

unordered_set<int> s(nums.begin(),nums.end());

but the books i'v been studying, such as
Programming Principles and Practice Using C++ 2nd Edition by Bjarne Stroustrup
C++ Primer

doesn't have much information on unordered_set

i'v tried checking
https://www.learncpp.com/cpp-tutorial/stl-containers-overview/
https://en.cppreference.com/w/cpp/container/unordered_set

but can't find the info i need.


could someone help point me to good resources with details to learn more about this topic?


as of now, i'm trying to understand this line of code specifically...

unordered_set<int> s(nums.begin(),nums.end());


i know what nums.begin() and nums.end() do
but i'm having problem learning more about the syntax

unordered_set<int> s( argument1, argument2 );

and what that does...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <unordered_set>
#include <vector>

using std::cout;
using std::cin;
using std::endl;
using std::unordered_set;
using std::vector;


class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        unordered_set<int> s(nums.begin(),nums.end());
        return !(s.size()==nums.size());
    }
};



int main(){
    vector<int> nums={1,3,5,7,9,5};
 //   int k= 7;

    Solution test;
    if(test.containsDuplicate(nums))
        cout << "true" << endl;
    else
        cout << "false" << endl;

    return 0;
}
https://en.cppreference.com/w/cpp/container/unordered_set/unordered_set
First you find the constructor which matches the use.
1
2
3
4
5
6
template< class InputIt >
unordered_set( InputIt first, InputIt last,
               size_type bucket_count = /*implementation-defined*/,
               const Hash& hash = Hash(),
               const key_equal& equal = key_equal(),
               const Allocator& alloc = Allocator() );


Basically, you're copying the vector elements into the set.

To understand what that means, read https://en.cppreference.com/w/cpp/container/unordered_set

But the premise is that sets can't contain duplicate elements.
s will contain the unique elements added from nums. If the size of s is the same as nums then there were no duplicates.

The specific syntax at L15 is to create the container using start/end iterators from the start iterator (nums.begin() ) to the end iterator (nums.end() ).

Topic archived. No new replies allowed.