14969 - CheatSheet (I2P2-Final)   

Description


1. Class Array Operator Overloading

• Assignment operator

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=

• Equality 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==

• Subscript operator for non-const Arrays

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[]

• Subscript operator for const Arrays

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[]

• Input 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

• Output operator

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

2. Inheritance Access Control

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

3. Operator Overloading

• Binary operators

/* 1. non-static member function */
complex operator+ ( const complex& );

/* 2. non-member function */
friend complex operator* ( const complex&, const complex& );

• Unitary operators

/* 1. non-static member function */
complex operator-();

/* 2. non-member function */
friend complex operator~( const complex& );

4. Inheritance

class A {
// A is a base class
};

class B: public A {
// B inherits A.
// B is a derived class
};

5. Dynamic Allocation

• Single variable allocation

int *ptr = new int;
delete ptr;

• Array allocation

int *ptr = new int[100];
delete [] ptr;

6. String Object

#include <string>
using namespace std;
...
string str1 = "Hello";
string str2 = "World";

7. Standard C++ Library - <string>

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.

8. Standard C++ Library - <sstream>

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.

• Example 1

stringstream ss{"Hello"};    // constructor
string str = ss.str();    // a = "Hello"

• Example 2

stringstream ss;
ss<<"75";
ss<<"76";
int num;
ss>>num;    // num = 7576

9. Reverse Iterator in std::vector

• rbegin() and rend() in vector

  • 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.
  • In the reversed view, 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

• Iterator movement in reversed vector view

  • ++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

10. Offline cppreference.com

• 中文說明

下載 partial judge code 後,將副檔名改成 .zip.cpp.h 是相同檔案,下載其中一個即可。

更改副檔名教學:

打開任意資料夾
>> 點擊上方「檢視」
>> 勾選「副檔名」
>> 將 .cpp 或 .h 更改成 .zip
解壓縮
>> 點進 reference 目錄
>> 點進 en 目錄
>> 打開 Main_Page.html

就可以看到完整的 cppreference.com 離線版。

• English Version

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.

Input

Output

Sample Input  Download

Sample Output  Download

Partial Judge Code

14969.cpp

Partial Judge Header

14969.h

Tags




Discuss