Set c++ stl question


Please explain this question

#include <bits/stdc++.h>
using namespace std;
int main()
{
sets;
pair<set::iterator, bool> ret;
s.insert(1);
s.insert(5);
s.insert(10);
s.insert(3);
ret = s.insert(10);
cout<<ret.second<<endl;
return 0;
}

Hey which question are you talking about its a 3 hour webinar.

#include <bits/stdc++.h>

using namespace std;
int main() {
    set<bool> s;        // You declaration of set was wrong
    pair < set::iterator, bool > ret;
    s.insert(1);
    s.insert(5);
    s.insert(10);
    s.insert(3);
    ret = s.insert(10);
    cout << ret.second << endl;
    return 0;
}

This code is meant to show that the insert function in the set returns a pair that contains set:iterator and the value bool. The insert function is used to add new elements to the set. We have just depicted that the return value of set is a pair of set:iterator pointing to the element and value of element we just inserted.

For more info about set::insert function. You can check http://www.cplusplus.com/reference/set/set/insert/

2 Likes

Sir this question was in c++ stl quiz, but in quiz section we don’t have the facility to ask the doubts so I asked it using webinar section of stl.

As I already explained you s.insert() returns a pair of set:iterator and its value. So ret.second will have the value 10 acc. to your question.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.