[英]How to simulate the nonexistent find_first_not_of function?
The std::basic_string
class template has member functions find_first_of and find_first_not_of . std::basic_string
类模板具有成员函数find_first_of和find_first_not_of 。
The <algorithm>
header, however, contains only a generic find_first_of . 但是, <algorithm>
标头只包含一个通用的find_first_of 。
Question1: Is the absence of 问题1:是否缺席
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? 只是疏忽(例如copy_if
)或是故意省略,因为行为可以用另一个标准函数实现?
Of course I could write my own find_first_not_of , but 当然我可以写自己的find_first_not_of ,但是
Question2: Is there a ready workaround somewhere in <algorithm>
? 问题2: <algorithm>
某处是否有现成的解决方法? For example, the absence of copy_if
is compensated by the presence of remove_copy_if
例如,缺少copy_if
可以通过remove_copy_if
的存在来补偿
Thanks in advance 提前致谢
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). 我有同样的问题,对你的问题的简短回答:使用标准的stl库是不可能的(虽然有可能使用boost :: phoenix)。
However, you can write your own closure surrounding the sequence iterators that accepts a parameterized 'Value' variable and returns a bool result. 但是,您可以编写自己的闭包,围绕接受参数化“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;
}
};
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) 或者,您可以编写使用较少分支的版本,但需要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'm not sure about your first question but I think the best you can do is find_if: 我不确定你的第一个问题,但我认为你能做的最好的是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));
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.