this is just an example not actual code if you were using iterator and list. You were using ostream and you wanted to cout statement how would you do that.
1 2 3 4 5 6 7 8 9
void example(std::ostream& os)
{
std::list<example>::iterator dummy;
for (dummy=example.begin(); dummy !=example.end(); ++dummy)
{
std::advance(dummy,1);
os << *dummy << "\t";//here what im talking about
}
}
has the ability to iterate through the elements of that range using a set of operators (with at least the increment (++) and dereference (*) operators).
and continues with comparing pointer and iterator ...
Perhaps you should write a compilable code and test what that *dummy does in practise?
While you are there, please consider the ramifications of having both ++dummy on line 4 and std::advance(dummy,1) on line 6.
i have tested just want to understand what wrong because i do test it i get an error at line 7 even thought this is just example. the ostream& os is the problem
#include <iostream>
#include <list>
#include <iterator>
struct example { int value ; /* ...... */ };
std::ostream& operator<< ( std::ostream& stm, example ex )
{ return stm << '{' << ex.value << '}' ; }
std::list<example> my_list { {12}, {83}, {54}, {76}, {21}, {90}, {64}, {38}, {47}, {61} } ;
void example( std::ostream& os )
{
// note: the name of the function'example' hides the name of the type 'example'
// std::list<example>::iterator dummy; // *** error: 'example' is not the name of a type
std::list< struct example >::iterator dummy ; // fixed: use an extended type specifier
// iterate through the list using iterators (print every element in the list)
for( dummy = my_list.begin(); dummy != my_list.end(); ++dummy ) os << *dummy << ' ' ;
os << '\n' ;
// iterate through the list using iterators (print every element in the list)
for( dummy = my_list.begin(); dummy != my_list.end(); std::advance(dummy,1) ) os << *dummy << ' ' ;
os << '\n' ;
// iterate through the list using iterators (let the compiler figure out the type of the terator)
for( auto iter = my_list.begin(); iter != my_list.end(); ++iter ) os << *iter << ' ' ;
os << '\n' ;
// iterate through the list using iterators (let the compiler figure out the type of the terator)
for( auto iter = my_list.begin(); iter != my_list.end(); iter = std::next(iter) ) os << *iter << ' ' ;
os << '\n' ;
// iterate through the list using iterators (let the compiler figure out how to write the loop)
for( constauto& ex : my_list ) os << ex << ' ' ;
os << '\n' ;
// iterate through the list using iterators (print every element in an even position in the list)
for( auto iter = my_list.begin(); iter != my_list.end(); ++iter )
{
os << *iter++ << " " ;
if( iter == my_list.end() ) break ;
}
os << '\n' ;
// iterate through the list using iterators (print every element in an even position in the list)
for( auto iter = my_list.begin(); iter != my_list.end(); iter = std::next(iter) )
{
os << *iter << " " ;
std::advance(iter,1) ;if( iter == my_list.end() ) break ;
}
os << '\n' ;
// iterate through the list using iterators (print every element in an odd position in the list)
for( auto iter = my_list.begin(); iter != my_list.end(); ++iter )
{
++iter ;if( iter == my_list.end() ) break ;
os << " " << *iter << ' ' ;
}
os << '\n' ;
// iterate through the list using iterators (print every element in an odd position in the list)
for( auto iter = my_list.begin(); iter != my_list.end(); std::advance(iter,1) )
{
iter = std::next(iter) ;if( iter == my_list.end() ) break ;
os << " " << *iter << ' ' ;
}
os << '\n' ;
}
int main()
{
example(std::cout) ;
}