Simplifying the following graph class

I have been studying graphs currently and came across a way to implement it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<functional>
#include<cstdlib>

using namespace std;

struct vertex;

struct edge
{
    vertex *dest;
    double cost;
    edge(vertex *a=NULL, double b=0)
    {
        dest=a;
        cost=b;
    }
};

struct vertex
{
    string name;
    vector<edge> adj;
    vertex(string s)
    {
        name=s;
    }
};

class graph
{
    public:
        typedef map<string, vertex *,less<string> > vmap;
        vmap work;
        void addvertex(const string&);
        void viewvertex();
        void addedge(const string& from, const string& to, double cost);
        void viewcostofedge(const string &from, const string &to);
};

void graph::addvertex(const string &name)
{
    vmap::iterator itr=work.begin();
    itr=work.find(name);
    if(itr==work.end())
    {
        vertex *v;
        v= new vertex(name);
        work[name]=v;
        return;
    }
        cout<<"\nVertex already exists!";

}

void graph::viewvertex()
{
    vmap::iterator itr=work.begin();
    for(;itr!=work.end();itr++)
    {
        cout<<"\n"<<itr->first;
    }
}


void graph::addedge(const string& from, const string& to, double cost)
{
    vertex *f=(work.find(from)->second);
    vertex *t=(work.find(to)->second);
    edge added(t,cost);
    f->adj.push_back(added);
}


void graph::viewcostofedge(const string &from, const string &to)
{
    vmap::iterator itr=work.find(from);
    vector<edge> v=(itr->second)->adj;
    vector<edge>::iterator itr1=v.begin();
    vertex *p;
    for(;itr1!=v.end();itr1++)
    {
        if(((*itr1).dest)->name==to)
            cout<<"\nThe cost is:-"<<(*itr1).cost;
    }
}

int main()
{
    int ch;

    graph g;
    string name;
    string string1,string2;
    double cost;
    do
    {
        cout<<"\n\n\n1.Add Vertex\n2.View Vertex\n3.Add Edge\n4.View Cost\n5.Exit\nEnter choice:-\n\n\n";
        cin>>ch;
        switch(ch)
        {
            case 1:
                cout<<"\nEnter the name of the vertex:-";
                cin>>name;
                g.addvertex(name);
                break;
            case 2:
                g.viewvertex();
                break;
            case 3:

                cout<<"\nEnter the spot:-";
                cin>>string1;
                cout<<"\nEnter the destination!";
                cin>>string2;
                cout<<"\nEnter cost:-";
                cin>>cost;
                g.addedge(string1,string2,cost);
                break;
            case 4:

                cout<<"\nEnter the spot:-";
                cin>>string1;
                cout<<"\nEnter the destination!";
                cin>>string2;
                g.viewcostofedge(string1,string2);
                break;
            case 5:
                exit(0);
            default:
                cout<<"\nWrong Choice!";
        }
    }while(1);
}


As you can see, the program seems overly complicated with all those refrences to other class. Can you help me simplify this class?
It looks fine to me, although I don't see why you need an edge class.
I need an edge struct for a list of edges.

Anyways, thanks. :)

Mod, you can lock this thread.
Topic archived. No new replies allowed.