This is an overloaded assignment operator. The const return type avoids expressions such as ( a1 = a2 ) = a3.
const Array &Array::operator=( const Array &right )
{
if ( &right != this ) // avoid self-assignment
{
// for Arrays of different sizes, deallocate original
// left-side Array, then allocate new left-side Array
if ( size != right.size )
{
delete [] ptr; // release space
size = right.size; // resize this object
ptr = new int[ size ]; // create space for Array copy
} // end inner if
for ( size_t i = 0; i < size; ++i )
ptr[ i ] = right.ptr[ i ]; // copy array into object
} // end outer if
return *this; // enables x = y = z, for example
} // end function operator=
Determines if two Array objects are equal. It returns true if they are equal; otherwise, it returns false.
bool Array::operator==( const Array &right ) const
{
if ( size != right.size )
return false; // arrays of different number of elements
for ( size_t i = 0; i < size; ++i )
if ( ptr[ i ] != right.ptr[ i ] )
return false; // Array contents are not equal
return true; // Arrays are equal
} // end function operator==
This is an overloaded subscript operator for non-const Array objects. The reference return creates a modifiable lvalue.
int &Array::operator[]( int subscript )
{
// check for subscript out-of-range error
if ( subscript < 0 || subscript >= size )
throw out_of_range( "Subscript out of range" );
return ptr[ subscript ]; // reference return
} // end function operator[]
This is an overloaded subscript operator for const Array objects. It returns a copy of the selected element.
int Array::operator[]( int subscript ) const
{
// check for subscript out-of-range error
if ( subscript < 0 || subscript >= size )
throw out_of_range( "Subscript out of range" );
return ptr[ subscript ]; // returns copy of this element
} // end function operator[]
This is an overloaded input operator for class Array. It inputs values for the entire Array.
istream &operator>>( istream &input, Array &a )
{
for ( size_t i = 0; i < a.size; ++i )
input >> a.ptr[ i ];
return input; // enables cin >> x >> y;
} // end function
This is an overloaded output operator for class Array.
ostream &operator<<( ostream &output, const Array &a )
{
// output private ptr-based array
for ( size_t i = 0; i < a.size; ++i )
{
output << setw( 12 ) << a.ptr[ i ];
if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output
output << endl;
} // end for
if ( a.size % 4 != 0 ) // end last line of output
output << endl;
return output; // enables cout << x << y;
} // end function operator<<
| Inheritance type | Public | Protected | Private |
|---|---|---|---|
| Public data/functions in the base class |
Public in the derived class | Protected in the derived class | Private in the derived class |
| Protected data/functions in the base class |
Protected in the derived class | Protected in the derived class | Private in the derived class |
| Private data/fun in the base class |
Not accessible in the derived class | Not accessible in the derived class | Not accessible in the derived class |
/* 1. non-static member function */
complex operator+ ( const complex& );
/* 2. non-member function */
friend complex operator* ( const complex&, const complex& );
/* 1. non-static member function */
complex operator-();
/* 2. non-member function */
friend complex operator~( const complex& );
class A {
// A is a base class
};
class B: public A {
// B inherits A.
// B is a derived class
};
int *ptr = new int;
delete ptr;
int *ptr = new int[100];
delete [] ptr;
#include <string>
using namespace std;
...
string str1 = "Hello";
string str2 = "World";
| Function / Operator | Description |
|---|---|
operator[] |
For string a, a[pos]: Return a reference to the character at position pos in the string a. |
operator+= |
Append additional characters at the end of current string. |
operator+ |
Concatenate strings. |
operator< |
Compare string values. For string a and b, a < b: if a is in front of b in dictionary order, return 1, else return 0. |
operator> |
Compare string values. For string a and b, a > b: if a is in back of b in dictionary order, return 1, else return 0. |
operator== |
Compare string values. For string a and b, a == b: if equal, return 1, else return 0; |
operator!= |
Compare string values. For string a and string b, a != b: if not equal, return 1, else return 0; |
compare() |
Compare string. For string a and b, a.compare(b): if equal, return 0. If string a is in front of string b in dictionary order, return -1. If string a is in back of string b in dictionary order, return 1. |
length() |
Return length of string. |
swap() |
Swap string values. |
push_back() |
For string a, a.push_back(c): append character c to the end of the string a, increasing its length by one. |
| Function / Operator | Description |
|---|---|
operator<< |
Retrieves as many characters as possible into the stream. |
operator>> |
Extracts as many characters as possible from the stream. |
str |
Returns a string object with a copy of the current contents of the stream. |
stringstream ss{"Hello"}; // constructor
string str = ss.str(); // a = "Hello"
stringstream ss;
ss<<"75";
ss<<"76";
int num;
ss>>num; // num = 7576
rbegin() returns a reverse iterator pointing to the last element of the vector.rend() returns a reverse iterator pointing to the theoretical position before the first element.rbegin() is the beginning and rend() is the end.
vector<int> v = {10, 20, 30, 40, 50};
for ( auto it = v.rbegin(); it != v.rend(); ++it ) {
cout << *it << " ";
}
// Output:
// 50 40 30 20 10
++it moves the reverse iterator to the next element in the reversed sequence.--it moves the reverse iterator to the previous element in the reversed sequence.next(it) moves forward along the reversed sequence.prev(it) moves backward along the reversed sequence.
vector<int> v = {10, 20, 30, 40, 50};
auto it = v.rbegin();
cout << *it << endl; // 50
++it;
cout << *it << endl; // 40
--it;
cout << *it << endl; // 50
cout << *next(it) << endl; // 40
auto it2 = next(v.rbegin(), 2);
cout << *it2 << endl; // 30
cout << *prev(it2) << endl; // 40
下載 partial judge code 後,將副檔名改成 .zip。 .cpp 和 .h 是相同檔案,下載其中一個即可。
更改副檔名教學:
打開任意資料夾
>> 點擊上方「檢視」
>> 勾選「副檔名」
>> 將 .cpp 或 .h 更改成 .zip
解壓縮
>> 點進 reference 目錄
>> 點進 en 目錄
>> 打開 Main_Page.html
就可以看到完整的 cppreference.com 離線版。
Download the partial judge code and change the extension to .zip. .cpp and .h are the same file. Downloading one of them is enough.
How to change the file extension:
Open the folder with the file in it
>> click "View"
>> check "File name extensions"
>> change .cpp or .h to .zip
Unzip the file
>> click "reference"
>> click "en"
>> open "Main_Page.html"
Then you can use the offline version of cppreference.com.