C++类属性算法search

简介: 类属算法search的功能是:给定两个迭代器区间,将后一个区间内的对象作为一个子序列,并在前一个区间内查找出现该子序列的第一个位置。 1 // Illustrating the generic equal and mismatch algorithms 2 #include 3 #...

类属算法search的功能是:给定两个迭代器区间,将后一个区间内的对象作为一个子序列,并在前一个区间内查找出现该子序列的第一个位置。

 
 
1 // Illustrating the generic equal and mismatch algorithms
2 #include < iostream >
3 #include < cassert >
4 #include < algorithm >
5 #include < string >
6 #include < list >
7 #include < deque >
8 #include < vector >
9 using namespace std;
10
11 int main()
12 {
13 cout << " Illustrating the generic equal "
14 << " and mismatch algorithms. " << endl;
15 list < string > driver_list;
16 vector < string > vec;
17 deque < string > deq;
18
19 driver_list.insert(driver_list.end(), " Clark " );
20 driver_list.insert(driver_list.end(), " Rindt " );
21 driver_list.insert(driver_list.end(), " Senna " );
22
23 vec.insert(vec.end(), " Clark " );
24 vec.insert(vec.end(), " Rindt " );
25 vec.insert(vec.end(), " Senna " );
26 vec.insert(vec.end(), " Berger " );
27
28 deq.insert(deq.end(), " Clark " );
29 deq.insert(deq.end(), " Berger " );
30
31 // Show that driver_list and the first 3 elements of
32 // vec are equal in all corresponding positions:
33 assert (equal(driver_list.begin(), driver_list.end(),
34 vec.begin()));
35
36 // Show that deq and the first 2 elements of driver_list
37 // are not equal in all corresponding positions:
38 assert ( ! equal(deq.begin(), deq.end(),
39 driver_list.begin()));
40
41 // Find the corresponding positions in deq and driver_list
42 // at which unequal elements first occur:
43 pair < deque < string > ::iterator, list < string > ::iterator >
44 pair1 = mismatch(deq.begin(), deq.end(),
45 driver_list.begin());
46
47 if (pair1.first != deq.end())
48 cout << " First disagreement in deq and driver_list:\n "
49 << * (pair1.first) << " and " << * (pair1.second)
50 << endl;
51 return 0 ;
52 }
相关文章
|
4天前
|
编译器 C++
【C++】一文全解四种经典 [ 特殊类 ]的设计
【C++】一文全解四种经典 [ 特殊类 ]的设计
|
5天前
|
编译器 C语言 C++
c++初阶------类和对象(六大默认构造函数的揭破)-3
c++初阶------类和对象(六大默认构造函数的揭破)
|
5天前
|
编译器 C语言 C++
c++初阶------类和对象(六大默认构造函数的揭破)-2
c++初阶------类和对象(六大默认构造函数的揭破)
|
5天前
|
存储 编译器 C语言
c++初阶------类和对象(六大默认构造函数的揭破)-1
c++初阶------类和对象(六大默认构造函数的揭破)
|
5天前
|
存储 编译器 C语言
c++初阶-------类和对象-2
c++初阶-------类和对象
|
5天前
|
编译器 C语言 C++
c++初阶-------类和对象-1
c++初阶-------类和对象
|
6天前
|
存储 Java C++
【C++类和对象】探索static成员、友元以及内部类
【C++类和对象】探索static成员、友元以及内部类
|
6天前
|
安全 程序员 编译器
【C++类和对象】初始化列表与隐式类型转换
【C++类和对象】初始化列表与隐式类型转换
|
6天前
|
安全 编译器 C++
【C++类和对象】const成员函数及流插入提取
【C++类和对象】const成员函数及流插入提取
|
6天前
|
存储 C++
【C++类和对象】日期类的实现(下)
【C++类和对象】日期类的实现