如何模拟不存在的find_first_not_of函数?

发布于 2024-11-26 11:54:27 字数 642 浏览 0 评论 0原文

std::basic_string 类模板具有成员函数 find_first_offind_first_not_of

然而, 标头仅包含通用的 find_first_of

问题1:是否只是缺少

std::find_first_not_of(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2)

监督(例如copy_if),还是故意省略,因为该行为可以通过另一个标准来实现功能?

当然,我可以编写自己的find_first_not_of,但是

问题2:中是否有现成的解决方法>?例如,copy_if 的缺失由 remove_copy_if 的存在进行补偿

提前致谢

The std::basic_string class template has member functions find_first_of and find_first_not_of.

The <algorithm> header, however, contains only a generic find_first_of.

Question1: Is the absence of

std::find_first_not_of(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2)

just an oversight (as for example copy_if) or is it intentionally omitted because the behavior can be achieved with another standard function?

Of course I could write my own find_first_not_of, but

Question2: Is there a ready workaround somewhere in <algorithm>? For example, the absence of copy_if is compensated by the presence of remove_copy_if

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

踏月而来 2024-12-03 11:54:27

我也遇到了同样的问题,对你的问题的简短回答是:使用标准 stl 库是不可能的(尽管使用 boost::phoenix 是可能的)。

但是,您可以围绕序列迭代器编写自己的闭包,该闭包接受参数化的“Value”变量并返回 bool 结果。

 template<class Iterator> struct is_not_in_range
{
    Iterator const begin;
    Iterator const end;
is_not_in_range(Iterator const& b, Iterator const& e)
    :   begin(b)
    ,   end(e) {}
    template<class Value> bool operator()(Value & v)
    {
          return std::find(begin,end,v) == end;
    }
};

然后你可以这样做

std::find_if(begin1, end1,  is_not_in_range<Iterator2>(begin2,end2));

或者,你可以编写一个使用较少分支的版本,但需要一个break-> continue(用goto语句近似)

template<class Iterator1, class Iterator2> Iterator1 find_first_not_of
(   Iterator1 const& begin1
,   Iterator1 const& end1
,   Iterator2 const& begin2
,   Iterator2 const& end2 )
{
    for(Iterator1 mid1 = begin1; mid1 != end1; ++mid1)
    {
        for(Iterator2 mid2 = begin2; mid2 != end2; ++mid2)
            if(*mid1 == *mid2)
                goto FOUND;
        return mid1;
FOUND:      ;
    }
    return end1;
};

I had this same problem, the short answer to your question: it's not possible with the standard stl libraries (although it is possible with boost::phoenix).

However, you can write your own closure surrounding the sequence iterators that accepts a parameterized 'Value' variable and returns a bool result.

 template<class Iterator> struct is_not_in_range
{
    Iterator const begin;
    Iterator const end;
is_not_in_range(Iterator const& b, Iterator const& e)
    :   begin(b)
    ,   end(e) {}
    template<class Value> bool operator()(Value & v)
    {
          return std::find(begin,end,v) == end;
    }
};

Then you can do this

std::find_if(begin1, end1,  is_not_in_range<Iterator2>(begin2,end2));

Alternatively, you can write a version that uses less branching, but requires a break->continue (approximated with a goto statement)

template<class Iterator1, class Iterator2> Iterator1 find_first_not_of
(   Iterator1 const& begin1
,   Iterator1 const& end1
,   Iterator2 const& begin2
,   Iterator2 const& end2 )
{
    for(Iterator1 mid1 = begin1; mid1 != end1; ++mid1)
    {
        for(Iterator2 mid2 = begin2; mid2 != end2; ++mid2)
            if(*mid1 == *mid2)
                goto FOUND;
        return mid1;
FOUND:      ;
    }
    return end1;
};
偏闹i 2024-12-03 11:54:27

最新的STL中添加了新函数(跳转至)。

all_ofany_ofnone_offind_if_notcopy_if 等。

There are new functions added in latest STL (Jump to).

all_of, any_of, none_of, find_if_not, copy_if etc.

信愁 2024-12-03 11:54:27

我不确定你的第一个问题,但我认为你能做的最好的事情就是 find_if:

template <class Iter>
class Check
{
public:
    Check(Iter first, Iter last) : first_(first), last_(last) { }
    template <class T>
    bool operator()(const T& item) { return std::find(first, last, item) == last; }
private:
    Iter first_;
    Iter last_;
};

find_if(first1, last1, Check<Iter2>(first2, last2));

I'm not sure about your first question but I think the best you can do is find_if:

template <class Iter>
class Check
{
public:
    Check(Iter first, Iter last) : first_(first), last_(last) { }
    template <class T>
    bool operator()(const T& item) { return std::find(first, last, item) == last; }
private:
    Iter first_;
    Iter last_;
};

find_if(first1, last1, Check<Iter2>(first2, last2));

写一个很容易:

pos = std::find(search_list.begin()...)
if (pos!= npos)
{
   pos = std::find(black_list.begin()...)
   if (pos!= npos)
   {
       continue search
   }
   else
  {
      found !!
  }
}
else
{
   not found
}

It's easy to write one:

pos = std::find(search_list.begin()...)
if (pos!= npos)
{
   pos = std::find(black_list.begin()...)
   if (pos!= npos)
   {
       continue search
   }
   else
  {
      found !!
  }
}
else
{
   not found
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文