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 70307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:42:37+00:00 2026-05-10T19:42:37+00:00

What would be the best way to check that a std::vector is sorted? Is

  • 0

What would be the best way to check that a std::vector is sorted? Is there something faster than a loop checking that v[i]<=v[i+1]? Is it faster/cleaner with iterators? Or is it actually better to just call sort every time (though the ‘v is already sorted’ case is quite common)?

We can safely assume the vector only contains PODs, usually floats and sometimes doubles and ints.

The size of the vector is non-trivial (usually a few thousands items) but not extreme (not gigabyte-sized).

  • in some instances we’ll sort the vector immediately afterwards, however there are other instances where we don’t (it’s an error case of our algorithm).
  • we already use a flag ‘IsSorted’ whenever possible.
  • 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. 2026-05-10T19:42:38+00:00Added an answer on May 10, 2026 at 7:42 pm

    Consider Multiple Cpu Cores

    It depends on your platform and number of items in the vector. You’d have to benchmark to find what’s best.

    It’s not possible to answer: Is there something faster than a loop checking that v[i]<=v[i+1] ?
    With: No.

    Because… computers now a days have multiple cpus/cores/hyperthreading. So, it may well be a lot quicker to exploit the parallism in the computer by spliting the work of checking to a number of threads, so each cpu can be checking a small range in parallel.

    It’s probably best to do this via a library function rather than implementing it yourself. New versions of libraries will exploit parallism. So, if you go for a std::sort you’ll probably find when you build against newer implementations of STL, they’ll do the operation in parallel for you without you having to worry about it. I don’t know if there are readily available versions of STL that do this already, but it’s worth sticking to the library functions so that when you upgrade to a version that does, this optimization is there for you without you needing to make any changes.

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

Sidebar

Ask A Question

Stats

  • Questions 86k
  • Answers 86k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It looks like IE does not support :visited styles on… May 11, 2026 at 5:17 pm
  • Editorial Team
    Editorial Team added an answer Nullable is a structure that contains a boolean hasValue and… May 11, 2026 at 5:17 pm
  • Editorial Team
    Editorial Team added an answer The easiest thing to do would be to include the… May 11, 2026 at 5:17 pm

Related Questions

Is there any way to check if a particular plugin is available? Imagine that
We have a ftp system setup to monitor/download from remote ftp servers that are
In a C++ application that can use just about any relational database, what would
I have a program that will analyzes source code. It can recursively go through

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.