Can I store a set iterator in another set for later use?

Oct 2, 2011 at 12:13am
I have 3 sets. I am comparing values in 2 sets and storing iterators from the first 2 sets in the 3rd set. But when I try to use the iterators in the 3rd set to reference the first 2 sets, I get errors.
Oct 2, 2011 at 12:15am
let us see the code
Oct 2, 2011 at 12:21am
#include <string>
#include <iostream>
#include <set>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int,int> PAIR;
struct paircomp {
bool operator() (const pair <int,int>& l, const pair<int,int>& r) const {
if (l.first < r.first) return true;
if ((l.first == r.first) && (l.second < r.second)) return true;
return false;
}
};
typedef set <PAIR, paircomp> PAIRSET;
typedef PAIRSET::iterator SETIT;
typedef pair <int, SETIT> DISTPAIR;
struct distcomp {
bool operator() (const DISTPAIR& l, const DISTPAIR& r) const {
if (l.first < r.first) return true;
if ((l.first == r.first) && (l.second->first > r.second->first)) return true;
if ((l.first == r.first) && (l.second->first == r.second->first) && (l.second->second > r.second->second)) return true;
return false;
}
};
typedef set <DISTPAIR, distcomp> DISTSET;

class DistantPoints {
public:
vector <int> getKth (int, int);
};

vector <int> DistantPoints::getKth (int N, int K) {
PAIRSET unpainted;
PAIRSET painted;
for (int i = 1; i < N*N+2; i++) {
for (int j = 1; j < N*N+2; j++) {
unpainted.insert(make_pair (i, j));
}
}
SETIT pit = painted.begin();
painted.insert(pit, make_pair (1, 1));
unpainted.erase(unpainted.begin());
SETIT uit = unpainted.begin();
for (int i = 2; i < K; i++) {
cout << "looping K times\n";
DISTSET candidates;
int distance;
for (uit = unpainted.begin(); uit != unpainted.end(); ++uit) {
set <int> distances;
PAIR pt = *uit;
for (pit = painted.begin(); pit != painted.end(); ++pit) {
distances.insert (distances.begin(), (pt.first - pit->first)*(pt.first - pit->first)+(pt.second - pit->second)*(pt.second - pit->second));
}
distance = *distances.begin();
candidates.insert(candidates.begin(), make_pair(distance, uit));
}
uit = candidates.end()->second;
painted.insert(pit, *uit);
unpainted.erase(uit);
}
DISTSET candidates;
int distance;
for (uit = unpainted.begin(); uit != unpainted.end(); ++uit) {
set <int> distances;
PAIR pt = *uit;
for (pit = painted.begin(); pit != painted.end(); ++pit) {
distances.insert (distances.begin(), (pt.first - pit->first)*(pt.first - pit->first)+(pt.second - pit->second)*(pt.second - pit->second));
}
distance = *distances.begin();
candidates.insert(candidates.begin(), make_pair(distance, uit));
}
uit = candidates.end()->second;
int myFirst = uit->first; //segmentation fault here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
int mySecond = uit->second;
vector <int> out;
out.insert(out.end(), myFirst);
out.insert(out.end(), mySecond);
return out;
}
Oct 2, 2011 at 12:29am
you have no main function
Oct 2, 2011 at 12:32am
or am I blind
Oct 2, 2011 at 4:13pm
True statement. I have no main. This is a class with a single function. When called by other functions, I get a segmentation fault.

It seems to have something to do with my use of the set iterator.
Topic archived. No new replies allowed.