Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7531211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:06:51+00:00 2026-05-30T05:06:51+00:00

I have a sequence of double (with no duplicates) and I need to sort

  • 0

I have a sequence of double (with no duplicates) and I need to sort them. Is filling a vector and then sorting it faster than inserting the values in a set?

Is this question answerable without a knowledge of the implementation of the standard library (and without a knowledge of the hardware on which the program will run) but just with the information provided by the C++ standard?

#include <vector>
#include <set>
#include <algorithm>
#include <random>
#include <iostream>

std::uniform_real_distribution<double> unif(0,10000);
std::default_random_engine re;

int main()
{
    std::vector< double > v;
    std::set< double > s;
    std::vector< double > r;
    size_t sz = 10;
    for(size_t i = 0; i < sz; i++) {
        r.push_back( unif(re) );
    }

    for(size_t i = 0; i < sz; i++) {
        v.push_back(r[i]);
    }
    std::sort(v.begin(),v.end());

    for(size_t i = 0; i < sz; i++) {
        s.insert(r[i]);
    }

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:06:52+00:00Added an answer on May 30, 2026 at 5:06 am

    From the C++ standard, all we can say is that they both have the same asymptotic complexity (O(n*log(n))).

    The set may be faster for large objects that can’t be efficiently moved or swapped, since the objects don’t need to be moved more than once. The vector may be faster for small objects, since sorting it involves no pointer updates and less indirection.

    Which is faster in any given situation can only be determined by measuring (or a thorough knowledge of both the implementation and the target platform).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a boost::mpl::vector with N elements, say: typedef boost::mpl::vector<int,float,double,short,char> my_vector; I wish to
I have the following code: IEnumerable<KeyValuePair<T, double>> items = sequence.Select(item => new KeyValuePair<T, double>(item,
I have a sequence which varies in length e.g. something like: items <- 1:4
I have a sequence like [0, 1, 0, 1, 0, 1, 0] and I
I have a sequence of maps. ;; input [{:country MX, :video 12345, :customer cid1}
I have a sequence of k nodes N1 , N2 , N3 , ...
I have a sequence. a1 = 1 - cos(x); ai = a1 + (-1)^(i-1)
suppose i have a sequence, called TEST_SEQ what would be the correct way of
Suppose I have a sequence x1,x2,x3.....xn, and I want to find the longest continuous
I have a strange problem with IE. I have a sequence of forms on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.