Is there a function like find_last_of in std but not in string?

那年仲夏 提交于 2019-12-24 05:49:29

问题


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?


回答1:


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)
    ...



回答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);

}



回答3:


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;


来源:https://stackoverflow.com/questions/53075961/is-there-a-function-like-find-last-of-in-std-but-not-in-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!