搜尋此網誌

2026年1月13日星期二

Chapter Quiz

A class template allows you to define a class that can work with any data type. Instead of writing separate classes for int, double, string, etc., you write one generic class and let the compiler generate the specific version when you use it

Inheritance allows one class (the derived class) to reuse and extend the functionality of another class (the base class).

Polymorphism means “many forms” — the ability of different classes to be treated through a common interface, often using virtual functions.

In C++ (and in object-oriented programming generally), a superclass is simply another name for a base class — the class that is being inherited from.

A constructor is a special member function that is automatically called when an object of a class is created.

A destructor is a special member function that is automatically called when an object goes out of scope or is deleted.


template <typename T>

size_t size_in_bits(const T& a) {

    return sizeof(a) * 8;

}

template <typename T>

Declares a function template that can accept any type T.

const T& a

The function takes a constant reference to an object of type T.

Using const ensures the function doesn’t modify a.

Using a reference avoids copying large objects.

sizeof(a)

Returns the size of the object a in bytes.

* 8

Converts bytes into bits (since 1 byte = 8 bits).

Return type size_t

A standard unsigned integer type used for sizes.


Queue (FIFO — First In, First Out)

  • Think of a line at a ticket counter: the first person to arrive is the first person served.
  • Operations:
    • Enqueue → add an element at the back.
    • Dequeue → remove an element from the front.

Stack (LIFO — Last In, First Out)

  • Think of a stack of plates: the last plate you put on top is the first one you take off.
  • Operations:
    • Push → add an element on top.
    • Pop → remove the top element.


What is std::map?

  • A sorted associative container in the C++ Standard Library.
  • Stores elements as key–value pairs (std::pair).
  • Keys are unique (no duplicates).
  • Internally implemented as a balanced binary search tree (usually a Red-Black Tree).
  • Provides logarithmic time complexity (O(log n)) for insert, search, and delete operations.


What is std::vector?

  • A sequence container that stores elements in a contiguous memory block (like an array).
  • Unlike arrays, vectors can grow or shrink dynamically.
  • Provides random access to elements (constant time O(1) for indexing).
  • Efficient insertion/removal at the end (push_back, pop_back are amortized O(1)), but slower at the front or middle (O(n)).
contiguous: touching or next to something

What is std::list?

  • A sequence container that stores elements in a doubly linked list.
  • Unlike std::vector, elements are not stored contiguously in memory.
  • Provides fast insertion and deletion anywhere in the list (O(1) if you already have the iterator).
  • Slower random access (O(n)), since you must traverse nodes sequentially.

 What is std::priority_queue?

  • A container adapter in the C++ Standard Library.
  • Works like a queue, but instead of FIFO order, it always keeps the largest (or highest priority) element at the front by default.
  • Internally implemented using a heap (usually a max-heap).
  • Provides logarithmic time complexity (O(log n)) for insertion and removal, and constant time O(1) for accessing the top element.

Pointer

  • A pointer is a variable that stores the memory address of another variable.
  • You can use it to directly access and manipulate memory.
int x = 10;
int* p = &x;   // p points to x
cout << *p;    // dereference → prints 10

*p → dereference (access value at address).
&x → address-of operator.
Pointers can be incremented/decremented to move through contiguous memory (like arrays).

Iterator

  • An iterator is an object that behaves like a pointer but is designed to work with STL containers (vector, list, map, etc.).
  • Provides a uniform way to traverse containers without worrying about their internal structure.
In computer science, traversing means systematically visiting or accessing every element in a data structure (like an array, tree, or graph) to process it, often using loops or specific algorithms.

#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<int> v = {1, 2, 3, 4};

    // Iterator
    vector<int>::iterator it;
    for (it = v.begin(); it != v.end(); ++it) {
        cout << *it << " ";  // dereference iterator
    }
}

begin() → returns iterator to first element.
end() → returns iterator past the last element.
Iterators can be incremented (++it) to move forward.
Some iterators support random access (like pointers in vector), while others only support sequential traversal (like in list).

Operator overloading in C++ lets you redefine how operators (like +, -, ==, [], etc.) behave for your own classes. It’s a powerful way to make user-defined types feel natural and intuitive to use.

Normally, operators work with built-in types (int, double, etc.).

With operator overloading, you can extend them to work with objects.

Example: Adding two Complex numbers with + instead of writing a function like add(c1, c2).

Complex numbers are numbers of the form a+bi, where a is the real part, b is the imaginary part, and i is the imaginary unit defined by i^2=-1. They extend the real number system to allow solutions to equations like x^2+1=0, which have no real solutions.


Microsoft Copilot

沒有留言:

發佈留言