In: Computer Science
Hello, for c++, please expert tell me how to use unordered set to do the rest method:insert, remove, find in container, declaration, visiting all elements.
1 . >
Inserting elements into unordered_Set :
CONCEPT : used a function st.insert() function of unordered_set<int>st name st.
C++ code to
show how to insert element into
unordered_set
NOTE: I have inserted {9,2,8,5,2 }
these elements into unordered_set set but our set will only
contains only unique elements. In these elements 2 is repeated so
set will contains 2 only 1 times.
so finally unordered_set contains = {9,2,8,5} these
elements.
2.>
Removal of particular elements from unordered_Set :
CONCEPT : used st.find(x) , this function will find the position of element x from set and give iterator pointing to x and once we find the iterator we will use st.erase(it) to remove that element from set.
C++ code to show how to remove element from unordered_set
3.>
st.find(x) to find element in unordered_set , below is the explained code with output
test case1: please enter the number you want to search in unordered_set : 8 Output : 8 is not found is unordered_set |
test case2: please enter the number you want to search in unordered_set : 3 Output : 3 is present in unordered_set |
Time complexity :
Insertion , removal , these all takes O(1) , constant time in best and average case in unordered_Set but in worst case time compelxity may be linear time O(n) which depends on the internally used hash function,