Finding Items

The Standard Library has a wide range of functions to search for items:

  • The min_element function will return an iterator to the smallest item in a range and the max_elementfunction will return an iterator to the maximum item. These functions are passed iterators for the range of items to check and a predicator that returns a bool from the comparison of two items. If you don't provide a predicator, the < operator for the type will be used.
        vector<int> planck{ 6,6,2,6,0,7,0,0,4,0 }; 
auto imin = min_element(planck.begin(), planck.end());
auto imax = max_element(planck.begin(), planck.end());
cout << "values between " << *imin << " and "<< *imax << endl;

  • The imin and imax values are iterators, which is why they are dereferenced to get the value. If you want to get the minimum element and the maximum element in one go, you can call the minmax_element, which will return a pair object with iterators to these items. As the name suggests, the adjacent_find function will return the position of the first two items that have the same value (and you can provide a predicate to determine what same value means). This allows you to search for duplicates and get the position of those duplicates.
        vector<int> vec{0,1,2,3,4,4,5,6,7,7,7,8,9}; 
vector<int>::iterator it = vec.begin();

do
{
it = adjacent_find(it, vec.end());
if (it != vec.end())
{
cout << "duplicate " << *it << endl;
++it;
}
} while (it != vec.end());
  • This code has a sequence of numbers in which there are some numbers duplicated that are next to each other. In this case there are three adjacent duplicates: 4 followed by 4, and the sequence 7,7,7 is 7 followed by 7, and 7 followed by 7. The do loop calls adjacent_find repeatedly until it returns the end iterator, indicating that it has searched all items. When a duplicate pair is found, the code prints out the value and then increments the start position for the next search.
  • The find function searches a container for a single value, and returns an iterator to that item or the end iterator if the value cannot be found. The find_if function is passed a predicate and it returns an iterator to the first item it finds that satisfies the predicate; similarly, the find_if_not function finds the first item that does not satisfy the predicate.
  • There are several functions that are given two ranges, one is the range to search and the other has the values to look for. The different functions will either look for one of the items in the search criteria or it will look for all of them. These functions use the == operator for the type that the container holds or a predicate.
  • The find_first_of function returns the position of the first item that it finds in the search list. The search function looks for a specific sequence, and it returns the first position of the whole sequence, whereas the find_end function returns the last position of the entire search sequence. Finally, the search_n function looks for a sequence that is a value repeated a number of times (the value and the repeat are given) in a specified container's range.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset