我有两个向量。“点”是我原来的点数组。“Chosen”是要从“Points”中删除的点的集合。我想从“选择”中获取唯一的点 ID,将它们分配给迭代器并删除这些点。但不知何故我做不到。
其次,在我研究的示例中,我无法理解迭代器如何链接到确定的向量。希望在您的帮助下,我会理解迭代器。
#include <StdAfx.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
struct SPoint
{
int id;
int X;
int Y;
};
vector<SPoint> points;
vector<SPoint> chosen;
vector<SPoint> cleared;
vector<SPoint>::iterator it;
void print_vect(const vector<SPoint> & vect)
{
for (int i = 0; i < vect.size(); ++i)
{
cout << vect[i].id << " (" << vect[i].X << "," << vect[i].Y << ")"<<endl;
}
cout << endl;
}
bool compare(double val1, double val2)
{
return val1 > val2;
}
void sort_points(vector<SPoint> & vect, char command)
{
bool cmp_result;
SPoint temp;
bool sorted=true;
for (int i = 0; i < vect.size()-1 ; i++)
{
sorted=true;
for (int j = 1; j <= vect.size()-1; j++)
{
switch (command)
{
case 'x':
{
cmp_result = compare(vect[j-1].X, vect[j].X);
break;
}
case 'y':
{
cmp_result = compare(vect[j-1].Y, vect[j].Y);
break;
}
case 'i':
{
cmp_result = compare(vect[j-1].id, vect[j].id);
break;
}
}
if (cmp_result)
{
sorted = false;
temp = vect[j-1];
vect[j-1] = vect[j];
vect[j] = temp;
}
}
if (sorted)
{
cout << "Sorted:" << endl;
print_vect(vect);
break;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
SPoint temp;
for (int i = 0; i < 10; i++)
{
temp.id = i;
temp.X = i;
temp.Y = i;
points.push_back(temp);
}
for (int i = 5; i < 10; i++)
{
temp.id = i;
temp.X = i;
temp.Y = i;
chosen.push_back(temp);
}
cout << "Points:" << endl;
print_vect(points);
cout << endl << endl;
cout << "Chosen:" << endl;
print_vect(chosen);
system("pause");
vector<SPoint>::iterator it;
for (int i = 0;i < chosen.size(); i++)
{
//points.erase(it);
}
print_vect(points);
system("pause");
print_vect(cleared);
system("pause");
return 0;
}