std::search
cppreference.com
<tbody>
</tbody>
template< class ForwardIt1, class ForwardIt2 > ForwardIt1 search( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last ); |
(1) | |
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > ForwardIt1 search( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p ); |
(2) | |
[s_first, s_last) [first, last - (s_last - s_first)). operator== , p.
[first, last)
|
||
[s_first, s_last)
|
||
| p | , true . :
| |
-ForwardIt1 ForwardIterator.
| ||
-ForwardIt2 ForwardIterator.
| ||
[s_first, s_last) [first, last - (s_last - s_first)). , last.
[s_first, s_last) , first. ( C++11)
S*N , S= std::distance(s_first, s_last), N = std::distance(first, last).
template<class ForwardIt1, class ForwardIt2>
ForwardIt1 search(ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last)
{
for (; ; ++first) {
ForwardIt1 it = first;
for (ForwardIt2 s_it = s_first; ; ++it, ++s_it) {
if (s_it == s_last) {
return first;
}
if (it == last) {
return last;
}
if (!(*it == *s_it)) {
break;
}
}
}
}
|
template<class ForwardIt1, class ForwardIt2, class BinaryPredicate>
ForwardIt1 search(ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last,
BinaryPredicate p)
{
for (; ; ++first) {
ForwardIt1 it = first;
for (ForwardIt2 s_it = s_first; ; ++it, ++s_it) {
if (s_it == s_last) {
return first;
}
if (it == last) {
return last;
}
if (!p(*it, *s_it)) {
break;
}
}
}
}
|
#include <string>
#include <algorithm>
#include <iostream>
template<typename Container>
bool in_quote(const Container& cont, const std::string& s)
{
return std::search(cont.begin(), cont.end(), s.begin(), s.end()) != cont.end();
}
int main()
{
std::string str = " , ?";
// str.find()
std::cout << std::boolalpha << in_quote(str, "") << '\n'
<< in_quote(str, "") << '\n';
std::vector<char> vec(str.begin(), str.end());
std::cout << std::boolalpha << in_quote(vec, "") << '\n'
<< in_quote(vec, "") << '\n';
}
:
true
false
true
false
.
| ( ) | |
| , ( ) | |
(C++11) |
, ( ) |
true, ( ) | |
| , ( ) | |
| ( ) |