is this a correct code or will this code constantly overwrite the same p as long as the loop proceeds and therefore I will end up taking multiple times the same value?
#include <iostream>
#include <fstream>
#include <set>
#include <utility>
usingnamespace std;
int main()
{
set< pair<int,int> > S;
ifstream in( "input.txt" );
int a, b;
char comma;
while ( in >> a >> comma >> b ) S.insert( { a, b } );
for ( auto e : S ) cout << e.first << " " << e.second << '\n';
}
@seeplus: Thank you very much Seeplus. One more question. Suppose I have the above three pairs in the set (I use vector here instead of a set), namely
(1 10)
(2 20)
(3 30)
I would like to spot the pair in the vector with the minimum second, which is the (1,10) pair. I have done it like below:
1 2 3 4 5 6 7 8 9
int saveIndex1 = 0;
for (int i = 1; i < v.size(); i++)
{
if (v[saveIndex1].second > v[i].second)
{
saveIndex1 = i;
}
}
int smallestvalue = v[saveIndex1].second;
, but, I would like to get the first of this minimum second pair because I would like to subtract it from another first of another pair of the vector (i.e 3-1, where 3 is the first of the 3,30 pair and 1 is the first of the 1,10 pair which is the minimum pair). Any ideas of how could I do this?