Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7788201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:55:16+00:00 2026-06-01T20:55:16+00:00

I was trying to implement some Graph Algorithms so continuing by testing I received

  • 0

I was trying to implement some Graph Algorithms so continuing by testing I received an error in GNU C++ compiler (Segmentation Fault). In Visual Studio I saw the cause is “vector iterators incompatible”. But how this happens? The error is thrown in shortestPathBFS function in the line “getName()” when I try to access a field of visitor object. Visitor is an element of Vertice* queue, so it must not depend on queue iterator in my opinion. If you can explain me why, I will be appreciated.

#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <string>
#include <iostream>

using namespace std;

//#define traverse(container,iterator) \
//for(typeof(container.begin()) iterator = container.begin(); iterator != container.end(); iterator++)

class Edge;
class Vertice
{
private:
    string name;
    vector<Edge*> incidences;
    bool visited;
public:
    Vertice() {name = "NULL"; visited = false;}
    Vertice(string name) {this->name = name; visited = false;}
    void setName(string name) {this->name = name;}
    string getName() {return name;}
    bool isVisited() {return visited;}
    void setVisited() {visited = true;}
    void setUnvisited() {visited = false;}
    void connectTo(Vertice*);
    void connectTo(Vertice*,int);
    void printNeighbors();
    vector<Vertice*> getNeighbors();
};

class Edge
{
private:
    int cost;
    Vertice *start,*end;
public:
    friend class Vertice;
    Edge() {cost = 0;}
    Edge(Vertice * start, Vertice * end) {this->start = start; this->end = end;} 
    Edge(Vertice * start, Vertice * end, int cost)
    {this->start = start; this->end = end; this->cost = cost;}
    Vertice* getEnd() {return end;}
    Vertice* getStart() {return start;}
};

void Vertice::connectTo(Vertice * w)
{
    incidences.push_back(new Edge(this,w));
}

void Vertice::connectTo(Vertice* w,int cost)
{
    incidences.push_back(new Edge(this,w,cost));
}

vector<Vertice*> Vertice::getNeighbors()
{
    vector<Vertice*> temp;
    for(vector<Edge*>::iterator it = incidences.begin(); it != incidences.end(); it++)
    {
        temp.push_back((*it)->getEnd());
    }
    return temp;
}

void Vertice::printNeighbors()
{
    for (vector<Edge*>::iterator i=incidences.begin(); i!= incidences.end(); i++)
    {
        cout<<(*i)->start->getName()<<"--"<<(*i)->cost<<"--"<<(*i)->end->getName()<<endl;
    }
}

class Graph
{
public:
    // using set for non-comparable elements are not good
    // but this is for exercising
    set<Vertice *> vertices;
public:
    void initGraph()
    {
        Vertice *v;
        v = new Vertice("IST");vertices.insert(v);
        v = new Vertice("ANK");vertices.insert(v);
        v = new Vertice("IZM");vertices.insert(v);
        v = new Vertice("BER");vertices.insert(v);
        v = new Vertice("TOR");vertices.insert(v);
        v = new Vertice("BEJ");vertices.insert(v);
        v = new Vertice("PER");vertices.insert(v);

        (*findByName("IST"))->connectTo(*findByName("ANK"),10);
        (*findByName("IST"))->connectTo(*findByName("IZM"),5);
        (*findByName("IST"))->connectTo(*findByName("BER"),61);

        (*findByName("IZM"))->connectTo(*findByName("ANK"),3);
        (*findByName("IZM"))->connectTo(*findByName("TOR"),98);
        (*findByName("IZM"))->connectTo(*findByName("BER"),70);

        (*findByName("BER"))->connectTo(*findByName("ANK"),59);
        (*findByName("BER"))->connectTo(*findByName("TOR"),91);

        (*findByName("ANK"))->connectTo(*findByName("PER"),77);
        (*findByName("ANK"))->connectTo(*findByName("BEJ"),151);

        (*findByName("BEJ"))->connectTo(*findByName("TOR"),48);
        (*findByName("TOR"))->connectTo(*findByName("ANK"),100);
        (*findByName("PER"))->connectTo(*findByName("BEJ"),162);
        (*findByName("TOR"))->connectTo(*findByName("PER"),190);
        (*findByName("BEJ"))->connectTo(*findByName("PER"),163);
    }

    set<Vertice*>::iterator findByName(string name)
    {
        for(set<Vertice*>::iterator it = vertices.begin(); it != vertices.end(); it++)
        {
            if ((*it)->getName() == name)
            {
                return it;
            }
        }
        return vertices.end();
    }

    int shortestPathBFS(Vertice * start, Vertice * finish)
    {
        queue<Vertice *> q;
        q.push(start);

        Vertice *visitor;
        while(!q.empty())
        {
            visitor = q.front();q.pop(); 
            visitor->setVisited();
            cout<<"BFS : "<<visitor->getName()<<endl;
            if (visitor->getName() == finish->getName())
            {
                break;
            }
            for(vector<Vertice*>::iterator it = (visitor->getNeighbors()).begin(); it != (visitor->getNeighbors()).end(); it++ )
            {
                if (!(*it)->isVisited())
                {
                    q.push((*it));
                }
            }
        }
        return 0;
    }

    void printAll()
    {
        for(set<Vertice*>::iterator it = vertices.begin(); it != vertices.end(); it++)
        {
            (*it)->printNeighbors();
        }
    }
};

int main(int argc, char **argv)
{
    Graph g;
    g.initGraph();
    g.printAll();
    g.shortestPathBFS(*(g.findByName("IST")),*(g.findByName("PER")));

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:55:18+00:00Added an answer on June 1, 2026 at 8:55 pm

    The culprit is this line in Graph::shortestPathBFS:

    for(vector<Vertice*>::iterator it = (visitor->getNeighbors()).begin(); it != (visitor->getNeighbors()).end(); it++ )
    

    The problem is that you cannot compare iterators from two different containers (even if the containers are the same type), but visitor->getNeighbors() returns a new object each time it is invoked. Consequently, it is initialized from one object then compared to an iterator from a different object.

    Rewrite the loop as:

    vector<Vertice*> neighbors = visitor->getNeighbors();
    for(vector<Vertice*>::iterator it = neighbors.begin(); it != neighbors.end(); ++it)
    {
        if (!(*it)->isVisited())
        {
            q.push((*it));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement some unit testing for an old framework. I am
I'm trying to implement some STL-style sorting algorithms. The prototype for std::sort looks something
I'm currently studying and trying to implement some algorithms. I'm trying to understand Big
I'm trying to implement some front end testing in a ASP.NET web application, and
im trying to implement some behaviors when a mapview element scrolls... by coding a
I'm trying to implement some rudimentary tabs in a Cocoa editor I'm working on.
I am trying to implement some interface changes in my app, based on the
I'm trying to implement some custom template tags to to a little more with
I am trying to implement some code on my web page to auto-scroll after
I'm trying to implement some small app which is a gui app and has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.