1

I have a class simplified as much as possible and for demonstration purposes. It looks as simple as:

#include <iostream>
#include <vector>


class Simple
{
private:
    std::vector<std::size_t> indices;
    std::vector<int> values;
public:
    void insert(std::size_t index, int value)
    {
        indices.push_back(index);    
        values.push_back(value);
    }
    int at(std::size_t index)
    {
        return values[indices[index]];    
    }
};

int main()
{
    Simple s;
    s.insert(10, 100);
    std::cout << s.at(10) << std::endl;
    return 0;
}

What I wat to achieve is to iterate over elements of this container and get std::pair at each iteration, whose first element would be a value from indices member and whose second element would be a value from values member. Something like:

Simple s;
s.insert(10, 100);
for (std::pair<std::size_t, int> node : s)
{
    std::cout << node.first << " " << node.second << std::endl; // expect to print 10 100
}

I'm really new to iterators. I know how to iterate through standard containers and get their values. I even know how to iterate through my Simple container and get value from values member at each iteration. I could do it like so:

//new member functions in Simple class
auto begin()
{
    std::begin(values);
}

auto end()
{
    std::end(values);
}

But I do not know how to create some new data type at each iteration and return it to the client code.

Jacobian
  • 10,122
  • 29
  • 128
  • 221
  • Your example code in `main` will produce undefined behavior. It looks like you're trying to make a map but not quite getting it right. Is there some reason you're not just using an STL map? – scohe001 Feb 05 '18 at 20:55
  • I know that it looks like map, but as I said, I made such a simple example just for demonstration purposes. – Jacobian Feb 05 '18 at 20:56
  • It this what you are looking for, maybe: https://stackoverflow.com/questions/8511035/sequence-zip-function-for-c11 – Rakete1111 Feb 05 '18 at 21:00
  • @Rakete1111. Something like that, but without boost dependencies. – Jacobian Feb 05 '18 at 21:01
  • The [range](https://github.com/ericniebler/range-v3/) based answer isn't boost. It may even be in your standard library as `std::experimental::ranges` – Caleth Feb 05 '18 at 21:16

1 Answers1

1

Notice that your trying to iterate through a class that doesn't have any kind of abstraction. What I mean is that you can only iterate through it if you "make it" a vector, or a different data structure that you could iterate through.

class Simple : public vector<std::pair<std::size_t, int>> { /*...*/ };

Once you declare a class with such syntax you would be able to handle it as a vector of pairs. You will have the advantage of declaring your own custom methods as well.

You can now:

Simple simple;
simple.push_back({10, 100});
for (auto element : simple) {
    std::cout << element.first << " " << element.second << std::endl;
}

I recommend you some reading in this kind of implementations. Sometimes it can save a lot of work! And you know what they say, why to reinvent the wheel?

Remember that the method at(10) will return the element on position 10 of your vector. In your example you are trying to read from an out_of_range position.

Maybe you're interested in map, which contains a key and a value. From your example, it doesn't seem that you are trying to keep your data structure sorted. If you are, you can use a map instead of an unordered_map. You can retrieve a value from a key by using the find method:

#include <unordered_map>

/* ... */

unordered_map<std::size_t, int> simple;    
simple.insert({10, 100});

auto it = simple.find(10);
if (it != simple.end()) {
    std::cout << "Found value from key: " << *it << std::endl;
} else {
    std::cout << "Your map doesn't contain such key!" << std::endl;
}

Notice that a map does not allow multiple keys with the same value. But a multimap does.

  • Probably, I did not stress it too much, but my example is not about map, multimap etc. I provided an example just for illustration purposes. I could have three vectors or more inside my Simple class and if I knew how to get a pair from iterator, I could generalize it and return a tuple. So, my question is purely about iterator. – Jacobian Feb 06 '18 at 06:53