I was solving a programming problem that involved a set of all p^2 + q^2 such that p and q are less than M. In order to solve the problem, I decided to create a sorted vector. However, the following code doesn't seem to work. It seems to be giving an incompatible vector iterators error at the
if (iter == bisquares.end()) {
The code is given below.
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ifstream fin("ariprog.in");
ofstream fout("ariprog.out");
int N, M;
fin >> N >> M;
vector<int> bisquares;
bisquares.push_back(0);
for (int i = 0; i <= M; i++) {
for (int j = 0; j <= M; j++) {
int bisquare = i * i + j * j;
vector<int>::iterator iter = bisquares.begin();
while (iter != bisquares.end()) {
if (bisquare < *iter) {
iter++;
bisquares.insert(iter, bisquare);
break;
}
if (bisquare == *iter) {
break;
}
iter++;
}
if (iter == bisquares.end()) {
bisquares.push_back(bisquare);
}
}
}
// Print Elements
for (int i = 0; i < bisquares.size(); i++) {
cout << bisquares[i] << " ";
if (i % 10 == 9) {
cout << endl;
}
}
fin.close();
fout.close();
return 0;
}
The file ariprog.in contains
3
5
I'm not exactly sure what's going on. Any help is appreciated.