SETS ARE MADE TO STORE NUMBERS IN SORTED ORDER. BUT WHEN I TRIED STORING THE VALUE IN SET, IT IS NOT STORING IN SORTED ORDER. ANY POSSIBLE REASONS WHY THIS IS HAPPENING?

#include<bits/stdc++.h>
using namespace std;

int main()
{
int a[] = {1,3,4,5,6};
int b[] = {25,869,78,5,7};
set B;
map<int, int> A;
for(int i=0;i<5;i++)
A.insert(pair<int,int>(a[i],b[i]));
for(map<int,int>::iterator i = A.begin();i!=A.end();i++)
B.insert(i->second);
for(int i=0;i<B.size();i++)
cout<<b[i]<<" ";
}

You’re outputting your initial array, not the set
To output the set, use

for(int x : B){
       cout<<x<<" ";
}

Sets don’t have indexes.

1 Like

Please understand the difference b/w title and description of your problem.

5 Likes

thanks