How to return null pointer for list member c++? -


i need return pointer member in list, , if required member not there null pointer (or other indication)

list<links>::iterator find_link(list<links> &link, char* id) {     list<links>::iterator link_index;     links link_i;      link_index = link.begin();     (int = 0; < link.size(), i++)     {         link_i = *link_index;          if (!strcmp(link_i.id, id)) { return link_index; };          link_index++;      };     cout << "node outsession not exist " << id;     return nullptr; // error (nullptr not same type list<links>::iterator) }; 

i added error message (cout << "node outsession not exist " << id;) indicate if link not found, need indicate caller function.

how can ? thanks

nullptr not iterator, can't return in function returns iterator value

std::find(begin(), end(), value) returns iterator , returns end() if value not found. check say:

std::list<int> l; ... auto found = std::find(l.begin(), l.end(), 6); // returns std::list::iterator if (found == l.end()) {     std::cout << "bogus!" << std::endl; } ... 

there other examples in container find returns value, , uses special value "not found"

std::string s = "go store"; auto found = s.find('x'); // returns size_t index string if (found == std::string::npos) {     std::cout << "bogus!" << std::endl; } 

returning nullptr make sense if function returned pointer. in case, nullptr reasonable sentinel value not found.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -