Monday 15 June 2015

c++ - Merits of std::find -


On the container's using std :: find of C ++ 11 There is no benefit for method

  • In the case of std :: vector (which has no search

  • In the case of std :: find uses some smart algorithm or simply the simple way to hover over each element Std :: map It looks like you need to pass with a std :: pair , which is the standard_type < / Code> of std :: map code. Usually you want to search for a key or a mapped element, it does not seem very useful. < Li>

    std :: list or std :: set or std :: unordered_set ? < / Ul>

    This can not be done because the vectors are not ordered. There is no other way to find an element in a random vector than with a linear search with O (n) complexity.

    On the other hand, the sequence container is a search () member Do not offer jobs, so you can not use it. In the case of Std :: map in

    you think that you need to pass with std :: pair, which is the value_type of the std :: map. Usually you want to search for a key or mapped element, it does not seem very useful.

    In fact, here you should use the find member job, which guarantees better complexity (O (log n)).

    In general, when a container exposes a member function with a member similar to a normal algorithm, this is because the member function does the same thing, but provides better complexity guarantee < / P>

    what about the other containers such as studs :: list or std :: set or std :: unordered_set

    simply std :: vector , std :: list is not a sorted container - hence the same conclusion applies Land.

    std :: set and Std :: unordered_set , instead, you should use the find () member function Those who guarantee better complexity (O (log n) and average o (1) respectively).

No comments:

Post a Comment