Iterators (c + +)

Source: Internet
Author: User

/* #include <iostream>
#include <vector>
using namespace Std;
int main ()
{
Vector<int> Vecint;
Vecint. Push_back (1);
Vecint. Push_back (2);
Vecint.push_back (3);
int size = Vecint.size ();

Vector<int> VecInt2 (10);

Vector<int> VecInt3 (5, 28);

Vector<int> vecInt4 (VECINT3);
【,)
/ector<int> vecInt5 (Vecint.begin (), Vecint.begin () + 1);
Vector<int> vecInt5 (Vecint.begin (), Vecint.begin () +2);
Vecint.resize (10);//re-expansion of 10 data lengths.

Vector.resize (10);//Reassign 10 Lengths
return 0;

Several uses of

}*/
//iterators:
#include <iostream>
#include <windows.h>
#include <algorithm>// Sort ()
#include <vector>
using namespace std;
void Printfvalue (int& a)
{
 cout << a << endl;
}
BOOL Mysort (int a,int b)
{
 return a > b;//from large to small sort
}
int main ()
{  
  Vector<int> vecint3;//This is how the container is defined.
 vector<int>::iterator itint;//is the way to define the iterator's
 itint = Vecint3.begin ();//First element
 itint = Vecint3.end ();  //The next
 vecint3.push_back (1) that is considered to be the last element from logic;
 vecint3.push_back (2);
 vecint3.push_back (3);
 vecint3.push_back (4);

int num = count (Vecint3.begin (), Vecint3.end (), 28);//Find 28 in Vecint3.begin () to Vecint3.end () range.
cout << num<<endl;
Vector<int>::iterator Itfind = Find (Vecint3.begin (), Vecint3.end (), 2);//function as above, can not find an error
int a = *itfind;
cout << a <<endl<< "**************************" << Endl;

Sort (Vecint3.begin (), Vecint3.end (), mysort);//Sort function (global)

For_each (Vecint3.begin (), Vecint3.end (), printfvalue);//number output in this interval
for (Vector<int>::iterator it = Vecint3.begin (); It! = Vecint3.end (); it++)//use iterators to output 1 to 4
{
cout << *it << endl;//iterator itself is a pointer
}
for (int i = 0; i < vecint3.size (); i++)//Use subscript output 1 to 4
{
cout << Vecint3[i] << Endl;
}


Vector<int>::const_iterator Constit;
Constit = Vecint3.begin ();

Vector<int>::reverse_iterator rlt;//is a reverse iterator
RLT = Vecint3.rend ();
RLT = Vecint3.rbegin ();

GetChar ();//System ("pause");
return 0;

}

Iterators (c + +)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.