Unordered_set non const iterator

For testing purposes, I created a bit of disordered_set and tried to iterate over the set. The set contains its own class:

class Student { private: int matrNr; string name; public: Student( const int& matrNr = 0, const string& name = "" ) : matrNr( matrNr ), name( name ) {} void setNr( const int& matrNr ) { this->matrNr = matrNr; } ... }; 

I inserted some elements and tried changing objects during iteration:

 unordered_set<Student, meinHash> meineHashTable; meineHashTable.emplace( 12, "Fred" ); meineHashTable.emplace( 22, "Barney" ); meineHashTable.emplace( 33, "Wilma" ); for (int i = 0; i < meineHashTable.bucket_count(); i++) { cout << "Bucketnummer: " << i << endl; unordered_set<Student, meinHash>::local_iterator iter; // not constant?!? if (meineHashTable.bucket_size( i ) > 0) { for (iter = meineHashTable.begin( i ); iter != meineHashTable.end( i ); iter++) { //const_cast<Student&>(*iter).setNr( 1234 ); //This does work iter->setNr( 1234 ); //This does not work } } else { cout << "An empty Bucket" << endl; } } 

I used local_iterator (and not const_local_iterator), but still I cannot change the objects. For some reason, an iterator refers to a persistent object.

My question now is: why is this so? If a normal iterator refers to a const object, then which one is different between a constant and a non-constant iterator?

Tested with VisualStudio 2013 and minGW.

Thanks in advance for any help :-)

EDIT: Hash Functor:

 struct meinHash { size_t operator()( const Student& s ) { return s.getNr(); } }; 

For future seekers on this topic who have the same question, here is an example output if you change matrNr with a strong one:

 const_cast<Student&>(*iter).setNr( 5 ); 

and try displaying it:

 unordered_set<Student, meinHash>::local_iterator iter = meineHashTable.find( 5 ); iter->display(); 

you can get something like:

Bucketnummer: 0

Empty bucket

Bucketnummer: 1

Matrikelnummer: 5

Name: Wilma

Bucketnummer: 2

Empty bucket

Bucketnummer: 3

Empty bucket

Bucketnummer: 4

Matrikelnummer: 5

Name: Fred

Bucket Weight: 5

Empty bucket

Bucketnummer: 6

Matrikelnummer: 5

Name: Barney

Bucket Weight: 7

Empty bucket

// Unwanted conclusion; -)

Matrikelnummer: -842150451

Name:

+7
c ++ c ++ 11 stl unordered-set
source share
3 answers

Both set and unordered_set have read-only keys. It’s easy to understand why this is so - if the key value changed, the data structure would force it to be submitted in the wrong place, and you can no longer find it.

In your example, suppose your hash function just returned the matrNr field. When the number of hashes changes, any 1234 lookup fails because nothing is stored in this hash bucket.

It would be possible to change the part of the object that is not used when creating the hash key, but this can make it difficult to find errors. The standards committee decided to eliminate this possibility by making all key const.

There are two ways to limit this restriction. The first is to separate the key from the value and use map or unordered_map instead. The second way is to remove an element from the set and insert it after changing it.

+11
source share

They determine the type a set<K> is const K , and for a map<K, T> it is pair<const K, T> ; also for unordered versions.

The iterator gives you access to value_type & , and const-iterator gives you const value_type & . As you can see, no type of iterator can "cancel" the key constant.

The reason the key is immutable is because it is an integral part of the underlying data structure; changing the key will require a nontrivial internal regrouping that will cause all kinds of problems (for example, nonzero computational complexity (to access the element!) and confusing ordering of the iterator).

+8
source share

unordered_set is a kind of data structure in which you cannot change an element without changing its location.

The non-constant iterator const is here because the STL protects you from such an obvious error.

If you want to change the unordered_set element, you need to remove it and add it again.

+1
source share

All Articles