In practice, the vector series in c ++ -- vector traversal (stl algorithm, vector iterator (do not judge not equal to end () in the loop), operator [])

Source: Internet
Author: User

In practice, the vector series in c ++ -- vector traversal (stl algorithm, vector iterator (do not judge not equal to end () in the loop), operator [])

There are many ways to traverse a vector container.

Index traversal:

for (i = 0; i
  

Iterator traversal:

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

Algorithm traversal:

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

In many books, algorithms are recommended for traversal. I wrote a simple program to compare the above three methods:

# Include  # Include  # Include  # Include  # Include  Using namespace std; typedef vector  VInt; void print_vec_operator (const vInt & v) // method 1. use subscript to access {int I; for (I = 0; I  (Cout, ""); cout <endl ;}int main () {vInt v; int I; for (I = 0; I <100000; I ++) {v. push_back (I);} int start_time_print_vec1 = GetTickCount (); print_vec_operator (v); int success = GetTickCount (); print_vec_iterator (v ); int success = GetTickCount (); int start_time_print_vec3 = GetTickCount (); success (v); int success = 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 <(response-start_time_print_vec3) <endl; return 0 ;}       

When a vector initializes 10000 elements, the efficiency of the three methods is comparable, and the running time is almost the same:
// Output:
// 1718 operator []
/// 1735 iterator
/// 1797 algorithm

However, when the veector is initialized to 100000, the efficiency of the three methods is significantly different:
// Output:
// 20016 operator []
/// 32172 iterator
/// 62468 algorithm

Write another vector to put a class:

# Include  # Include  # Include  # Include  # Include  Class AAA {public: void MakeFull2 () {}}; int main () {int nCount = 1000000; std: vector <AAA *> vAAA; vAAA. resize (nCount); for (int I = 0; I <nCount; ++ I) {vAAA [I] = new AAA;} // time int start, end; // test the member function call (std: vector subscript access method) 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 the member function call (STL algorithm) start = GetTickCount (); std: for_each (vAAA. begin (), vAAA. end (), std: mem_fun  (& AAA: MakeFull2); end = GetTickCount (); std: cout <end-start <std: endl; // test the member function call (STL iterator method) 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 the member function call (STL iterator method) 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 the command again and the result is:
// 296
// 63
// 594
// 1672

In this case, using algorithm + functional for traversal is the most efficient.
I personally think that the subscript index method will always be more efficient than the iterator method.

The following analyzes the differences between the two iterator methods:

Now let's take a look at the prototype of std: vector: end:

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

It is to judge itr every time! = VAAA. end (), we need to re-construct an iterator and return the result, which of course reduces the efficiency.

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.