mercredi 15 juillet 2020

Iterators and const string reference prints garbage

I'm trying to write an iterator and implemented the operator *:

std::pair<std::pair<int, int>, const Container&> operator*() const { 
    return std::make_pair(std::make_pair(1, 1), "HELLO");
}

where Container is a template parameter, in our case an std::string.

However, when trying to run the following program (given that "view" is the iterated container):

for (const auto &container_tuple : view) {
        std::string x = std::to_string(std::get<0>(container_tuple.first));
        std::string y = std::to_string(std::get<1>(container_tuple.first));
        std::cout << "X: " << x << ", " << std::endl;
        std::cout << "Y: " << y << ", " << std::endl;
        std::cout << "String: " << container_tuple.second << std::endl;
    }

The output is:

X: 1, 
Y: 1, 
String:

Any idea why is this happening?

Aucun commentaire:

Enregistrer un commentaire