From 1fba558d434ba625a9fb743f77b5a300e874da11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=A1=E5=9D=82=E6=98=B4?= Date: Sun, 22 Dec 2024 01:10:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E6=88=90vector=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homework6/test1.cpp | 14 +++++++------- homework6/test2.cpp | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/homework6/test1.cpp b/homework6/test1.cpp index 1ffb137..fcc7807 100644 --- a/homework6/test1.cpp +++ b/homework6/test1.cpp @@ -1,25 +1,25 @@ // 编写一个程序,输出在顺序表(3,6,2,10,l,8,5,7,4,9) 中采用顺序查找方法查找关键字5的过程。 -#include +#include #include using namespace std; template -int seqSearch(list l,T value) { +int seqSearch(vector l,T value) { if (l.empty()) { cout << "空表" << endl; return -1; } bool found = false; int index = 0; - for (typename list::iterator it = l.begin(); it != l.end(); ++it) { - cout<<"正在顺序查找第"< l,T value) { } int main() { - list l={3,6,2,10,'l',8,5,7,4,9}; + vector l={3,6,2,10,'l',8,5,7,4,9}; int index=seqSearch(l,5); cout< -#include +#include using namespace std; +template + +void ListBinaryCopy(vector p,vector sl,vector sr) { + int low=0; + int high=p.size()-1; + int mid=(low+high)/2; + + for (int i=low;i<=mid;i++) { + sl[i]=p[i]; + } + + for (int i=mid+1;i<=high;i++) { + sr[i]=p[i]; + } +} + +template +int BinarySearch(vector l, T value) { + if (l.empty()) return -1; + + vector sub_list_left, sub_list_right; + ListBinaryCopy(l, sub_list_left); + + BinarySearch(sub_list_left, value); + BinarySearch(sub_list_right, value); +} + int main() { + vector l = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; return 0; -} \ No newline at end of file +}