C++ 需要递归二进制搜索在模板内部创建的对象
我有一个任务,我一直在努力解决这个问题 作业内容如下: “编写一个程序,在 至少五个对象,并将它们存储在SimpleVector中。在 存储在SimpleVector中,使用二进制递归搜索函数查找并显示结果“ 到目前为止,我的代码是:C++ 需要递归二进制搜索在模板内部创建的对象,c++,templates,recursion,binary-search,C++,Templates,Recursion,Binary Search,我有一个任务,我一直在努力解决这个问题 作业内容如下: “编写一个程序,在 至少五个对象,并将它们存储在SimpleVector中。在 存储在SimpleVector中,使用二进制递归搜索函数查找并显示结果“ 到目前为止,我的代码是: #include <iostream> #include <iomanip> #include "Phonebook.h" #include "C:\Users\John\Google Drive\C++ Projects\CECS2223
#include <iostream>
#include <iomanip>
#include "Phonebook.h"
#include "C:\Users\John\Google Drive\C++ Projects\CECS2223\MyString\MyString.h"
#include "SimpleVector.h"
using namespace std;
const int SIZE = 5;
int main()
{
SimpleVector<Phonebook> Book(SIZE); // Create object inside the template
Book[0].number = "787-254-5874"; // Initialization of objects
Book[0].name = "Pepito Perez";
Book[1].number = "787-925-9872";
Book[1].name = "Rosa Lennis";
Book[2].number = "787-555-6981";
Book[2].name = "Juan E. Aristizabal";
Book[3].number = "787-659-3325";
Book[3].name = "Andrea Rivera";
Book[4].number = "787-148-6932";
Book[4].name = "Elsie Colon";
int i, j;
bool flag = 1;
MyString tempName, tempNumber;
for (i = 1; (i <= SIZE) && flag; i++) // Loop for bubble sorting objects
{
flag = 0;
for (j = 0; j < (SIZE - 1); j++)
{
if (Book[j + 1].name < Book[j].name)
{
tempName = Book[j].name;
Book[j].name = Book[j + 1].name;
Book[j + 1].name = tempName;
tempNumber = Book[j].number;
Book[j].number = Book[j + 1].number;
Book[j + 1].number = tempNumber;
flag = 1;
}
}
}
return 0;
}
#包括
#包括
#包括“Phonebook.h”
#包括“C:\Users\John\Google Drive\C++Projects\cecs223\MyString\MyString.h”
#包括“SimpleVector.h”
使用名称空间std;
常数int SIZE=5;
int main()
{
SimpleVector手册(大小);//在模板内创建对象
Book[0]。number=“787-254-5874”;//对象的初始化
书籍[0]。name=“Pepito Perez”;
书籍[1]。编号=“787-925-9872”;
书[1]。name=“Rosa Lennis”;
书籍[2]。编号=“787-555-6981”;
书[2].name=“胡安E.阿里斯蒂扎巴尔”;
书[3]。编号=“787-659-3325”;
书[3]。name=“安德里亚·里维拉”;
书籍[4]。编号=“787-148-6932”;
书籍[4].name=“Elsie Colon”;
int i,j;
布尔标志=1;
MyString tempName,tempNumber;
对于(i=1;(我离题了:在大量使用该模板之前,帮自己一个忙,你违反了它,结果总是很糟糕。有趣的是,你的作业似乎并不要求你编写二进制搜索。因此,从技术上讲,你可以使用一个预滚模板。但是,你自己编写并学习可能是个好主意根据经验=)“这是一个很好的观点,@paddy。我将建议一个稍微不同的策略。使用一个固定的已知良好搜索编写代码,并测试其填充效果。然后加入您自己的搜索算法。同时测试两组新代码毫无意义。@paddy我脑子里已经有了一个递归二进制函数,问题是我不知道如何使用ObjectYou编写它似乎混淆了职责。二进制搜索根本不应由SimpleVector
类提供,因为不能保证它是排序的。您已经在外部对其进行了排序,因此也应该在外部对其进行二进制搜索。使用相同的比较条件进行搜索您用于排序。更好的是,定义运算符
#pragma once
#ifndef SIMPLEVECTOR_H
#define SIMPLEVECTOR_H
#include <iostream>
#include <cstdlib>
using namespace std;
template <class T>
class SimpleVector
{
private:
T *aptr;
int arraySize;
void subError(); // Handles subscripts out of range
public:
SimpleVector(int); // Constructor
SimpleVector(const SimpleVector &); // Copy constructor
~SimpleVector(); // Destructor
int size()
{ return arraySize; }
T &operator[](int); // Overloaded [] operator
void print(); // outputs the array elements.
};
//*******************************************************
// Constructor for SimpleVector class. Sets the size *
// of the array and allocates memory for it. *
//*******************************************************
template <class T>
SimpleVector<T>::SimpleVector(int s)
{
arraySize = s;
aptr = new T [s];
for (int count = 0; count < arraySize; count++)
aptr[count] = T();
}
//******************************************************
// Copy Constructor for SimpleVector class. *
//******************************************************
template <class T>
SimpleVector<T>::SimpleVector(const SimpleVector &obj)
{
arraySize = obj.arraySize;
aptr = new T [arraySize];
for(int count = 0; count < arraySize; count++)
aptr[count] = obj[count];
}
//*****************************************************
// Destructor for SimpleVector class. *
//*****************************************************
template <class T>
SimpleVector<T>::~SimpleVector()
{
if (arraySize > 0)
delete [] aptr;
}
//******************************************************
// subError function. Displays an error message and *
// terminates the program when a subscript is out of *
// range. *
//******************************************************
template <class T>
void SimpleVector<T>::subError()
{
cout << "ERROR: Subscript out of range.\n";
exit(0);
}
//*******************************************************
// Overloaded [] operator. The argument is a subscript. *
// This function returns a reference to the element *
// in the array indexed by the subscript. *
//*******************************************************
template <class T>
T &SimpleVector<T>::operator[](int sub)
{
if (sub < 0 || sub >= arraySize)
subError();
return aptr[sub];
}
//********************************************************
// prints all the entries is the array. *
//********************************************************
template <class T>
void SimpleVector<T>::print( )
{
for (int k = 0; k < arraySize; k++ )
cout << aptr[k] << " ";
cout << endl;
}
#endif