简体   繁体   中英

unordered_map insertion fails

Note: I spent a lot of time trying to understand where my problem was coming from. However, as I was writting this post, I found the solution to my problem. Yet, I think it is still worth posting it. Besides, I am not 100% sure about my interpretation.


Here is my simplified problem with a simple example.

matrix is a sparse representation of a binary matrix . In this example I chose:

1 0
1 1
0 0
1 1

Each column is represented by a set<int> , giving the indices of the non-zero elements. For instance, for the first column of the matrix, lines of indices {0,1,3} are non-zero.

Objective of the algorithm :

  • For each column, find the highest index on that column (for instance, 3 for the first column), see if some column has already been inserted in the highestMapToColumns unordered_map.
  • Then in case a column is found, perform some operations on the column. No operations is performed here though, for the sake of clarity.
  • Finally, add the pair (highestIndex,column) to the unordered map highestMapToColumns .

`

// main.cpp
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <unordered_map>

using namespace std;

int main(int argc, char** argv) {

    vector<set<int>* >* matrix = new vector<set<int>* >();
    int column1[]= {0,1,3};
    int column2[]= {1,3};
    matrix->push_back(new set<int>(column1,column1+3));
    matrix->push_back(new set<int>(column2,column2+2));

    unordered_map<int, set<int>* > highestMapToColumns;
    int highestIndex = -1;
    for(vector<set<int>* >::iterator iteratorOnColumns = matrix->begin();iteratorOnColumns!=matrix->end();iteratorOnColumns++){
        while(true){
            if((*iteratorOnColumns)->size() == 0){
                highestIndex = -1;
                break;
            }
            highestIndex = *((*iteratorOnColumns)->rbegin());
            cout<<"Attempting to get index : "<<highestIndex<<endl;
            set<int>* toSubstract = highestMapToColumns[highestIndex];
            if (toSubstract != nullptr) {
                cout<<"Column found !!"<<endl;
                break;
            }else{ 
                cout<<"Column not found"<<endl;
                break;
            }
        }
        if(highestIndex != -1){
            cout<<"Attempting to insert index : "<<highestIndex<<endl;
            highestMapToColumns.insert(make_pair(highestIndex, (*iteratorOnColumns)));
        }
    }
    return 0;
}

Output of this program:

Attempting to get index : 3
Column not found
Attempting to insert index : 3
Attempting to get index : 3
Column not found
Attempting to insert index : 3

The first "Column not found" output is normal since nothing was inserted in the unordered map, however, the second is not.

I inserted a breakpoint and debugged at the moment of the insertion. I observed that *iteratorOnColumns was not a nullptr, and that it was pointing to the expected set<int> representing column1.

There must be a problem of insertion...

If I understood correctly :

The problem comes from the fact that when I attempt to find a column in the unordered_map highestMapToColumns , with the given key highestIndex , it inserts the pair (highestIndex,nullptr) , and this prevents any further insertion of the same key in the map...

Therefore highestMapToColumns , will only return the nullptr which is the first value inserted with the key highestIndex .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM