Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::find_end

 

std::find_end is an STL algorithm to find the last occurrence of a sequence in a container.

 

#include <algorithm>
#include <cassert>
#include <vector>

int main()
{
  std::vector<int> v;
  v.push_back(1);
  v.push_back(0);
  v.push_back(0);
  v.push_back(2);
  v.push_back(0);
  v.push_back(0);
  v.push_back(3);

  std::vector<int> sequence;
  sequence.push_back(0);
  sequence.push_back(0);

  assert(std::find_end(
    v.begin(),v.end(),
    sequence.begin(),sequence.end())
    != v.end()
    && "Assume sequence is found");

  assert(std::find_end(
    v.begin(),v.end(),
    sequence.begin(),sequence.end())
    != v.begin()
    && "Assume sequence is not found at the beginning");

  assert(* (std::find_end(
    v.begin(),v.end(),
    sequence.begin(),sequence.end()) - 1 )
    == 2
    && "Assume there is a 2 before the found sequence");
}

 

 

 

 

 

External links

 

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict