Search code examples
c++stdset

Accessing an std::set via a dereferenced iterator


Consider:

#include <stdio.h>
#include <set>

void printset(std::set<int>& Set) {
    for (std::set<int>::iterator siter = Set.begin(); siter != Set.end(); ++siter) {
        int val = *siter;
        printf("%d ", val);
    }
}

void printsetofset0(std::set<std::set<int>>& SetofSet) {
    for (std::set<std::set<int>>::iterator siter = SetofSet.begin(); siter != SetofSet.end(); ++siter) {
        std::set<int> Set = *siter;
        printset(Set);
    }
}

void printsetofset1(std::set<std::set<int>>& SetofSet) {
    for (std::set<std::set<int>>::iterator siter = SetofSet.begin(); siter != SetofSet.end(); ++siter) {
        printset(*siter);//this line gives error
    }
}   

printsetofset1 with the printset(*siter); gives error:

<source>: In function 'void printsetofset1(std::set<std::set<int> >&)':
<source>:20:34: error: binding reference of type 'std::set<int>&' to 'const std::set<int>' discards qualifiers
   20 |                         printset(*siter);
      |                                  ^~~~~~
<source>:4:38: note:   initializing argument 1 of 'void printset(std::set<int>&)'
    4 |         void printset(std::set<int>& Set) {
      |                       ~~~~~~~~~~~~~~~^~~
Compiler returned: 1

See Godbolt Link here.

printsetofset0 with the lines: std::set<int> Set = *siter; printset(Set);

compiles and works just fine.

What is the reason why one printsetofset0 works, while seemingly functionally equivalent (and shorter) preintsetofset1 does not work?


Solution

  • The elements, which the iterator points to, are constant. You can read it here in the Notes: Because both iterator and const_iterator are constant iterators (and may in fact be the same type), it is not possible to mutate the elements of the container through an iterator returned by any of these member functions.. But, passing a reference to your printset function would violate this property.

    Internally, sets allow fast access via index structures. Changing the elements would require to reorder the set (internally).

    You can solve the situation by adding a const modifier to the parameter of your printset function:

    void printset(const std::set<int>& Set)