1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <iostream>
#include <algorithm>
#include <string>
template <class ForwardIterator1, class ForwardIterator2>
bool subsequence (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2) {
while (first2 != last2)
{
ForwardIterator1 it = std::find (first1, last1, *first2);
if (it == last1)
return false;
++first2; first1 = it;
}
return true;
}
int main() {
std::string A[] = {"a", "b", "c", "f", "b"};
std::string B[] = {"b", "f"};
std::string C[] = {"c", "a"};
if (subsequence (A, A+5, B, B+2))
std::cout << "B is a subsequence of A." << std::endl;
else
std::cout << "B is not a subsequence of A." << std::endl;
if (subsequence (A, A+5, C, C+2))
std::cout << "C is a subsequence of A." << std::endl;
else
std::cout << "C is not a subsequence of A." << std::endl;
std::cin.get();
}
|