Important Pair question #2

Hello again,

I am having problem with the following. I am trying to read data with commas from a txt file into pairs, and store the pairs into a set of pairs i.e


My code is something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

typedef pair<int, int> pairs;

int main(){
. . .
int value1, value2;
string s1,s2, line;
set<pairs> s;
while (getline(fin, line)) {
  stringstream ss(line);
  getline(ss, s1, ',');
  value1 = stoi(s1);
  getline(ss, s2, ',');
  value2 = stoi(s2);
  pairs p = make_pair(value1,value2);
  s.insert(p);
}


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?

Thank you

Last edited on
This will read and display the contents of the file - given the format above:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <fstream>
#include <iostream>
#include <utility>
#include <set>

using Pairs = std::pair<int, int>;

std::istream& operator>>(std::istream& is, Pairs& p) {
	is >> p.first;
	is.ignore();
	return is >> p.second;
}

std::ostream& operator<<(std::ostream& os, const Pairs& p)
{
	return os << '(' << p.first << ' ' << p.second << ')';
}

int main() {
	std::ifstream fin("pairs.txt");

	if (!fin)
		return (std::cout << "cannot open file\n"), 1;

	std::set<Pairs> s;

	for (Pairs p; fin >> p; s.emplace(p));

	for (const auto& p : s)
		std::cout << p << '\n';
}



(1 10)
(2 20)
(3 30)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <fstream>
#include <set>
#include <utility>
using namespace 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?
Last edited on
Possibly something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <fstream>
#include <iostream>
#include <utility>
#include <vector>
#include <limits>

using Pairs = std::pair<int, int>;

std::istream& operator>>(std::istream& is, Pairs& p) {
	is >> p.first;
	is.ignore();
	return is >> p.second;
}

std::ostream& operator<<(std::ostream& os, const Pairs& p)
{
	return os << '(' << p.first << ' ' << p.second << ')';
}

bool operator<(const Pairs& p1, const Pairs& p2)
{
	return p1.second < p2.second;
}

auto operator-(const Pairs& p1, const Pairs& p2) {
	return Pairs {p1.first - p2.first, p1.second - p2.second};
}

int main() {
	std::ifstream fin("pairs.txt");

	if (!fin)
		return (std::cout << "cannot open file\n"), 1;

	std::vector<Pairs> s;

	Pairs lowest {std::numeric_limits<int>::max(), std::numeric_limits<int>::max()};

	for (Pairs p; fin >> p; s.emplace_back(p))
		if (p < lowest)
			lowest = p;

	std::cout << "The first lowest is " << lowest << '\n';
	std::cout << "Subtracted from (3 30) is " << Pairs {3, 30} - lowest << '\n';
}



The first lowest is (1 10)
Subtracted from (3 30) is (2 20)

Topic archived. No new replies allowed.