Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • 3
    Good answer! Find is always o(n). lower_bound is o(log(n)) if used with random-access iterators. Commented Jul 8, 2009 at 19:54
  • 55
    Sorting is O(nlogn) though, so it's worth only if you're doing more than O(logn) searches. Commented Jun 15, 2014 at 0:48
  • 12
    @liori True it depends on your usage patterns. If you only need to sort it once, then repeatedly do many searches it can save you. Commented Jun 17, 2014 at 16:24
  • 1
    @Brian Neal, sorting a large vector is worth if there has to be many element searches on it. Sorting will be O(nlogn) and O(n) will be better if one has to find an element only once :) Commented Mar 11, 2018 at 18:17
  • 1
    Try to use std::set<> together with find() indeed. Or use std::map<> depending on your needs. Commented May 9, 2023 at 20:04