c++11 - Implementing BFS using C++ -


i trying implement bfs in c++, here code.

#include <iostream> #include <list> #include <string> #include <limits> #include <map>  int infinity=std::numeric_limits<int>::max();   struct node{         int value;         int distance;         std::string color;          node(int val):         value(val),         distance(infinity),         color("white")         {} };  //using adjlist = std::map<node*,std::list<node*>>; typedef std::map<node*,std::list<node*>> adjlist; adjlist create_graph() {     node* n1 = new node(1);     node* n2 = new node(2);     node* n3 = new node(3);     node* n4 = new node(4);     node* n5 = new node(5);     node* n6 = new node(6);     node* n7 = new node(7);     node* n8 = new node(8);      adjlist m;     m[n1] = {n2, n5};     m[n2] = {n1, n6};     m[n3] = {n4, n6, n7};     m[n4] = {n3, n7, n8};     m[n5] = {n1};     m[n6] = {n2, n3, n7};     m[n7] = {n3, n4, n6, n8};     m[n8] = {n4, n7};     return m; }   void bfs(const adjlist& m, node* n1) {     std::list<node*> queue;     queue.push_back(n1);     unsigned count = 0;      while (!queue.empty())     {         auto n = queue.front();         std::cout << n->value << std::endl;         queue.pop_front();         std::cout << *(m[n].begin()) << std::endl;         for(auto = m[n].begin(); != m[n].end(); ++it)         {             if ((*it)->color != "black")                 queue.push_back(*it);         }          n->color = "black";         n->distance = count;         ++count;     } } 

on trying compile gcc, receive following error messages.

bfs.cpp: in function ‘void bfs(const adjlist&, node*)’: bfs.cpp:59:27: error: passing ‘const adjlist {aka const std::map<node*, std::list<node*> >}’ ‘this’ argument of ‘std::map<_key, _tp, _compare, _alloc>::mapped_type& std::map<_key, _tp, _compare, _alloc>::operator[](const key_type&) [with _key = node*; _tp = std::list<node*>; _compare = std::less<node*>; _alloc = std::allocator<std::pair<node* const, std::list<node*> > >; std::map<_key, _tp, _compare, _alloc>::mapped_type = std::list<node*>; std::map<_key, _tp, _compare, _alloc>::key_type = node*]’ discards qualifiers [-fpermissive]          std::cout << *(m[n].begin()) << std::endl;                            ^ bfs.cpp:60:20: error: passing ‘const adjlist {aka const std::map<node*, std::list<node*> >}’ ‘this’ argument of ‘std::map<_key, _tp, _compare, _alloc>::mapped_type& std::map<_key, _tp, _compare, _alloc>::operator[](const key_type&) [with _key = node*; _tp = std::list<node*>; _compare = std::less<node*>; _alloc = std::allocator<std::pair<node* const, std::list<node*> > >; std::map<_key, _tp, _compare, _alloc>::mapped_type = std::list<node*>; std::map<_key, _tp, _compare, _alloc>::key_type = node*]’ discards qualifiers [-fpermissive]    for(auto = m[n].begin(); != m[n].end(); ++it)                     ^ bfs.cpp:60:40: error: passing ‘const adjlist {aka const std::map<node*, std::list<node*> >}’ ‘this’ argument of ‘std::map<_key, _tp, _compare, _alloc>::mapped_type& std::map<_key, _tp, _compare, _alloc>::operator[](const key_type&) [with _key = node*; _tp = std::list<node*>; _compare = std::less<node*>; _alloc = std::allocator<std::pair<node* const, std::list<node*> > >; std::map<_key, _tp, _compare, _alloc>::mapped_type = std::list<node*>; std::map<_key, _tp, _compare, _alloc>::key_type = node*]’ discards qualifiers [-fpermissive]    for(auto = m[n].begin(); != m[n].end(); ++it) 

i not sure wrong. please point out mistake.

std::map::operator[] non-const because insert elements if needed:

int main() {     std::map<std::string, std::string> m;     m["new element"] = "1"; } 

the problem m const adjlist&, on cannot call non-const member functions. can use std::map::find() instead:

auto itor = m.find(n); if (itor != m.end())     std::cout << *(m->second.begin()) << std::endl; 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -