Iterator Not Iterating

I have a list<pair<int, pair<int, int> > > named farms, and likewise I have a list<pair<int, pair<int, int> > >::iterator named it.

I store some things in there and them I try to print them out, like this.

1
2
3
4
for(it = farms.begin(); it != farms.end(); it++);
    {
        cout << (*it).first << ' ' << (*it).second.first << '|' << (*it).second.second << endl;
    }


However this is the output I get:

691 299|10555588

Which I noticed is what I have in origin; except the 105555888.

pair<int, int> origin = make_pair(691, 299);

What is going on here?

Full code below.

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <utility>
#include <string>
#include <list>

using namespace std;

int main(int argc, char* args[])
{
    string in_filename = "farms.txt";
    string out_filemane = "farms_sorted.txt";

    pair<int, int> origin = make_pair(691, 299);
    list<pair<int, pair<int, int> > > farms;
    list<pair<int, pair<int, int> > >::iterator it, it2;

    ifstream in_file(in_filename.c_str(), ios::in);

    if(!in_file.good())
    {
        cout << "Error while opening file!";
        return 1;
    }

    while(in_file.good())
    {
        char n = in_file.get();
        char temp_1[4] = "";
        int temp_2 = 0;
        int temp_3 = 0;
        int x = 0;
        int y = 0;

        while(in_file.good() && n != '(')
        {
            in_file.get(n);
        }

        if(!in_file.good()) break;

        in_file.get(temp_1, 4, '|');
        x = atoi(temp_1);

        // Moving the get pointer ahead one.
        in_file.get();

        in_file.get(temp_1, 4, ')');
        y = atoi(temp_1);

        temp_2 = origin.first - x;
        temp_3 = origin.second - y;

        temp_2 *= temp_2;
        temp_3 *= temp_3;

        temp_2 = temp_2 + temp_3;

        farms.push_back(make_pair(temp_2, make_pair(x, y)));

        //cout << temp_2 << ' ' << x << '|' << y << endl;
    }

    //cout << endl;

    for(it = farms.begin(); it != farms.end(); it++);
    {
        cout << (*it).first << ' ' << (*it).second.first << '|' << (*it).second.second << endl;
    }

    cout << endl;

    it2 = farms.begin();
    it2++;

    for(it = farms.begin(); it2 != farms.end(); it++)
    {
        while((*it).first > (*it2).first)
        {
            cout << (*it).first << " " << (*it2).first << endl;
            farms.insert(it, make_pair((*it2).first, make_pair((*it2).second.first, (*it2).second.second)));
            cout << (*it).first << " " << (*it2).first << endl;
            it2 = farms.erase(it2);
            cout << (*it).first << " " << (*it2).first << endl;

            if(it == farms.begin()) break;

            it--;
            it2--;
        }

        it2++;

        if(it2 == farms.end()) break;
    }

    cout << endl;

    for(it = farms.begin(); it != farms.end(); it++);
    {
        cout << (*it).first << ' ' << (*it).second.first << '|' << (*it).second.second << endl;
    }

    return 0;
}
Last edited on
I have two ; that I shouldn't have in there.
Topic archived. No new replies allowed.