私はいくつかの頂点オブジェクトインスタンスでいっぱいになっているベクトルを持っていて、 'x'とそれに続く 'y'座標に従ってソートする必要があります。オブジェクトのベクトルを並べ替える
私は出力として期待
#ifndef VERTEX_H
#define VERTEX_H 1
class Vertex
{
private:
double __x;
double __y;
public:
Vertex(const double x, const double y);
bool operator<(const Vertex &b) const;
double x(void);
double y(void);
};
#endif // VERTEX_H
vertex.cpp
#include "vertex.h"
Vertex::Vertex(const double x, const double y) : __x(x), __y(y)
{
}
bool Vertex::operator<(const Vertex &b) const
{
return __x < b.x() || (__x == b.x() && __y < b.y());
}
double Vertex::x(void)
{
return __x;
}
double Vertex::y(void)
{
return __y;
}
run.cpp
#include <algorithm>
#include <stdio.h>
#include <vector>
#include "vertex.h"
void prnt(std::vector<Vertex *> list)
{
for(size_t i = 0; i < list.size(); i++)
printf("Vertex (x: %.2lf y: %.2lf)\n", list[i]->x(), list[i]->y());
}
int main(int argc, char **argv)
{
std::vector<Vertex *> list;
list.push_back(new Vertex(0, 0));
list.push_back(new Vertex(-3, 0.3));
list.push_back(new Vertex(-3, -0.1));
list.push_back(new Vertex(3.3, 0));
printf("Original:\n");
prnt(list);
printf("Sorted:\n");
std::sort(list.begin(), list.end());
prnt(list);
return 0;
}
があるvertex.h:
Original:
Vertex (x: 0.00 y: 0.00)
Vertex (x: -3.00 y: 0.30)
Vertex (x: -3.00 y: -0.10)
Vertex (x: 3.30 y: 0.00)
Sorted:
Vertex (x: -3.00 y: -0.10)
Vertex (x: -3.00 y: 0.30)
Vertex (x: 0.00 y: 0.00)
Vertex (x: 3.30 y: 0.00)
しかし、私は実際に取得することです:
Original:
Vertex (x: 0.00 y: 0.00)
Vertex (x: -3.00 y: 0.30)
Vertex (x: -3.00 y: -0.10)
Vertex (x: 3.30 y: 0.00)
Sorted:
Vertex (x: 0.00 y: 0.00)
Vertex (x: -3.00 y: -0.10)
Vertex (x: -3.00 y: 0.30)
Vertex (x: 3.30 y: 0.00)
私は正確に間違って起こっているかわからない、任意のアイデア?
どうすればコンパイルできますか?あなたの演算子はconst&Vertexをとりますが、 'double Vertex :: x()const'はありません。 – Chris
これを読んでください:http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-ac-identifier/228797#228797 –
StackOverflowへようこそ私たちが通常行うここでの3つのことを思い出させてください:1)あなたが助けを受けても、あなたの専門分野で**質問に答えるようにしてください** [[FAQを読む]](http://tinyurl.com/2vycnvr )3)良いQ&Aを見たら、[灰色の三角形を使って]投票してください(http://i.imgur.com/kygEP。png)。システムの信頼性は、ユーザーが知識を共有して得た評判に基づいているためです。また、あなたの問題をより良く解決する答えを受け入れることを覚えておいてください。(「チェックマークの印を押して」) –