Unit 10th-Unit job: OJ Programming-Creating an array class template
View Help
return
Warm tips:
1. This assignment belongs to the online Judge topic and is enigmatic grading by the system immediately after submission.
2. Students can submit an unlimited number of answers before the deadline , and the system will take the highest score as the final result.
Create a template class in this unit job practice.
The unit job involves algorithms such as bubbling sorting, linear lookups, and so on. If you are unfamiliar with sorting and searching, you can do it by yourself, Baidu or Google
In accordance with the terms of academic integrity, I guarantee that this work is done independently.
1 Write an array class template array that can store different types of array elements, find and sort arrays of elements (5 points)
For details, refer to "unit 10th-Job Description:" OJ-Create Array class template ""
time limit: 500ms memory limit: 32000kb
#include <iostream> #include <vector> #include <limits>using namespace std;template <typename T >class ARRAY{PUBLIC://2. The constructor for the array class is as follows: Array (vector<t> v) {size_ = V.size ();d Ata_ = static_cast<t*> (new t[size_]); for (int i = 0; I &l T Size_; i++) {Data_[i] = V[i];}} 3. The other three functions of the Array class to be written are as follows: ~array (); destructor to release the memory void that has been applied to sort (); Sort all the elements in the array from small to large to sort int seek (T key); Finds the specified element key, returning its location (position number starting from 0); If it is not found, it returns -1//4. The display function of the array class is as follows: void display () {int i;for (i = 0; i < size_; i++) {cout << data_[i];if (i! = size_-1) cout & lt;< ""; Outputs a space}}private://1. The array class has 2 private data members: t* Data_; The first address of the memory that holds all the elements in the array is int size_; The number of elements in the array};template<typename T>array<t>::~array ()//destructors, releasing the memory already applied to {Delete[]data_;} Template<typename t>void Array<t>::sort ()//sorts all elements in the array from small to large sorted {for (int i = 0; i < size_-1; i++) {for (int j = 0; J < size_-1-i; J + +) {if (Data_[j] > data_[j + 1]) {T temp = data_[j];d ata_[J] = Data_[j + 1];d ata_[j + 1] = temp;}}} Template<typename t>int Array<t>::seek (T key)//find the specified element key and return to its location (position number starting from 0); If not found, returns -1{for (int i = 0; i < size_; i++) {if (key = = Data_[i]) {return i;}} return-1;} int main () {vector<int> vi;int ti;vector<double> vd;double td;vector<char> vc;char tc;while (Cin.get ( ) = ' \ n ') {cin.unget (); Cin >> Ti;vi.push_back (TI);} while (Cin.get ()! = ' \ n ') {cin.unget (); Cin >> Td;vd.push_back (TD);} while (CIN >> TC) {vc.push_back (TC);} Array<int> AI (vi); Array<double> AD (VD); array<char> AC (VC), cout << Ai.seek (Ten) << endl;cout << ad.seek (10.0) << Endl;cout << Ac.seek (' a ') << endl;ai.sort (); Ad.sort (); Ac.sort (); Ai.display (); cout << Endl;ad.display (); cout << Endl;ac.display ();//GCC and VC compilers are paused in debug mode for easy viewing of Run results # if (defined (__debug__) | | defined (_DEBUG)) Cin.ignore (numeric_ Limits<streamsize>::max (), ' \ n '); Cin.get (); #endifreturn 0;}
Introduction to the _c++ program design of NetEase Cloud Classroom (Next) _ 10th: The month reflects the thousand rivers not to reduce the Ming-template _ 10th Unit-Unit job: OJ Programming-Creating an array class template