似乎无法使用迭代器在 for 循环中打印 list<VERTEX>vertices? C++
Cant seem to print list<VERTEX>vertices within a for loop using an iterator? C++
我遇到一个问题,我无法打印列表 'vertex' 顶点中的坐标集。解释一下,'Vertex' 是一个 class,它基本上包含构成顶点的两个坐标(形状的两条线相交处)。问题出在第二个 for 循环中,该循环旨在打印出列表中的坐标。任何人都可以帮助我尝试打印出来吗?我尝试了很多方法,到目前为止,错误总是给我带来问题。
代码如下,感谢帮助!
INT 主
int main(){
//obj
Console con;
RandomNumber rand;
Vertex vertex;
//Declarations
list<Vertex>vertices;
//list<Vertex>::iterator ii;
//set Vertex coords and push into list
for (int i = 0; i <= 8; i++){
vertices.push_back(Vertex(rand.random(0, 10), rand.random(0, 10)));
}
//iterate through the list outputting a char at each vertex (coords)
for (list<Vertex>::iterator ii = vertices.begin(); ii != vertices.end(); ii++){
cout << vertices[ii];
}
system("pause");
}
顶点CLASS
#pragma once
class Vertex
{
int x;
int y;
public:
Vertex(int x = 10, int y = 10);
~Vertex();
int getX() const;
int getY() const;
void setX(unsigned x);
void setY(unsigned y);
bool operator== (const Vertex &p2) const;
};
#include "Vertex.h"
Vertex::Vertex(int x, int y)
{
this->x = x;
this->y = y;
}
Vertex::~Vertex(){}
int Vertex::getX() const {
return x;
}
int Vertex::getY() const {
return y;
}
void Vertex::setX(unsigned x) {
this->x = x;
}
void Vertex::setY(unsigned y) {
this->y = y;
}
bool Vertex::operator== (const Vertex &point) const {
return (this->x == point.getX()) && (this->y == point.getY());
}
//iterate through the list outputting a char at each vertex (coords)
for (list<Vertex>::iterator ii = vertices.begin(); ii != vertices.end(); ii++){
cout << vertices[ii];
}
这本质上是错误的。您需要将迭代器当作指向列表中第 ii 个对象的指针来使用。
for (list<Vertex>::iterator ii = vertices.begin(); ii != vertices.end(); ii++){
cout << ii->getX() << ',' << ii->getY();
// if you have an overload for 'operator <<(std::ostream&, Vertex)' you can also do
cout << *ii
}
<<
运算符不适用于自定义类型。您需要指定 <<
运算符在遇到您的 Vertex
class 时的行为方式。
您需要生成一个能够理解您的顶点的签名 class。取以下函数:
std::ostream& operator<<(std::ostream &os, const Vertex &vertex) {
os << "Node: [" << vertex.getX() << ", " << vertex.getY() << "]";
return os;
}
这将允许您使用您编写的代码。
需要给你加一个功能class:
声明:
friend std::ostream& operator <<(std::ostream &os,const Vertex &obj);
和实施:
std::ostream& operator<<( std::ostream &os,const Vertex &obj )
{
os << << obj.getX() << ',' << obj.getY();
return os;
}
这将声明 << 运算符的重载,如所见 here.
您还必须将迭代更改为:
for (list<Vertex>::iterator ii = vertices.begin(); ii != vertices.end(); ii++){
cout << *ii;
}
我遇到一个问题,我无法打印列表 'vertex' 顶点中的坐标集。解释一下,'Vertex' 是一个 class,它基本上包含构成顶点的两个坐标(形状的两条线相交处)。问题出在第二个 for 循环中,该循环旨在打印出列表中的坐标。任何人都可以帮助我尝试打印出来吗?我尝试了很多方法,到目前为止,错误总是给我带来问题。 代码如下,感谢帮助!
INT 主
int main(){
//obj
Console con;
RandomNumber rand;
Vertex vertex;
//Declarations
list<Vertex>vertices;
//list<Vertex>::iterator ii;
//set Vertex coords and push into list
for (int i = 0; i <= 8; i++){
vertices.push_back(Vertex(rand.random(0, 10), rand.random(0, 10)));
}
//iterate through the list outputting a char at each vertex (coords)
for (list<Vertex>::iterator ii = vertices.begin(); ii != vertices.end(); ii++){
cout << vertices[ii];
}
system("pause");
}
顶点CLASS
#pragma once
class Vertex
{
int x;
int y;
public:
Vertex(int x = 10, int y = 10);
~Vertex();
int getX() const;
int getY() const;
void setX(unsigned x);
void setY(unsigned y);
bool operator== (const Vertex &p2) const;
};
#include "Vertex.h"
Vertex::Vertex(int x, int y)
{
this->x = x;
this->y = y;
}
Vertex::~Vertex(){}
int Vertex::getX() const {
return x;
}
int Vertex::getY() const {
return y;
}
void Vertex::setX(unsigned x) {
this->x = x;
}
void Vertex::setY(unsigned y) {
this->y = y;
}
bool Vertex::operator== (const Vertex &point) const {
return (this->x == point.getX()) && (this->y == point.getY());
}
//iterate through the list outputting a char at each vertex (coords)
for (list<Vertex>::iterator ii = vertices.begin(); ii != vertices.end(); ii++){
cout << vertices[ii];
}
这本质上是错误的。您需要将迭代器当作指向列表中第 ii 个对象的指针来使用。
for (list<Vertex>::iterator ii = vertices.begin(); ii != vertices.end(); ii++){
cout << ii->getX() << ',' << ii->getY();
// if you have an overload for 'operator <<(std::ostream&, Vertex)' you can also do
cout << *ii
}
<<
运算符不适用于自定义类型。您需要指定 <<
运算符在遇到您的 Vertex
class 时的行为方式。
您需要生成一个能够理解您的顶点的签名 class。取以下函数:
std::ostream& operator<<(std::ostream &os, const Vertex &vertex) {
os << "Node: [" << vertex.getX() << ", " << vertex.getY() << "]";
return os;
}
这将允许您使用您编写的代码。
需要给你加一个功能class:
声明:
friend std::ostream& operator <<(std::ostream &os,const Vertex &obj);
和实施:
std::ostream& operator<<( std::ostream &os,const Vertex &obj )
{
os << << obj.getX() << ',' << obj.getY();
return os;
}
这将声明 << 运算符的重载,如所见 here.
您还必须将迭代更改为:
for (list<Vertex>::iterator ii = vertices.begin(); ii != vertices.end(); ii++){
cout << *ii;
}