The traversal of Vector series--vector (STL algorithm, Vector iterator (do not judge in the loop not equal to end ()), operator[]) in the actual combat C + +

Source: Internet
Author: User

There are many ways to traverse a vector container, and it is also a good way to use them.

Traversal by index:

for (i0i<v.sizei++){    cout << v[i] << " ";}

Iterator traversal:

for (vInt::const_iterator iter = v.begin(); iter != v.end();iter++){    " ";}

Algorithm traversal:

copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));

Many books recommend using algorithms for traversal. A simple program was written to compare the above three methods:

#include <iostream>#include <vector>#include <iterator>#include <algorithm>#include <time.h>#include <windows.h>using namespace STD;typedef  vector<int>VInt;voidPrint_vec_operator (ConstVInt & V)//Method one, using subscript access{intI for(i =0; I<v.size (); i++) {cout<< V[i] <<" "; }cout<< Endl;}voidPrint_vec_iterator (ConstVInt &v)//Method two, with iterator access{ for(Vint::const_iterator iter = V.begin (); ITER! = V.end (); iter++) {cout<< *iter <<" "; }cout<< Endl;}voidPrint_vec_algorithm (ConstVInt &v)//Method Three, copy the contents of the container to the cout bound iterator{Copy (V.begin (), V.end (), ostream_iterator<int> (cout," "));cout<< Endl;}intMain () {VInt V;intI for(i =0; i<100000;    i++) {v.push_back (i); }intSTART_TIME_PRINT_VEC1 = GetTickCount (); Print_vec_operator (v);intEND_TIME_PRINT_VEC1 = GetTickCount ();intSTART_TIME_PRINT_VEC2 = GetTickCount (); Print_vec_iterator (v);intEND_TIME_PRINT_VEC2 = GetTickCount ();intSTART_TIME_PRINT_VEC3 = GetTickCount (); Print_vec_algorithm (v);intEND_TIME_PRINT_VEC3 = GetTickCount ();STD::cout<< (END_TIME_PRINT_VEC1-START_TIME_PRINT_VEC1) << Endl;STD::cout<< (END_TIME_PRINT_VEC2-START_TIME_PRINT_VEC2) << Endl;STD::cout<< (END_TIME_PRINT_VEC3-START_TIME_PRINT_VEC3) << Endl;return 0;}

When a vector initializes 10,000 elements, the three methods are comparable in efficiency and run several times at the same time:
Output:
1718 operator[]
1735 iterator
1797 algorithm

But when the veector is initialized to 100000, the efficiency of the three methods has a large gap:
Output:
20016 operator[]
32172 iterator
62468 algorithm

Write a vector and put a class in it:

#include <iostream>#include <vector>#include <iterator>#include <algorithm>#include <functional>#include <windows.h>classaaa{ Public:voidMakeFull2 () {}};intMain () {intNcount =1000000;STD:: vector< aaa* >VAAA; Vaaa.resize (ncount); for(inti =0; i < ncount; ++i) {Vaaa[i] =NewAAA; }//Time    intStart, end;//Test member function call (std::vector access mode)Start = GetTickCount (); size_t count = Vaaa.size (); for(size_t i =0; I < count;    ++i) vaaa[i]->makefull2 (); End = GetTickCount ();STD::cout<< End-start <<STD:: Endl;//Test member function call (STL algorithm mode)Start = GetTickCount ();STD:: For_each (Vaaa.begin (), Vaaa.end (),STD::mem_fun<void, aaa> (&AMP;AAA::MAKEFULL2)); End = GetTickCount ();STD::cout<< End-start <<STD:: Endl;//Test member function call (STL iterator mode)Start = GetTickCount ();STD:: vector< aaa* >:: Iterator itr_end = Vaaa.end (); for(STD:: vector< aaa* >:: Iterator ITR = Vaaa.begin (); ITR! = itr_end;    ++ITR) (*ITR)->makefull2 (); End = GetTickCount ();STD::cout<< End-start <<STD:: Endl;//Test member function call (STL iterator mode)Start = GetTickCount (); for(STD:: vector< aaa* >:: Iterator ITR = Vaaa.begin (); ITR! = Vaaa.end ();    ++ITR) (*ITR)->makefull2 (); End = GetTickCount ();STD::cout<< End-start <<STD:: Endl;return 0;}//output://313 oprator[]//62 Algorithm//422 iterator//922 iterator

Run again and the result is:
296
63
594
1672

This is the time to use algorithm+functional for the most efficient traversal.
A person feels that indexing is always more efficient than the way iterators do.

The following analysis of the two types of iterators, why the difference is not small:

This will take a look at the prototype of Std::vector::end ():

end() _NOEXCEPT{   //returnforend of mutable sequence    return (iterator(this->_Mylast(), &this->_Get_data()));}

Is that every time you judge ITR! = Vaaa.end (), you have to re-construct an iterator and return it, which of course reduces efficiency.

The traversal of Vector series--vector (STL algorithm, Vector iterator (do not judge in the loop not equal to end ()), operator[]) in the actual combat C + +

Related Article

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.