如何模拟不存在的find_first_not_of函数?
std::basic_string
类模板具有成员函数 find_first_of 和 find_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也遇到了同样的问题,对你的问题的简短回答是:使用标准 stl 库是不可能的(尽管使用 boost::phoenix 是可能的)。
但是,您可以围绕序列迭代器编写自己的闭包,该闭包接受参数化的“Value”变量并返回 bool 结果。
然后你可以这样做
或者,你可以编写一个使用较少分支的版本,但需要一个break-> continue(用goto语句近似)
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.
Then you can do this
Alternatively, you can write a version that uses less branching, but requires a break->continue (approximated with a goto statement)
最新的STL中添加了新函数(跳转至)。
all_of
、any_of
、none_of
、find_if_not
、copy_if
等。There are new functions added in latest STL (Jump to).
all_of
,any_of
,none_of
,find_if_not
,copy_if
etc.我不确定你的第一个问题,但我认为你能做的最好的事情就是 find_if:
I'm not sure about your first question but I think the best you can do is find_if:
写一个很容易:
It's easy to write one: