#include
#include
using namespace std;
class Node
{
public:
Node();
void SetX(int);
int GetX();
private:
int x;
};
Node::Node()
{
x = 0;
}
int Node::GetX()
{
return x;
}
void Node::SetX(int i)
{
x = i;
}
class test
{
public:
void Add(Node*);
vector GetNode();
private:
vector m_node;
};
void test::Add(Node* node)
{
m_node.push_back(node);
}
vector test::GetNode()
{
return m_node;
}
int main()
{
test tes;
for (int i = 0; i < 10; i++)
{
Node* node = new Node;
tes.Add(node);
}
vector<Node*>::iterator iter = tes.GetNode().begin();
for (; iter != tes.GetNode().end(); iter++)
{
cout << (*iter)->GetX() << endl;
}
}
你剛開始學吧,好好看看vector的用法
class Node
{
public:
Node();
void SetX(int);
int GetX();
private:
int x;
};
Node::Node()
{
x = 0;
}
int Node::GetX()
{
return x;
}
void Node::SetX(int i)
{
x = i;
}
class test
{
public:
void Add(Node* node);
vector<Node*>* GetNode();
private:
vector<Node*> m_node;
};
void test::Add(Node* node)
{
m_node.push_back(node);
}
vector<Node*>* test::GetNode()
{
return &m_node;
}
int main()
{
test tes;
for (int i = 0; i < 10; i++)
{
Node* node = new Node;
node->SetX(i);
tes.Add(node);
}
vector<Node*>::iterator iter = tes.GetNode()->begin();
for (; iter != tes.GetNode()->end(); iter++)
{
cout << (*iter)->GetX() << endl;
}
//最後釋放node
}