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.