boost::range::find_end

References

Headers

boost::range::find_end is available by including any of the following headers:

  • boost/range/algorithm/find_end.hpp or
  • boost/range/algorithm.hpp

Examples

search.cpp

#include <iostream>
#include <locale>
#include <boost/range/algorithm.hpp>


const std::string haystack = "ABC abc abc";
const std::string needle = "abc";


bool iequals(char lhs, char rhs) {
    std::locale loc;
    return std::toupper(lhs, loc) == std::toupper(rhs, loc);
}


void search_demo() {
    // Return an iterator to the first occurance of one range inside another.
    // If the value is not found, the end iterator is returned.
    auto it = boost::range::search(haystack, needle);
    auto iit = boost::range::search(haystack, needle, iequals);

    std::cout << "First occurrence of \"abc\": " << (it - haystack.begin()) << std::endl;
    std::cout << "First occurrence of \"abc\" (case-insensitive): "
              << (iit - haystack.begin()) << std::endl;
}

void find_end_demo() {
    // Return an iterator to the last occurance of one range inside another.
    // If the value is not found, the end iterator is returned.
    auto it = boost::range::find_end(haystack, needle);
    auto iit = boost::range::find_end(haystack, needle, iequals);

    std::cout << "Last occurrence of \"abc\": " << (it - haystack.begin()) << std::endl;
    std::cout << "Last occurrence of \"abc\" (case-insensitive): "
              << (iit - haystack.begin()) << std::endl;
}

int main() {
    // Note that search() and find_end() have corresponding functionality.
    // This is not to be confused with find_first_of().
    search_demo();
    find_end_demo();

    return 0;
}

Output:

First occurrence of "abc": 4
First occurrence of "abc" (case-insensitive): 0
Last occurrence of "abc": 8
Last occurrence of "abc" (case-insensitive): 8

 

Boost Range for Humans

This reference is part of Boost Range for Humans. Click the link to the overview.