2

I know there is function

string::find_last_of

But I want to process a large chunk of char* data. I don't want to assign the char* to a string.

I know there is a std function

std::find

But it only can find char* on positive sequence. Is there such a way to find in reverse order? If not in std, is there a way in boost?

bolov
  • 72,283
  • 15
  • 145
  • 224
Administrator
  • 254
  • 1
  • 8

3 Answers3

3

std::find_end()

Searches for the last subsequence of elements [s_first, s_last) in the range [first, last).

For example

#include <algorithm>

char *data = ...;
char *data_end = data + size;
char toFind = ...;
char *pfind = &toFind;

char *found = std::find_end(data, data_end, pfind, pfind + 1);
if (found != data_end)
    ...
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
2

If you want to search for one character, like std::find() does, you can use... std::find(). Take a look at std::reverse_iterator:

auto foo(char* str, std::size_t size, char to_find)
{
    auto rev_it = std::find(std::make_reverse_iterator(str + size),
                            std::make_reverse_iterator(str),
                            to_find);
    auto it = std::make_reverse_iterator(rev_it);

}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
bolov
  • 72,283
  • 15
  • 145
  • 224
1

There is a function in boost can solve this problem.

boost::algorithm::find_last

example:

char* s = "abcdabc";
cout << boost::algorithm::find_last(s, "b").begin() - s;
Administrator
  • 254
  • 1
  • 8