我使用 Visual Studio 2010。
struct SPoint
{
int id;
int X;
int Y;
};
////////////////
vector<SPoint> points;
vector<SPoint> chosen;
////////////////
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;
}
////////////////
print_vect(points);
编译器显示:
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(4674): error C2784: 'bool std::operator <(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'SPoint'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\iterator(371) : see declaration of 'std::operator <'
我在一个单独的项目中对此进行了建模:
struct SPoint
{
int X;
int Y;
};
vector<SPoint> points;
vector<SPoint> selected;
void print_vector(const vector<SPoint> & points) {
for (int i = 0; i < points.size(); i++)
{
cout << '('<<points[i].X <<',' <<points[i].Y <<')'<< endl;
}
cout << endl;
}
int main ()
{
SPoint temp = {0, 0};
for (int i = 0; i < 11;i++)
{
temp.X = i;
temp.Y = i;
points.push_back(temp);
}
for (int i = 5; i< 11;i++)
{
temp.X = i;
temp.Y = i;
selected.push_back(temp);
}
print_vector(points);
system ("pause" );
return 0;
}
它完美地工作。我试图在这个问题上找到一些东西。他们说,编译器无法比较两个对象。将“<”方法添加到您的课程中。但到目前为止,我正在学习过程编程。然后,我的试用示例有效。为什么和做什么?