C++ 反向字符串首先查找\u而不是\u

C++ 反向字符串首先查找\u而不是\u,c++,string,find,C++,String,Find,我有一个std::string,我想找到第一个字符的位置: 与以下所有字符不同:'、'\n'和'\t' 从我指示的位置降低 例如,如果我有以下字符串和位置: string str("AAA BBB=CCC DDD"); size_t pos = 7; 我想有可能使用这样的方法: size_t res = find_first_of_not_reverse(str, pos, " \n\t"); // now res = 4, because 4 is the position of the

我有一个
std::string
,我想找到第一个字符的位置:

  • 与以下所有字符不同:
    '
    '\n'
    '\t'
  • 从我指示的位置降低
例如,如果我有以下
字符串和位置:

string str("AAA BBB=CCC DDD");
size_t pos = 7;
我想有可能使用这样的方法:

size_t res = find_first_of_not_reverse(str, pos, " \n\t");
// now res = 4, because 4 is the position of the space character + 1

我该怎么办?

正如Bo评论的那样,templatetypedef的答案是99%;我们只需要而不是std::string::find_last_not_of

#include <cassert>
#include <string>

std::string::size_type find_first_of_not_reverse(
    std::string const& str,
    std::string::size_type const pos,
    std::string const& chars)
{
    assert(pos > 1);
    assert(pos < str.size());

    std::string::size_type const res = str.find_last_of(chars, pos - 1) + 1;
    return res == pos ? find_first_of_not_reverse(str, pos - 1, chars)
         : res ? res
         : std::string::npos;
}

int main()
{
    std::string const str = "AAA BBB=CCC DDD";
    std::string const chars = " \n\t";
    std::string::size_type res = find_first_of_not_reverse(str, 7, chars); // res == 4
    res = find_first_of_not_reverse(str, 2, chars); // res == npos
}
#包括
#包括
std::string::size\u type查找\u first\u的\u not\u reverse(
std::字符串常量和str,
std::string::size\u type const pos,
标准::字符串常量和字符)
{
断言(pos>1);
断言(pos
正如Bo所评论的,templatetypedef的答案是99%;我们只需要而不是std::string::find_last_not_of:

#include <cassert>
#include <string>

std::string::size_type find_first_of_not_reverse(
    std::string const& str,
    std::string::size_type const pos,
    std::string const& chars)
{
    assert(pos > 1);
    assert(pos < str.size());

    std::string::size_type const res = str.find_last_of(chars, pos - 1) + 1;
    return res == pos ? find_first_of_not_reverse(str, pos - 1, chars)
         : res ? res
         : std::string::npos;
}

int main()
{
    std::string const str = "AAA BBB=CCC DDD";
    std::string const chars = " \n\t";
    std::string::size_type res = find_first_of_not_reverse(str, 7, chars); // res == 4
    res = find_first_of_not_reverse(str, 2, chars); // res == npos
}
#包括
#包括
std::string::size\u type查找\u first\u的\u not\u reverse(
std::字符串常量和str,
std::string::size\u type const pos,
标准::字符串常量和字符)
{
断言(pos>1);
断言(pos
我很好奇为什么basic\u string不首先定义我自己和朋友的rfind\u。我认为应该。无论如何,这里有一个非递归(参见ildjarn的答案)实现,它应该满足这个问题的要求。它可以编译,但我还没有测试过

std::string delims = " \n\t";
reverse_iterator start = rend()-pos-1, found = 
std::find_first_of(start,rend(),delims.begin(),delims.end());
return found==rend()?npos:pos-(found-start);
要像rfind pos一样,如果它是npos或大于size(),则需要将其设置为size()


附言:我认为这个问题可以从一些编辑中受益。首先,“先找到,后找到,再找到,而不是反向”,这是相当误导的。我想应该是rfind_first of(然后在结果中加1)。

我很好奇为什么basic_string不定义rfind_first of我自己和朋友。我认为应该。无论如何,这里有一个非递归(参见ildjarn的答案)实现,它应该满足这个问题的要求。它可以编译,但我还没有测试过

std::string delims = " \n\t";
reverse_iterator start = rend()-pos-1, found = 
std::find_first_of(start,rend(),delims.begin(),delims.end());
return found==rend()?npos:pos-(found-start);
要像rfind pos一样,如果它是npos或大于size(),则需要将其设置为size()


附言:我认为这个问题可以从一些编辑中受益。首先,“先找到,后找到,再找到,而不是反向”,这是相当误导的。我认为应该先找到(然后在结果中加1)。

空格字符位于位置3。第一个B位于位置4。你为什么想要空间角色的位置?您询问的角色位置不在给定列表中。我不确定我是否理解您的问题。应该是:是下列字符之一:“,\n”和“\t”?@JuanBesa我想在示例中找到第一个“B”,因为我指定了位置7。如果我的位置是str.lenght-1,则res将是第一个“D”的位置。@gliderkite-很抱歉,我误解了你的问题。我认为templatetypedef的思路是正确的。您是否应该只在字符列表中查找最后一个字符,然后添加1?空格字符位于位置3。第一个B位于位置4。你为什么想要空间角色的位置?您询问的角色位置不在给定列表中。我不确定我是否理解您的问题。应该是:是下列字符之一:“,\n”和“\t”?@JuanBesa我想在示例中找到第一个“B”,因为我指定了位置7。如果我的位置是str.lenght-1,则res将是第一个“D”的位置。@gliderkite-很抱歉,我误解了你的问题。我认为templatetypedef的思路是正确的。你不应该只在你的字符列表中找到最后一个字符,然后加上1吗?