【发布时间】:2018-05-26 23:00:47
【问题描述】:
在下一个代码中我可以输入一个数字 n 然后我可以输入 n 个 数字>(x,y) 坐标。
代码:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n = 0;
char cr;
class punto{
private:
int x;
int y;
public:
punto();
~punto(){}
void setx(int xn){ x = xn;}
void sety(int yn) { y = yn; }
void printcoord();
};
punto::punto()
{
x=0;
y=0;
}
void punto:: printcoord()
{
cout << x << " " << y << endl;
}
int main()
{
vector<punto> list;
int x;
int y;
punto *p1;
cin >> n >> cr;
for(int contador=0; contador<n; contador++){
if(contador<n){
cin >> x;
cin >> y;
p1=new punto;
p1->setx(x);
p1->sety(y);
list.push_back(*p1);
cin.get();
}
}
vector<punto>::iterator it;
if(cr=='x'){
for ( it = list.begin(); it != list.end(); ++it ) {
it->printcoord();
}
}
if(cr=='y'){
for ( it = list.begin(); it != list.end(); ++it ) {
it->printcoord();
}
}
return 0;
}
这意味着如果我们输入这个:
5 x
1 2
3 4
6 1
2 2
1 1
我们得到这个输出。
1 2
3 4
6 1
2 2
1 1
问题是我不知道如何对 x 或 y 的坐标进行排序。
坐标存储为向量中的对象。
如果我尝试使用 sort 它会说 x 和 y 是非类类型 int。
输出应该是:
1 1 1 1
1 2 6 1
2 2 for x 1 2 for y
3 4 2 2
6 1 3 4
我将不胜感激。
【问题讨论】:
标签: c++ sorting c++11 object stdvector