0

I'm trying to get a vector of string from input to create a graph , but i don't know why in middle my code it crashes. please help me fix it. I use Visual Studio.

#include <iostream>
#include <vector>
#include <iterator>

void main(void)
{
    {
        using namespace std;
        int8_t n{ 0 };
        cout << "enter the size of graph : ";
        cin >> n;
        vector<string> graph(n);
        string connectionsWith;
        vector<string>::iterator i;
        string::iterator e;
        int p{ 0 };
        for (i = graph.begin(); i != graph.end(); ++i)
        {
            cout << '\n' << "enter the vertices that are connected to " << p << " : ";
            cin >> connectionsWith;
            graph.push_back(connectionsWith);
            p++;
        }
        p = 0;
        for (i = graph.begin(); i != graph.end(); ++i)
        {
            cout << p << " is connected to " << *i;
            p++;
        }
    }
}
Shahrooz
  • 196
  • 1
  • 14
  • 1
    Did you try to use a debugger? – RoQuOTriX Dec 16 '19 at 13:00
  • @RoQuOTriX yes I use Visual Studio – Shahrooz Dec 16 '19 at 13:00
  • 1
    Your iterators are invalidated when you do a `push_back`. I'm not sure what you are trying to do exactly, but you probably just want a simple `for ( size_t i = 0; i < graph.size(); ++i )` – ChrisMM Dec 16 '19 at 13:01
  • 1
    `void main(void)` no. Use [`int main()`](https://stackoverflow.com/q/204476/) – L. F. Dec 16 '19 at 13:03
  • 2
    Also don't do [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – ChrisMM Dec 16 '19 at 13:04
  • @ShahroozLeon didn't know Visual Studio was a debugger. Everyday I learn something new – RoQuOTriX Dec 16 '19 at 13:08
  • 1
    Why do you insist on using iterators for the first loop? – user253751 Dec 16 '19 at 13:17
  • I thought they must be used like this , I'm newbie to these iterators and I'm not sure about using them correctly. please if you can tell me where in this code I should use iterator and where not. @user253751 – Shahrooz Dec 16 '19 at 13:29

2 Answers2

2

In your constructor of graph, you allocate n string. Inside the loop you add on top of the constructed n strings, n more strings via push back. That potentially invalidates your iterators, as ChrisMM said, also not the most efficient way to implement such a scenario.

So as a solution, instead of

vector<string> graph(n);

do

vector<string> graph;
graph.reserve(n);

and iterate over the index, e.g. from 0 to n, and push back.

Especially in the first loop you are not dereferencing the iterator at all, which suggests that using index based loop would show your intent better.

life_steal
  • 55
  • 7
0

life_steal pointed out the problem. I would like to add few information and other way to solve the problem.

int8_t n{ 0 }; 

vector<string> graph(n); // Here initialization happening with ASCII. If input is "1" then it would be "49". Consider changing int8_t to int if you unaware of that. 

graph.push_back(connectionsWith); //Instead of this line Use: *i = connectionsWith; it directly assign the value.
programmer 05
  • 74
  • 1
  • 11