非常感謝您的評論,我在這裏是新手,不熟悉協議。這是我完整的例子。這裏就是我的頂點構造函數 裝載頂點[0] = XXX 在頂點構造函數 裝載頂點[1] = XXX 在頂點構造函數 裝載在頂點構造函數頂點[2] = XXX 得到執行基本指針/參考問題
加載頂點[3] = XXX 在頂點構造函數 加載頂點[4] = XXX 在頂點構造函數 加載頂點[5] = XXX 在頂點構造函數 加載頂點[0] = XXX calculateCosts 得到頂點命名xxx 移動頂點n艾湄XXX 做 分段故障
#include <iostream>
#include <vector>
#include <cassert>
#include <limits>
#include <initializer_list>
#include <iterator>
// required compile option g++ homeWork2.cpp -std=c++0x
using namespace std;
const int infinity = numeric_limits<int>::infinity();
//const vertex* nullPtr = 0;
class vertex
{
public:
string name;
vertex(string name = "undefined")
{
cout << "in vertex ctor " << endl;
name = "AAAAAA";
value = infinity;
prev_vertex = NULL;
}
void set_value(int& value)
{
assert(value > 0);
value = value;
}
int get_value()
{
return value;
}
void set_prev_vertex(vertex& prev_vertex)
{
prev_vertex = prev_vertex;
}
vertex* get_prev_vertex()
{
return prev_vertex;
}
private:
int value;
vertex * prev_vertex;
};
class graph
{
public:
graph(int graphSize = 50)
{
for (int i = 0; i < graphSize; ++i)
{
vertex* pV = new vertex("vertex name");
pV->name = "xxx";
cout << "loading vertex[ " << i << "] = " << pV->name << endl;
vertices.push_back(pV);
}
}
// vector<vertex> get_neighbors(int i);
void push_Vertex(unsigned position, vertex* v);
void remove_Vertex(unsigned position);
vertex* get_Vertex(unsigned position);
int size();
vector<vertex*> get_neighbors(int i);
~graph()
{
while (vertices.size() > 0)
{
// cout << "removing vertices[ " << vertices.size() << "] = " << vertices.back() << endl;
delete vertices.back();
vertices.pop_back();
}
}
private:
int graph_count;
vector<vertex*> vertices;
};
void graph::push_Vertex(unsigned position, vertex* v)
{
// cout << "copying vertex named " << *v->name << endl;
// assert(v!=NULL);
vertices[position] = v;
// vertices.push_back(v);
}
void graph::remove_Vertex(unsigned position)
{
vertices.erase(vertices.begin()+position);
}
vertex* graph::get_Vertex(unsigned position)
{
// vector<int>::iterator iter = vertices.begin()+=position;
vertex* pV = vertices.at(position);
cout << "got vertex named " << pV->name << endl;
return pV;
}
vector<vertex*> graph::get_neighbors(int i)
{
vector<vertex*> vertices;
/* load the it neighbors*/
return vertices;
}
int graph::size()
{
return vertices.size();
}
void moveVertex(graph& fromSet, graph& toSet, int position, int value)
{
vertex* v = fromSet.get_Vertex(position);
cout << "moving vertex named " << v->name << endl;
toSet.push_Vertex(0, v);
}
void calculateCosts(graph& unvisitedSet, graph& visitedSet)
{
moveVertex(unvisitedSet, visitedSet, 0, 0);
for (int i =0; i < unvisitedSet.size(); ++i) {
// do the algo process ...
}
}
//
int main()
{
// adjacency_list adjList = adjacency_list(6);
// cost_list costList = cost_list(6);
graph visitedSet = graph(6);
graph unvisitedSet = graph(1);
cout << "calculateCosts" << endl;
calculateCosts(visitedSet, unvisitedSet);
cout << "done" << endl;
return 0;
}
請發表一篇http://sscce.org/ – d33tah