#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>
iterator_t
const_iterator_t
const_iterator_t cbegin()
const_iterator_t cend()
under what conditions will be called cbegin() and cend()
for what purpose they exist in std::list, etc. types
Find more questions by tags C++
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
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