map/set iterator not dereferencable. Multimap container isse - c++

I'm geting this error message map/set iterator not dereferencable When trying to get value by key in multimap. In this code I'm trying to show nonoriented graph represented by adjacency list (vector<Vertex*> vertexList)
void NonOrGraph::show() {
cout << endl;
multimap<int, int> used;
for (int i = 0; i < vertexList.size(); i++) {
if (vertexList[i]->adjMap.empty()) {
cout << vertexList[i]->index << " isolated";
} else {
for(map<Vertex*, int>::iterator it = vertexList[i]->adjMap.begin();
it != vertexList[i]->adjMap.end();
it++)
{
int from = vertexList[i]->index;
int to = it->first->index;
int weight = it->second;
used.insert(pair<int, int>(from, to));
if (used.find(to)->second != from) {
cout << from << " <--(" << weight << ")--> " << to << endl;
}
}
}
}
cout << "\n\n";
}

The problem is likely here:
if (used.find(to)->second != from) {
If to is not in used, used.find() will return used.end(), which you then dereference. It is undefined behavior to dereference the end() iterator, which in this case manifests by giving you a runtime error.
You have to check the iterator against end() first:
std::multimap<int, int>::iterator multi_it = used.find(to);
if (multi_it != used.end() && multi_it->second != from) {
// ^^^^^^^^^^^^^^^^^^^^
// now, it's safe to dereference

Related

How do I loop through a unordered_map in C++ without auto?

I am trying to loop through an unordered_map, to find if any of its VALUES is greater than 2. But this syntax is wrong
unordered_map<int, int> mp;
for (int i = 0; i < N; i++)
{
mp[arr[i]]++;
}
for (int i = 0; i < mp.size(); i++ ) {
cout << mp[i].second << " " << endl; //mp[i].second is wrong syntax
if (mp[i].second > 2) {
cout << "WRONG";
x = false;
break;
}
}
how can I do this?
It seems that you assumed(incorrectly) that mp[i] is std::pair<int, int> when in fact mp[i] gives you the mapped value which is of type int in your example. This can be seen from std::map::operator[]:
T& operator[]( Key&& key ); (2)
Returns a reference to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist.
(emphasis mine)
Method 1
Since you don't want to use auto, you can explicitly write the type std::pair<int, int> in the range-based for loop as shown below:
//---------vvvvvvvvvvvvvvvvvvvvvvvv--------------------->not using auto here as you want
for (const std::pair<const int, int> &pair_elem: mp) {
cout << pair_elem.second << " " << endl;
if (pair_elem.second > 2) {
cout << "WRONG";
//other code here
}
}
Method 2
Note that you can also use std::find_if instead of a for loop as shown below:
auto iter = std::find_if(mp.begin(), mp.end(), [](const std::pair<int,int>& elem)
{ return elem.second > 2;});
if (iter != mp.end())
std::cout << "Greater found ";
else
{
std::cout<<"Greater not found"<<std::endl;
}
Demo
your question is missing a lot of information and literarily , if you searched on web , you would find many ways to loop over unordered_map in C++ , but anyways , here is the edited version of your code with a test case:
#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
const int N = 10;
int arr[N] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
bool x = true;
unordered_map<int, int> mp;
unordered_map<int, int>:: iterator it;
for (int i = 0; i < N; i++)
{
mp[arr[i]]++;
}
mp[9] = 5;
for (it = mp.begin(); it != mp.end(); it++) {
cout << "key is " << it->first << " ,value is " << it->second << " " << endl;
if (it -> second > 2) {
cout << "WRONG";
x = false;
cout << "key is " << it->first << " ,value is " << it->second << " is greater than 2" << endl;
break;
}
}
return 0;
}

map::erase sometimes does not erase the entry from the map

I'm having a sporadic problem right now where map::erase() says it worked, but the map sometimes still has the element in it.
auto iterator = device_map.begin(); // std::map<std::string,Device*>
size_t nDeleted = 0;
while (iterator != map.end()) {
Device* device = device_map[iterator->first];
if (device->done()) {
device->close();
cout << "Erasing " << iterator->first << endl;
nDeleted = device_map.erase(iterator->first);
delete device;
}
++iterator;
}
This is called in a std::thread every 500ms. I've tested it by printing the contents of the map before and after the loop, like this:
cout << "device_map = ";
for (std::map<string, Device*>::iterator it = device_map.begin(); it != device_map.end(); ++it) {
cout << it->first << " = " << it->second << "; ";
}
cout << endl;
Sometimes, even if nDeleted == 1, the contents of device_map is the same before and after unmapping the element (except that *device == nullptr because of the delete).
I've also put a breakpoint and device_map.size() is indeed unchanged after calling device_map.erase() does not work.
I have tried this other version of map::erase with the same results:
auto iterator = device_map.begin(); // std::map<std::string,Device*>
size_t nDeleted = 0;
while (iterator != map.end()) {
Device* device = device_map[iterator->first];
if (device->done()) {
device->close();
cout << "Erasing " << iterator->first << endl;
iterator = device_map.erase(iterator);
delete device;
} else {
++iterator;
}
}
Is there something that I'm doing wrong?
Thank you,
Fred
As #Jeffrey suggested, the issue was with another thread accessing the std::map object while map::erase was being called. Adding a mutex solved the problem.

Getting strange error while trying to set up an iterator

This is probably a silly error but I can't seem to find what I have done wrong.
The error I am getting is no operator "=" matches these operands.
Here is my code...
void print_words(const map < string, int >& m1) {
map<string, int>::iterator it;
cout << "Number of non-empty words: " << m1.size() << '\n';
int count = 0;
for (it = m1.begin(); it != m1.end(); it++) {
}
}
I get the error in the for loop in the it = m1.begin() statement and I cannot go on to print out the map if I can't iterate through it.
Use a const iterator:
void print_words(const map < string, int >& m1) {
cout << "Number of non-empty words: " << m1.size() << '\n';
int count = 0;
for (map<string, int>::const_iterator it = m1.cbegin(); it != m1.cend(); it++) {
}
}
Use a const_iterator or auto.
void print_words(const map < string, int >& m1) {
cout << "Number of non-empty words: " << m1.size() << '\n';
int count = 0;
for (auto it = m1.cbegin(); it != m1.cend(); it++) {
}
}

map iterator not dereferencable

I am getting compile error "map/set" iterator not dereferencable". What is going on here?
#include<iostream>
#include<map>
using namespace std;
int main(){
map<int, int> m;
map<int, int>::iterator itr=m.begin();
itr->first = 0;
itr->second = 0;
cout << itr->first << " " << itr->second;
return 0;
}
The map is empty, so m.begin() is equal to the past-the-end iterator and is therefore invalid.
You first have to insert elements somehow (you can also do that implicitly by using operator[]) to make it useful.
Also, you cannot modify the key of an element like this. You would have to remove (erase) the element from the map and insert a new one with the new key.
Here's an example about that:
#include<iostream>
#include<map>
using namespace std;
int main(){
map<int, int> m;
// insert element by map::insert
m.insert(make_pair(3, 3));
// insert element by map::operator[]
m[5] = 5;
std::cout << "increased values by one" << std::endl;
for(map<int, int>::iterator it = m.begin(); it != m.end(); ++it)
{
it->second += 1;
cout << it->first << " " << it->second << std::endl;
}
// replace an element:
map<int, int>::iterator thing = m.find(3);
int value = thing->second;
m.erase(thing);
m[4] = value;
std::cout << "replaced an element and inserted with different key" << std::endl;
for(map<int, int>::iterator it = m.begin(); it != m.end(); ++it)
{
cout << it->first << " " << it->second << std::endl;
}
return 0;
}
Your map's empty! What your iterator is pointing to is undefined.
What you wanted to do is
int main(){
map<int, int> m;
m[0] = 0;
map<int, int>::iterator itr=m.begin();
cout << itr->first << " " << itr->second;
return 0;
}
Here you have not assign any value. and you can not change the key of itr->first of it. It can be read only. But you can edit itr->second.
map<int, int> m;
m[10]=0;
map<int, int>::iterator itr=m.begin();
itr->second=10;
cout << itr->first << " " << itr->second;

Iterator VS const_iterator, using it with distance()

Just a question, on the use of const_iterator vs just iterator. More specifically with the use of distance(). Below is some basic code that just craps out a list of "fav_games" that the user enters (earlier in the application). I wanted to also crap out the 'index' of the vector so as to print out a numbered list.
Partial Code:
int main()
{
vector<string> fav_games;
vector<string>::const_iterator iter; // const_iterator no worky with "distance()"
if (fav_games.size() > 0) {
cout << "\n\nCurrent game list: \n";
for (iter = fav_games.begin(); iter != fav_games.end(); ++iter)
{
cout << distance(fav_games.begin(), iter) << ". " << *iter << endl;
}
}
return 0;
}
My question is why, "const_iterator" will not work, where I am forced to use "iterator" instead. Looking for the 'theory' behind it. "distance()" appears to expecting and "iterator" -- not "const_iterator". ..Why?
Just for reference the compile error if I use "const_iterator":
Error 1 error C2782: 'iterator_traits<_Iter>::difference_type std::distance(_InIt,_InIt)' : template parameter '_InIt' is ambiguous z:\micah\c++\favgames\favgames\favgames.cpp 49 1 favgames
Thanks!
Try this instead:
vector<string>::const_iterator b, iter, e;
if (fav_games.size() > 0) {
cout << "\n\nCurrent game list: \n";
for (b = fav_games.begin(), iter = b, e = fav_games.end(); iter != e; ++iter)
{
cout << distance(b, iter) << ". " << *iter << endl;
}
}
distance has no problem with two const_iterator instances, or two iterator instances. Your mistake was in mixing them.
Still, making O(n) calls to distance is crazy. Just use a counter:
vector<string>::const_iterator iter, e;
size_t i;
if (fav_games.size() > 0) {
cout << "\n\nCurrent game list: \n";
for (i = 0, iter = fav_games.begin(), e = fav_games.end(); iter != e; (++iter), (++i))
{
cout << i << ". " << *iter << endl;
}
}
In C++11 and later, one may of course go one step further and avoid explicit use of the iterators at all:
if (fav_games.size() > 0) {
int i = 0;
cout << "\n\nCurrent game list: \n";
for (const string& game : fav_games)
{
cout << (i++) << ". " << game << endl;
}
}