How to add support for range-based loop in your class?

Hello, everyone!

Asking knowledgeable and experienced in C++11 to help.

Wrote my first "Hello World" to learn how to add in their types of support for-each loop. Here is the code:

#include <iostream>
#include <list>


struct Foo
{
 int f1;
 double f2;
 short f3;
};

class RangeBasedLoopExample
{
public:
 typedef std::list<foo>::iterator iterator_t;
 typedef std::list<foo>::is assigned to a const_iterator const_iterator_t;

public:
 RangeBasedLoopExample() {
 Foo foo1 = {1, 2.5, 3};
 Foo foo2 = {2, 3.6, 4};
fooList.push_back(foo1);
fooList.push_back(foo2);
}

 iterator_t begin() {
 std::cout << "[1] - RangeBasedLoopExample::begin()\n";
 return fooList.begin();
}

 iterator_t end() {
 std::cout << "[2] - RangeBasedLoopExample::end()\n";
 return fooList.end();
}

 const_iterator_t begin() const {
 std::cout << "[3] - RangeBasedLoopExample::begin() const\n";
 return fooList.begin();
}

 const_iterator_t end() const {
 std::cout << "[4] - RangeBasedLoopExample::end() const\n";
 return fooList.end();
}

 const_iterator_t cbegin() {
 std::cout << "[5] - RangeBasedLoopExample::cbegin()\n";
 return fooList.cbegin();
}

 const_iterator_t cend() {
 std::cout << "[6] - RangeBasedLoopExample::cend()\n";
 return fooList.cend();
}

private:
 std::list<foo> fooList;
};


void print1(RangeBasedLoopExample& b)
{
 for (const auto& foo : b)
 std::cout << foo.f1 << ';' << foo.f2 << ';' << foo.f3 << '\n';
}

void print2(const RangeBasedLoopExample& b)
{
 for (const auto& foo : b)
 std::cout << foo.f1 << ';' << foo.f2 << ';' << foo.f3 << '\n';
}

int main(int argc, char* argv[])
{
print1(RangeBasedLoopExample());
print2(RangeBasedLoopExample());
 return 0;
}</foo></foo></foo></list></iostream>


However, until I know when and under what conditions will be called cbegin() and cend() for what purpose they exist in std::list, etc. types? When they need to add in your own types?
September 19th 19 at 12:17
1 answer
September 19th 19 at 12:19
Solution
iterator_t
const_iterator_t

The suffix _t is reserved, it is better not to use it in your code.

const_iterator_t cbegin()
const_iterator_t cend()

In standard containers, these methods -- const.

under what conditions will be called cbegin() and cend()

I think that only explicitly by the programmer by hand.

for what purpose they exist in std::list, etc. types

So you can easily get a const iterator from a non-const container.

1.
So I mean right in this example, added support for ranged-based loop?

2.
>>_t suffix is reserved, it is better not to use it in your code.
Never heard of it. Can you reveal more? - Oceane_Von84 commented on September 19th 19 at 12:22
> So I mean right in this example, added support for ranged-based loop?
Yes, to support you want to implement only begin() and end().

> Never heard of it. Can you reveal more?
lists.gnu.org/archive/html/bug-gnulib/2009-11/msg0... - Carrol commented on September 19th 19 at 12:25

Find more questions by tags C++