My Training Period: xx hours
This is a continuation from the previous Module, compiled usingVC++7.0 / .Net, win32 empty console mode application.g++ program example compilation is given at the end of this Module. The source code in text for this tutorial is available inC++ STL Iterator source code.
The C++ STL iterators abilities that should be acquired:
operator!= prototypes
const reverse_iterator<RandomIterator>& _Right );
const istream_iterator<Type, CharType, Traits, Distance>& _Left, const istream_iterator<Type, CharType, Traits, Distance>& _Right );
const istreambuf_iterator<CharType, Traits>& _Right );
Parameters
|
// iterator, operator!=
#include <iterator>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
int i;
vector<int> vec;
for(i = 1; i<=10; ++i)
vec.push_back(i);
vector<int>::iterator veciter;
cout<<"The vector vec data: ";
for(veciter = vec.begin(); veciter != vec.end(); veciter++)
cout<<*veciter<<" ";
cout<<endl;
// initializing reverse_iterators to the last element
vector<int>::reverse_iterator rvecpos1 = vec.rbegin(), rvecpos2 = vec.rbegin();
cout<<"The iterators rvecpos1 and rvecpos2 points to the first\n"
<<"element in the reversed sequence: "<<*rvecpos1<<endl;
cout<<"\nOperation: rvecpos1 != rvecpos2\n";
if(rvecpos1 != rvecpos2)
cout<<"The iterators are not equal."<<endl;
else
cout<<"The iterators are equal."<<endl;
rvecpos1++;
cout<<"\nThe iterator rvecpos1 now points to the second\n"
<<"element in the reversed sequence: "<<*rvecpos1<<endl;
cout<<"\nOperation: rvecpos1 != rvecpos2\n";
if(rvecpos1 != rvecpos2)
cout<<"The iterators are not equal."<<endl;
else
cout<<"The iterators are equal."<<endl;
return 0;
}
The return value is true if the iterator objects are equal; false if the iterator objects are not equal.
One iterator object is equal to another if they address the same elements in a container. If two iterators point to different elements in a container, then they are not equal.
// iterator, operator==
#include <iterator>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
int i;
vector<int> vec;
for(i = 11; i<15; ++i)
vec.push_back(i);
vector <int>::iterator veciter;
cout<<"The vector vec data: ";
for(veciter = vec.begin(); veciter != vec.end(); veciter++)
cout<<*veciter<<" ";
cout<<endl;
// initializing reverse_iterators to the last element
vector<int>::reverse_iterator rvecpos1 = vec.rbegin(), rvecpos2 = vec.rbegin();
cout<<"\nThe iterators rvecpos1 and rvecpos2 points\nto the first"
<<"element in the reversed sequence: "<<*rvecpos1<<endl;
cout<<"\nOperation: rvecpos1 == rvecpos2\n";
if(rvecpos1 == rvecpos2)
cout<<"The iterators are equal."<<endl;
else
cout<<"The iterators are not equal."<<endl;
rvecpos1++;
cout<<"\nThe iterator rvecpos1 now points to the second\n"
<<"element in the reversed sequence: "<<*rvecpos1<<endl;
cout<<"\nOperation: rvecpos1 == rvecpos2\n";
if(rvecpos1 == rvecpos2)
cout<<"The iterators are equal."<<endl;
else
cout<<"The iterators are not equal."<<endl;
return 0;
}
template<class RandomIterator>
bool operator<( const reverse_iterator<RandomIterator>& _Left, const reverse_iterator<RandomIterator>& _Right );
Parameter | Description |
_Left | An object of type iterator. |
_Right | An object of type iterator. |
Table 31.13 |
The return value is true if the iterator on the left side of the expression is less than the iterator on the right side of the expression; false if it is greater than or equal to the iterator on the right.
One iterator object is less than another if it addresses an element that occurs earlier in the container than the element addressed by the other iterator object.
One iterator object is not less than another if it addresses either the same element as the other iterator object or an element that occurs later in the container than the element addressed by the other iterator object.
// iterator, operator<
#include <iterator>
#include <vector>
#include <iostream>
int main()
{
using namespace std;
int i;
vector<int> vec;
for(i = 10; i<= 17; ++i)
vec.push_back(i);
vector<int>::iterator veciter;
cout<<"The initial vector vec is: ";
for(veciter = vec.begin(); veciter != vec.end(); veciter++)
cout<<*veciter<<" ";
cout<<endl;
// initializing reverse_iterators to the last element
vector<int>::reverse_iterator rvecpos1 = vec.rbegin(), rvecpos2 = vec.rbegin();
cout<<"The iterators rvecpos1 & rvecpos2 initially point\nto the "
<<"first element in the reversed sequence: "<<*rvecpos1<<endl;
cout<<"\nOperation: rvecpos1 < rvecpos2\n";
if(rvecpos1 < rvecpos2)
cout<<"The iterator rvecpos1 is less than the iterator rvecpos2."<<endl;
else
cout<<"The iterator rvecpos1 is not less than the iterator rvecpos2."<<endl;
cout<<"\nOperation: rvecpos1 > rvecpos2\n";
if(rvecpos1 > rvecpos2)
cout<<"The iterator rvecpos1 is greater than the iterator rvecpos2."<<endl;
else
cout<<"The iterator rvecpos1 is not greater than the iterator rvecpos2."<<endl;
cout<<"\nOperation: rvecpos2++;\n";
rvecpos2++;
cout<<"The iterator rvecpos2 now points to the second\n"
<<"element in the reversed sequence: "<<*rvecpos2<<endl;
cout<<"\nOperation: rvecpos1 < rvecpos2\n";
if(rvecpos1 < rvecpos2)
cout<<"The iterator rvecpos1 is less than the iterator rvecpos2."<<endl;
else
cout<<"The iterator rvecpos1 is not less than the iterator rvecpos2."<<endl;
cout<<"\nOperation: rvecpos1 > rvecpos2\n";
if(rvecpos1 > rvecpos2)
cout<<"The iterator rvecpos1 is greater than the iterator rvecpos2."<<endl;
else
cout<<"The iterator rvecpos1 is not greater than the iterator rvecpos2."<<endl;
return 0;
}
|
The return value is true if the iterator on the left side of the expression is less than or equal to the iterator on the right side of the expression; false if it is greater than the iterator on the right.
One iterator object is less than or equal to another if it addresses the same element or an element that occurs earlier in the container than the element addressed by the other iterator object.
One iterator object is greater than another if it addresses an element that occurs later in the container than the element addressed by the other iterator object.
// iterator, operator<=
#include <iterator>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
int i;
vector<int> vec;
for(i = 10; i<= 15; ++i)
vec.push_back(i);
vector <int>::iterator veciter;
cout<<"The vector vec data: ";
for(veciter = vec.begin(); veciter != vec.end(); veciter++)
cout<<*veciter<<" ";
cout<<endl;
vector <int>::reverse_iterator rvecpos1 = vec.rbegin()+1, rvecpos2 = vec.rbegin();
cout<<"The iterator rvecpos1 points to the\n"
<<"second element in the reversed sequence: "<<*rvecpos1<<endl;
cout<<"The iterator rvecpos2 points to the\n"
<<"first element in the reversed sequence: "<<*rvecpos2<<endl;
cout<<"\nOperation: rvecpos1<=rvecpos2\n";
if(rvecpos1<=rvecpos2)
cout<<"The iterator rvecpos1 is less than or\nequal to the iterator rvecpos2."<<endl;
else
cout<<"The iterator rvecpos1 is greater than\nthe iterator rvecpos2."<<endl;
cout<<"\nOperation: rvecpos2++\n";
rvecpos2++;
cout<<"The iterator rvecpos2 now points to the second\n"
<<"element in the reversed sequence: "<<*rvecpos2<<endl;
cout<<"\nOperation: rvecpos1 <= rvecpos2\n";
if(rvecpos1 <= rvecpos2)
cout<<"The iterator rvecpos1 is less than or\nequal to the iterator rvecpos2."<<endl;
else
cout<<"The iterator rvecpos1 is greater than\nthe iterator rvecpos2."<<endl;
return 0;
}
The return value is true if the iterator on the left side of the expression is greater than the iterator on the right side of the expression; false if it is less than or equal to the iterator on the right.
One iterator object is greater than another if it addresses an element that occurs later in the container than the element addressed by the other iterator object.
One iterator object is not greater than another if it addresses either the same element as the other iterator object or an element that occurs earlier in the container than the element addressed by the other iterator object.
// iterator, operator>
#include <iterator>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
int i;
vector<int> vec;
for(i = 10; i<= 15; ++i)
vec.push_back(i);
vector <int>::iterator veciter;
cout<<"The vector vec data: ";
for(veciter = vec.begin(); veciter != vec.end(); veciter++)
cout<<*veciter<<" ";
cout<<endl;
vector <int>::reverse_iterator rvecpos1 = vec.rbegin(), rvecpos2 = vec.rbegin();
cout<<"The iterators rvecpos1 & rvecpos2 point to the\n"
<<"second element in the reversed sequence: "<<*rvecpos1<<endl;
cout<<"\nOperation: rvecpos2 > rvecpos1\n";
if(rvecpos2 > rvecpos1)
cout<<"The iterator rvecpos2 is greater than\nthe iterator rvecpos1."<<endl;
else
cout<<"The iterator rvecpos2 is not greater than\nthe iterator rvecpos1."<<endl;
cout<<"\nOperation: rvecpos2++\n";
rvecpos2++;
cout<<"The iterator rvecpos2 now points to the second\n"
<<"element in the reversed sequence: "<<*rvecpos2<<endl;
cout<<"\nOperation: rvecpos2 > rvecpos1\n";
if(rvecpos2 > rvecpos1)
cout<<"The iterator rvecpos2 is greater than\nthe iterator rvecpos1."<<endl;
else
cout<<"The iterator rvecpos2 is not greater than\nthe iterator rvecpos1."<<endl;
return 0;
}
The return value is true if the iterator on the left side of the expression is greater than or equal to the iterator on the right side of the expression; false if it is less than the iterator on the right.
One iterator object is greater than or equal to another if it addresses the same element or an element that occurs later in the container than the element addressed by the other iterator object.
One iterator object is less than another if it addresses an element that occurs earlier in the container than the element addressed by the other iterator object.
// iterator, operator>=
#include <iterator>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
int i;
vector<int> vec;
for(i = 10; i<= 15; ++i)
vec.push_back(i);
vector <int>::iterator veciter;
cout<<"The vector vec data: ";
for(veciter = vec.begin(); veciter != vec.end(); veciter++)
cout<<*veciter<<" ";
cout<<endl;
vector <int>::reverse_iterator rvecpos1 = vec.rbegin(), rvecpos2 = vec.rbegin();
cout<<"The iterators rvecpos1 & rvecpos2 point to the\n"
<<"second element in the reversed sequence: "<<*rvecpos1<<endl;
cout<<"\nOperation: rvecpos2 >= rvecpos1\n";
if(rvecpos2 >= rvecpos1)
cout<<"The iterator rvecpos2 is greater than or\nequal to the iterator rvecpos1."<<endl;
else
cout<<"The iterator rvecpos2 is not greater than\nthe iterator rvecpos1."<<endl;
cout<<"\nOperation: rvecpos2++\n";
rvecpos2++;
cout<<"The iterator rvecpos2 now points to the second\nelement in the reversed sequence: "<<*rvecpos2<<endl;
cout<<"\nOperation: rvecpos2 >= rvecpos1\n";
if(rvecpos2 >= rvecpos1)
cout<<"The iterator rvecpos2 is greater than\nor equal to the iterator rvecpos1."<<endl;
else
cout<<"The iterator rvecpos2 is not greater than\nthe iterator rvecpos1."<<endl;
cout<<"\nOperation: rvecpos1 >= rvecpos2\n";
if(rvecpos1 >= rvecpos2)
cout<<"The iterator rvecpos1 is greater than\nor equal to the iterator rvecpos2."<<endl;
else
cout<<"The iterator rvecpos1 is not greater than\nthe iterator rvecpos2."<<endl;
return 0;
}
template<class RandomIterator>
reverse_iterator<RandomIterator> operator+( typename reverse_iterator<Iterator>::difference_type _Off,
const reverse_iterator<RandomIterator>& _Right );
Parameter | Description |
_Off | The number of positions the constreverse_iterator is to be offset. |
_Right | The const reverse_iterator that is to be offset. |
Table 31.14 |
The return value is the sum _Right + _Off.
// iterator, operator+
#include <iterator>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
int i;
vector<int> vec;
for(i = 10; i<=15; ++i)
vec.push_back(i);
vector<int>::iterator veciter;
cout<<"The vector vec data: ";
for(veciter = vec.begin(); veciter != vec.end(); veciter++)
cout<<*veciter<<" ";
cout<<endl;
vector<int>::reverse_iterator rveciter1 = vec.rbegin();
cout<<"\nThe iterator rveciter1 initially points to\n"
<<"the first element in the reversed sequence: "<<*rveciter1<<endl;
cout<<"\nOperation: rveciter1 = diff + rveciter1\n";
vector<int>::difference_type diff = 4;
rveciter1 = diff + rveciter1;
cout<<"The iterator rveciter1 now points to the fifth\n"
<<"element in the reversed sequence: "<<*rveciter1<<endl;
return 0;
}
The source code in text for this tutorial is available inC++ STL Iterator source code.
Acomplete C++ Standard Library documentation that includes STL.