2

Already asked this question and received answers about c++ STL but what about boost?

This is a question about boost Finders. If you have a link to describable boost library implementations I would appreciate it to make hunting the boost library for practical applications easier.

My question is which boost finder most applies to lastIndexOf?

Community
  • 1
  • 1
Mushy
  • 2,535
  • 10
  • 33
  • 54
  • Can you explain your use-case. Why do you need a finder instance instead of using standard (or boost) explicit function to find the last index of a character or pattern? If you just want a function that works for finding the last index of a pattern, use the solution @sftrabbit posted. – Vite Falcon Mar 26 '13 at 17:45
  • @ViteFalcon Yes, I am looking for a function in boost that finds last index of a pattern and opted to use solution posted. – Mushy Mar 26 '13 at 17:54

1 Answers1

2

Well first off, the simplest option if you're going to search for the last occurrence of a substring is to use std::string::rfind:

std::string str = "Hello, World!";
int index = str.rfind("o");

If you need to use Boost because you want it to work on generic ranges, use boost::algorithm::find_last. It takes two ranges. The second range is searched for in the first range.

std::string str = "Hello, World!";
iterator_range<std::string::iterator> it = find_last(str, "o");
int index = std::distance(str.begin(), it.begin());

If you really want to user a finder, it seems like you're looking for boost::algorithm::last_finder. The finders return a function object that takes two iterators as its arguments. The function returns an iterator_range You can use it like so:

auto finder = last_finder("o");

std::string str = "Hello, World!";
iterator_range<std::string::iterator> it = finder(str.begin(), str.end());
int index = std::distance(str.begin(), it.begin());
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • Perhaps the easiest thing to do is best for now. Thank you for your answer. – Mushy Mar 26 '13 at 17:47
  • @Mushy Just to be clear, you are aware that `std::string` has an `rfind` member function, right? If you're just searching in strings, use that. Use the `boost` algorithms if you need to work on generic containers. – Joseph Mansfield Mar 26 '13 at 17:51
  • Yes I know of rfind determined in a link to my original question. Are you suggesting that the boost library is best or perhaps should only be used on generic containers? I have a large program employing both generic and non. – Mushy Mar 26 '13 at 17:57
  • @Mushy It is best to use `rfind` if all you need to do is search for the last occurrence of a substring in a `std::string`. But if you need to search through any type of container, maybe a `std::vector` or `std::set`, you'll want to use the boost algorithms. – Joseph Mansfield Mar 26 '13 at 17:58
  • Ahhh ok then and thank you for the clarifications. I will switch the code to rfind since I am searching a std::string. But isn't a std::string a container too containing characters and offering some of the standard container operations? – Mushy Mar 26 '13 at 18:01
  • @Mushy Strictly speaking, `std::string` doesn't meet all of the requirements of a container. It's very nearly a container though. Anyway, the point is, being generic sometimes comes at a price. If you *know* you're using a `std::string`, then there's no point being generic about it. – Joseph Mansfield Mar 26 '13 at 18:04