C++ experiment --- integer array operator overloading

Integer array operator overloading

Description
defines the Array class:

1. Have data members int length and int *mems, which are the number of elements in the array and the list of elements, respectively.

No-argument constructor, set mems to NULL and length to 0.

Overload == operator, used to determine whether two Array objects are equal. Equality includes two situations: (1) Two objects are the same object, that is, they have the same address (remember: this pointer points to the current object, which is the address of the current object); (2) The length of the two objects is the same, And the values ​​of corresponding elements in mems are the same. All other cases are not equal.

Use friend functions to overload the << and >> operators. See below for input and output formats.

Input is
divided into multiple lines.

The first line is a positive integer M, indicating that there are M arrays.

Each array is a row, where the first non-negative integer N represents the number of elements in the array, and there are N integers after that.

Output
has M lines.

The first line of output is the first array.

Starting from the second line, the corresponding array elements are output first (separated by a space between two, and there can be no spaces at the beginning and end). If the array is empty, no elements are output. Then according to whether this array is the same as the previous one, output "unequal to above." and "equal to above".

Sample Input

5
3 1 2 3
3 1 2 3
0
7 1 2 3 4 5 6 7
7 1 2 3 4 5 6 8

Sample Output

1 2 3
1 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.

Title given code

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;
}

code:

#include<iostream>

using namespace std;
class Array{
    
    
	int length;
	int *mems;
public:
	Array(){
    
    
		length=0;
		mems=NULL;
	}
 	bool operator ==(const Array A){
    
    
		if(this==&A){
    
    
			return true;
		}else if(A.length==length){
    
    
			for(int i=0;i<length;i++){
    
    
				if(A.mems[i]!=mems[i]){
    
    
					return false;
				}
			}
			return true;
		}else{
    
    
			return false;
		}
	}
//	使用友元重载也是ok的
//	friend bool operator ==(Array A,Array B){
    
    
//		if(&A==&B)return true;
//		else if(A.length==B.length){
    
    
//			for(int i=0;i<A.length;i++){
    
    
//				if(A.mems[i]!=B.mems[i])return false;
//			}
//			return true;
//		}else{
    
    
//			return false;
//		}
//	}
//	
	friend istream& operator >>(istream &is,Array &A){
    
    
		is>>A.length;
		A.mems=new int[A.length];
		for(int i=0;i<A.length;i++){
    
    
			is>>A.mems[i];
		}
		return is;
	}
	
	friend ostream& operator <<(ostream &os,const Array &A){
    
    
		if(A.length>0){
    
    
			for(int i=0;i<A.length;i++){
    
    
				os<<A.mems[i];
				if(i<A.length-1)os<<' ';
			}
		}
		return os;
	} 
};

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;
}

Guess you like

Origin blog.csdn.net/timelessx_x/article/details/115050760