0

I created code to convert graph representation (A list of edges -> the adjacency list)

How can i fix error?

I have Segmentation fault in line res[cur.first - 1].insert(cur.second);for input

4 3
3 2
2 1
4 2

My code

#include <bits/stdc++.h>

using namespace std;

pair<int, int> strToSotredPair(const string& s) {
    int a = 0, b = 0;
    int pos = 0;
    while (s[pos] != ' ') {
        a = a * 10 + s[pos] - '0';
        ++pos;
    }
    while (pos < s.size()) {
        b = b * 10 + s[pos] - '0';
        ++pos;
    }
    return {min(a, b), max(a, b)};
}


int main() {
    int n, m;
    string input;
    cin >> n;
    cin >> m;
    vector<set<int>> res(n, set<int>());
    for (int i = 0; i < m; ++i) {
        cin >> input;
        auto cur = strToSotredPair(input);
        res[cur.first - 1].insert(cur.second); // error in this line
    }
    for (int i = 0; i < res.size(); ++i) {
        cout << res[i].size() << ' ';
        for (auto item : res[i]) {
            cout << item << ' ';
        }
        cout << endl;
    }
}
alexmosk25
  • 127
  • 1
  • 9
  • 1
    Apart from anything else you never check that `cur.first - 1` is a valid index for `res`. – G.M. Aug 30 '20 at 13:15
  • Output the result for strToSotredPair() and that could help you. – splrs Aug 30 '20 at 13:21
  • 3
    [Why should I not #include ?](https://stackoverflow.com/q/31816095/5910058) – Jesper Juhl Aug 30 '20 at 13:23
  • 3
    [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/5910058) – Jesper Juhl Aug 30 '20 at 13:23
  • 2
    The bug in `strToSortedPair` is trivially observed in a debugger. This is precisely what a debugger is for. If you don't know how to use a debugger this is a good opportunity to learn how to use it to run your program one line at a time, monitor all variables and their values as they change, and analyse your program's logical execution flow. Knowing how to use a debugger is a required skill for every C++ developer, no exceptions. With your debugger's help you should be able to quickly find all bugs in this and all future programs you write, without having to ask anyone for help. – Sam Varshavchik Aug 30 '20 at 13:32

1 Answers1

1

There are a couple of error's in your code because of which you were getting that error.

  1. You wanted the graph edges as strings. So you need to take edge inputs as strings. For that, you would be required to use getline(cin,input) and cin.ignore(). (My suggestion: Do not take inputs as strings because as it is you are converting them to integers afterwards. By default if you just write cin<<node1<<node2; the two nodes of an edge would have come as integers and function strToSotredPair would not be needed).

  2. In function strToSotredPair after you get the integer a, pos variable is pointing to the space. So you need to increase it by 1 to point to the starting position to 2nd number. Hence a pos++ was needed.

    #include <bits/stdc++.h>
    using namespace std;
    
    pair<int, int> strToSotredPair(const string& s) {
        int a = 0, b = 0;
        int pos = 0;
        while (s[pos] != ' ') {
            a = a * 10 + s[pos] - '0';
            ++pos;
        }
        pos++;
        while (pos < s.size()) {
            b = b * 10 + s[pos] - '0';
            ++pos;
        }
        return {min(a, b), max(a, b)};
    }
    
    
    int main() {
        int n, m;
        string input;
        cin >> n;
        cin >> m;
        vector<set<int>> res(n, set<int>());
        cin.ignore(); // To clear the input buffer
        for (int i = 0; i < m; ++i) {
            getline(cin, input); // to get line by line string inputs
            auto cur = strToSotredPair(input);
            res[cur.first - 1].insert(cur.second); 
        }
        for (int i = 0; i < res.size(); ++i) {
            cout << res[i].size() << ' ';
            for (auto item : res[i]) {
                cout << item << ' ';
            }
            cout << endl;
        }
    }

algo_user
  • 191
  • 7