"vector subscript out of range"

Hello, I am working on a graph algorithm but I have a problem. It seems that my problem about adding elements to a vector but I can't fix it. Can anyone help me please?

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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Node;
class Link {
public:
	int cost;
	Node *dest;
	Link(int c, Node *d) {
		cost = c;
		dest = d;
	}
};

class Node {
public:
	char name;
	vector<Link> neighbors;
	Node(char n) {
		name = n;
	}
};

int main() {
	
	vector<Node> nodes;
	vector<char> temp;
	int k = 10;
	char s;
	for (int i = 0; i < k; i++) {
		cout << "enter nodes or Q for ending" << endl;
		cin >> s;
		if (s == 'Q') {
			k = -1;
		}
		else {
			temp.push_back(s);
		}

	}
	for (int i = 0; i < temp.size(); i++)
	{
		nodes.push_back({ temp[i], });
	}
	int a = 0, cost = 0;
	k = 10;
	int b = 10;
	for (int j = 0; j < b; j++) {

		for (int i = 0; i < k; i++) {
			cout << "enter destination of " << nodes[a].name << " for ending please enter Q (end of all enter W)" << endl;
			cin >> s;
			
			if (s == 'W') {
				k = -1;
				b = -1;
			}
			else if (s == 'Q') {
				k = -1;
			}
			else {
				Node dest(s);
				cout << "enter cost" << endl;
				cin >> cost;
				Link c(cost, &dest);
				nodes[a].neighbors.push_back(c);
                             }
		}
			k = 10;
			a++;
	}
}
Last edited on
print the value of 'a' and 'nodes.size()' in the line 51 loop every iteration.
Thank you, that is fixed. but I have the same problem is here. what should I do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char start;
	int limit;
	int total=0;
	cout << "enter starting node :" << endl;
	cin >> start;
	cout << "enter a limit :" << endl;
	cin >> limit;

	for (int i = 0; i < nodes.size(); i++) {
		if (nodes[i].name == start) {
			for (int j = 0; j < nodes.size(); j++) {
				total += nodes[i].neighbors[j].cost;
					if (total <= limit) {
						cout << nodes[i].neighbors[j].dest->name;
						start = nodes[i].neighbors[j].dest->name;
					}
			}
		}
	}
Lets write the loop with a bit different style for emphasis:
1
2
3
4
5
6
7
8
9
10
11
12
13
for ( const auto & jack : nodes ) {
  if ( jack.name == start ) {
    const auto & neigh = jack.neighbors;

    for ( int j = 0; j < nodes.size(); j++) {
      total += neigh[j].cost;
      if (total <= limit) {
        cout << neigh[j].dest->name;
        start = neigh[j].dest->name;
      }
    }
  }
}

Your inner loop iterates nodes.size() elements of the neigh that is a vector<Link>.
Does nodes.size() == neigh.size()?
Thank you, for your answer, nodes.size()!=neigh.size but when I made this
 
for ( int j = 0; j < neigh.size(); j++) 
for loop works but I cannot get the result that I want. should I create a new algorithm ?
We can't see what you {did,got,expected} and thus cannot hint how to fix it. That issue you can help with.
I want exactly this:

Last edited on
You are on a course about graphs, so you should "know" the usual approaches: depth first, etc. That leaves two tasks: (1) choose suitable approach and (2) implement it correctly.

Side note:
If each node has unique name, then
1
2
for ( const auto & jack : nodes ) {
  if ( jack.name == start ) {

is technically correct, but logically misleading. Preferably:
1
2
1. Find node X that is the starting point
2. Get routes for X

1
2
3
4
auto node = std::find_if( nodes.begin(), nodes.end(), [start](auto& n){return start == n.name;} );
if ( node != nodes.end() ) {
  // determine routes from *node
}


You do output within the search. In the second example you probably get:
ACBACBDB
If you would print newline when limit is reached, then you would get:
ACBACB
DB

Perhaps you should store the route and show it when you hit the limit? Not before.
Note that whenever you reach a crossroads, you have to make a copy of the "route so far" for each path that you will follow.
Actually I am on a course about algorithms, so I don't know much about graph.

yes, each node has unique name but I didn't understand what do you mean this
1
2
3
4
auto node = std::find_if( nodes.begin(), nodes.end(), [start](auto& n){return start == n.name;} );
if ( node != nodes.end() ) {
  // determine routes from *node
}


Should I rewrite this loop ?
1
2
3
4
5
6
7
8
9
10
11
for (int i = 0; i < nodes.size(); i++) {
		if (nodes[i].name == start) {
			for (int j = 0; j < nodes.size(); j++) {
				total += nodes[i].neighbors[j].cost;
					if (total <= limit) {
						cout << nodes[i].neighbors[j].dest->name;
						start = nodes[i].neighbors[j].dest->name;
					}
			}
		}
	}
Think about:
1
2
3
4
auto node = std::find_if( nodes.begin(), nodes.end(), [start](auto& n){return start == n.name;} );
if ( node != nodes.end() ) {
  routes( *node, 0, limit );
}

where void routes( const Node& node, int total, int limit ); is a recursive function that prints routes.
Ok. I will work on it.
Thank you very much @keskiverto
Topic archived. No new replies allowed.