实现图,节点/类语法c++
Implementing Graphs, node/class syntax C++
我正在阅读关于图形的TopCoder教程http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=graphsDataStrucs2,我正在尝试将伪代码实现到c++中。我的问题是:类和结构有什么不同?哪一个更适合处理图形?如何在c++中传递类节点到堆栈中?我试过这样做,但是当我通过s.push(node(top.x+1, top.y));
时,它说没有匹配的函数找到。但是如果我像这样传递:
node a;
a.x=x;
a.y=y;
s.push(a);
不会产生任何错误。我只是想实现和编写问题的解决方案GrafixMask(伪代码在上面的链接中给出)。下面是我的部分实现:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
bool fills[600][400];
class node
{
public:
int x;
int y;};
int doFill(int x, int y)
{
int result=0;
stack<node> s;
node a;
a.x=x;
a.y=y;
s.push(a);
while(s.empty()==false)
{
node top=s.top();
s.pop();
if(top.x < 0 || top.x>=600) continue;
if (top.y < 0 || top.y >=400) continue;
if(fills[top.x][top.y]) continue;
fills[top.x][top.y]=true;
result++;
s.push(node(top.x+1, top.y));
s.push(node(top.x, top. y+1));
s.push(node(top.x, top.y-1));
s.push(node(top.x-1, top.y));
}
return result;
}
int main()
{
for(int i=0; i < 599; i++)
for(int j=0; j < 399; j++)
fills[i][j]=false;
}
您的node
类没有接受两个整数的构造函数。如果您想以这种方式构造节点,则需要有:
class node
{
public:
int x;
int y;
node() : x( 0 ), y( 0 ) {}
node( int pX, int pY ) : x( pX ), y( pY ) {}
};
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 助记符和指向成员语法的指针
- 有人能分解一下这个c++模板的语法吗
- C++避免重复声明的语法是什么
- QMetaObject invokeMethod的基于函数指针的语法
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 单独定义模板化嵌套类方法的正确语法
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 为什么我会收到错误 C2143 语法错误:缺少"*"之前的';'?
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 使用基类指针调用基类的值构造函数的语法是什么?
- 很好的语法来获取对向量/数组数据的大小引用?
- C++语法运算符功能?
- C++使用 rand 定义函数语法
- 什么文件可以修改 atom 的C++语法?
- 创建模板嵌套类实例的语法?
- C++语法差异:二维和一维数组(指针算术)
- 将信息输入到下面显示的结构向量中的正确语法/格式是什么