Problem B: integer array operator overloading time limit:1 Sec Memory limit:128 MB
submit:2871 solved:1875
[Submit] [Status] [Web Board] Description
Define the Array class:
1. Owns the data member int length and int *mems, respectively, the number of elements in the array and the list of elements.
2. No parameter constructor, set MEMS to Null,length to 0.
3. Overload = = operator, which is used to determine whether two array objects are equal. Equality consists of two cases: (1) Two objects are the same object, that is, they have the same address (remember: this pointer points to the current object, is the address of the current object), and (2) two objects have the same length, and the corresponding elements in the MEMS have the same values. All other cases are unequal.
4. Use the friend function to overload the << and >> operators. The input and output formats are shown below.
Input
The input is divided into multiple lines.
The first line is a positive integer m, which indicates that there are m arrays.
Each array is a row, where the first non-negative integer n represents the number of elements of the array, followed by n integers.
Output
The output has m rows.
The first line of output is the first array.
Starting with the second line, the corresponding array elements (22 separated by a space, without spaces) are first exported, and if the array is empty, the element is not output. Then, depending on whether the array is the same as the previous array, the output is "unequal to above." (Not equal) and "equal to above" (equal).
Sample Input
53 1 2 33 1 2 307 1 2 3 4 5 6 77 1 2 3 4 5 6 8
Sample Output
1 2 2 3 equal to above. Unequal to ABOVE.1 2 3 4 5 6 7 unequal to ABOVE.1 2 3 4 5 6 8 unequal to above.
HINT Append codeappend.cc,
int main () { int cases; cin>>cases; Array arraies[cases]; for (int i = 0; i < cases; i++) { cin>>arraies[i]; } cout<<arraies[0]<<endl; for (int i = 1; i < cases; i++) { if (arraies[i] = = Arraies[i-1]) { cout<<arraies[i]<< "<<" equal to above. " <<endl; } else { cout<<arraies[i]<< "" << "unequal to above." <<endl; } } return 0;}
#include <iostream>using namespace Std;class array{public:int length; int *mems; Array (): Length (0), MEMS (NULL) {} friend IStream &operator>> (IStream &is, Array &p) {int len ; is>>len; P.length=len; P.mems=new Int[p.length]; for (int i=0; i<len; i++) {is>>p.mems[i]; } return is; } friend Ostream &operator<< (ostream &os, Array &p) {for (int i=0; i<p.length; i++) {if (i==0) os<<p.mems[i]; else os<< "" <<p.mems[i]; }//os<<endl; return OS; } bool operator== (Array &p) {if (length==p.length) {for (int i=0; i<length; i++) {if (Mems[i]!=p.mems[i]) return false; } return true; } return false; } ~array () {delete []mems;}}; int MAIn () {int cases; cin>>cases; Array arraies[cases]; for (int i = 0; i < cases; i++) {cin>>arraies[i]; } cout<<arraies[0]<<endl; for (int i = 1; i < cases; i++) {if (arraies[i] = = Arraies[i-1]) {cout<<arraies[i ]<< "" << "equal to above." <<endl; } else {cout<<arraies[i]<< "" << "unequal to above." <<endl; }} return 0;}
Problem B: integer array operator overloading