TransWikia.com

Using set in c++ standard template library (STL)

Stack Overflow Asked by Tushar Jain on December 9, 2021

Sets are the collection. There is a function in set isinsert().

It’s return type : it returns the iterator pointing to the inserted element in the collection.

I write a code and its working :

#include<bits/stdc++.h>
using namespace std;
int main(){

set<int> s;

s.insert (1);

s.insert (4);

s.insert (2);

s.insert (5);

s.insert (3);

cout << "The elements in set are: ";

for (auto it = s.begin(); it!= s.end (); it++)

cout << *it;

return 0;
}

I write another code and it also working:

#include<bits/stdc++.h>
using namespace std;
int main(){
 
set<int> s;

auto itr=s.insert (s.begin(),5);

itr=s.insert (itr,4);

itr=s.insert (itr,2);

cout << "The elements in set are: ";

for (auto it = s.begin(); it!= s.end (); it++)

cout << *it;

return 0;
}

Now i merge the logic of both my previous code but now its not working why? :

#include<bits/stdc++.h>
using namespace std;
int main(){

set<int> s;

auto itr=s.insert (1);

s.insert (itr,4);


cout << "The elements in set are: ";

for (auto it = s.begin(); it!= s.end (); it++)

cout << *it;

return 0;
}

Output in above code :


 
/usr/include/c++/7/bits/stl_set.h:536:7: note: candidate: std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::insert(std::set<_Key, _Compare, _Alloc>::const_iterator, const value_type&) [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>; std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<int>; std::set<_Key, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<int>; std::set<_Key, _Compare, _Alloc>::value_type = int]
       insert(const_iterator __position, const value_type& __x)
       ^~~~~~
/usr/include/c++/7/bits/stl_set.h:536:7: note:   no known conversion for argument 1 from ‘std::pair<std::_Rb_tree_const_iterator<int>, bool>’ to ‘std::set<int>::const_iterator {aka std::_Rb_tree_const_iterator<int>}’
/usr/include/c++/7/bits/stl_set.h:541:7: note: candidate: std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::insert(std::set<_Key, _Compare, _Alloc>::const_iterator, std::set<_Key, _Compare, _Alloc>::value_type&&) [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>; std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<int>; std::set<_Key, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<int>; std::set<_Key, _Compare, _Alloc>::value_type = int]
       insert(const_iterator __position, value_type&& __x)
       ^~~~~~
/usr/include/c++/7/bits/stl_set.h:541:7: note:   no known conversion for argument 1 from ‘std::pair<std::_Rb_tree_const_iterator<int>, bool>’ to ‘std::set<int>::const_iterator {aka std::_Rb_tree_const_iterator<int>}’
/usr/include/c++/7/bits/stl_set.h:556:2: note: candidate: template<class _InputIterator> void std::set<_Key, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>]
  insert(_InputIterator __first, _InputIterator __last)
  ^~~~~~
/usr/include/c++/7/bits/stl_set.h:556:2: note:   template argument deduction/substitution failed:
yo.cpp:14:16: note:   deduced conflicting types for parameter ‘_InputIterator’ (‘std::pair<std::_Rb_tree_const_iterator<int>, bool>’ and ‘int’)
 s.insert (itr,4)
                ^
In file included from /usr/include/c++/7/set:61:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:87,
                 from yo.cpp:1:
/usr/include/c++/7/bits/stl_set.h:568:7: note: candidate: void std::set<_Key, _Compare, _Alloc>::insert(std::initializer_list<_Tp>) [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>]
       insert(initializer_list<value_type> __l)
       ^~~~~~
/usr/include/c++/7/bits/stl_set.h:568:7: note:   candidate expects 1 argument, 2 provided

One Answer

When you use the syntax:

auto itr = s.insert(5);

The return type of s.insert() with the parameter 5 gives a type of:

std::pair<std::set<int>::iterator, bool>

But there's no such known overloaded conversion from that type to std::set<int>::iterator in the syntax:

itr = s.insert(itr, 4); // error!
_______________^^^_____

To solve it, you need to declare itr correctly:

auto itr = s.insert(s.begin(), 5);

Rather than only:

auto itr = s.insert(5);

In the correct case, the return type of s.insert() is std::set<int>::iterator which is compatible for the next statements.

Answered by Rohan Bari on December 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP