STL vector+sort Sorting and multiset/multimap sorting comparison

Source: Internet
Author: User

This article is collected and collated by www.169it.com

In the STL Library of C + +, sorting can be done by saving all elements to the vector, then sorting by the sort algorithm, or by Multimap implementation when inserting elements. When sorting by vector+sort, all elements need to be stored in the vector container first, and sort will need to be taken out of the list before sorting. The Multimap is implemented as a red-black tree, so the elements are sorted in the process of insertion. So which sort is faster?

Here's a test program:


if(gettimeofday(&tv, NULL) != 0)  return0.0;    returntv.tv_sec + tv.tv_usec / 1000000.0; } struct Score {    string name;    doublescore;    booloperator <( constScore& right)  const{      returnscore < right.score;    } }; intmain( intargc,  char ** argv) {    vector<Score> src;    for( inti = 0; i < 10000000; i++) {      intnum =  rand ();      charbuf[32];      sprintf (buf,  "%d" , num);      Score score = { buf, num };      src.push_back(score);    }    {      doublestime =  time ();      vector<Score> res;      for(vector<Score>::const_iterator it = src.begin();           it != src.end(); ++it) {        res.push_back(*it);      }      sort(res.begin(), res.end());      doubleetime =  time ();      printf ( "vector: %f\n" , etime - stime);    }    {      doublestime =  time ();      multiset<Score> res;      for(vector<Score>::const_iterator it = src.begin();           it != src.end(); ++it) {        res.insert(*it);      }      doubleetime =  time ();      printf ( "multiset: %f\n" , etime - stime);    }    return0; }

The result of the program operation is:

1 2 3           time vector   4.776060 multiset 10.761023

In this test, the vector+sort sort is much faster than the multiset (Multimap implementation-based multiset). Fast sorting is the fastest sorting algorithm in all currently known sorting algorithms, so it is faster than multiset based on heap ordering.

Before this test results, most people would have no doubt that the multiset sort would be faster. This is also for a reason, fast sorting speed, although fast, but in the actual operation, it requires a large number of copies of elements, its copy operation of the time complexity of O (Nlogn), and based on the red-black tree multiset in the process of sorting avoids the copy of the element. If the memory footprint of the element is larger, the multiset sort will be faster than Vector+sort. To test this result, the structure in the above test program is modified to:

struct   score {    string name1;    string name2;    string Name3;    string name4;    Double   score;    BOOL   operator < ( const   score& right)   Const   {      return   Score < Right.score;    } };
1 2 3 4 5 6 7 8 9

Then recompile and run the test program with the test result:

1 2 3           time vector   12.955739 multiset 11.368364

The results of this test are consistent with our expectations.

In short, when we use the STL sorting algorithm, we need to make the appropriate choice according to the different element constructs, if all is the relatively simple element, then is suitable chooses the vector+sort to carry on the sorting, for the string composition occupies the larger space the complex element, should use the multiset. If the total number of sorted elements is relatively small, select either one.

    • Article Source: STL vector+sort Sorting and multiset/multimap sorting comparison


STL vector+sort Sorting and multiset/multimap sorting comparison

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.