搜尋此網誌

2026年1月9日星期五

Learn from exercise

std::deque (short for double-ended queue) is a container in C++ that allows fast insertion and deletion at both ends. It’s part of the Standard Template Library (STL).

contiguous: touching or next to something

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

int main() {
    deque<int> dq;

    // Add elements
    dq.push_back(10);   // [10]
    dq.push_front(20);  // [20, 10]

    // Access elements
    cout << dq.front() << endl; // 20
    cout << dq.back() << endl;  // 10
    cout << dq[1] << endl;      // 10

    // Remove elements
    dq.pop_front(); // [10]
    dq.pop_back();  // []

    return 0;
}

dq[1] means "give me the element at index 1".

Since C++ uses zero-based indexing, dq[0] is the first element, and dq[1] is the second element.

​intimidate somebody (into something/into doing something) to frighten or threaten somebody so that they will do what you want

In C++, template is used to define generic functions or classes, while typename clarifies that a dependent name inside a template refers to a type.

template <typename T>
T add(T a, T b) {
    return a + b;
}

enum class in C++ is a strongly typed enumeration introduced in C++11. It improves upon traditional enum by providing better type safety and scoping.

#include <iostream>
using namespace std;

enum class Color { Red, Green, Blue };
enum class Fruit { Apple, Banana, Orange };

int main() {
    Color c = Color::Red;
    Fruit f = Fruit::Apple;

    // Scoped access
    cout << (c == Color::Red) << endl;   // true
    cout << (f == Fruit::Banana) << endl; // false

    // No implicit conversion to int
    // int x = c; // ❌ Error

    // Explicit cast required
    int x = static_cast<int>(Color::Green); // ✅ Works
    cout << x << endl; // prints 1
}

In C++, the operator == is the equality comparison operator.

It checks whether two values are equal.
Returns a boolean (true or false).

The symbol :: in C++ is called the scope resolution operator. It’s used to specify the scope in which a name (variable, function, class, etc.) is defined.

Main Uses of ::

1. Accessing Global Variables
If a local variable shadows a global one, :: lets you access the global version:
#include <iostream>
using namespace std;

int x = 10; // global

int main() {
    int x = 20; // local
    cout << x << endl;   // prints 20
    cout << ::x << endl; // prints 10 (global x)
}

2. Accessing Class Members
Used to define or access members outside the class body:
class MyClass {
public:
    static int value;
    void show();
};

int MyClass::value = 42; // define static member

void MyClass::show() {   // define member function
    std::cout << "Value = " << value << std::endl;
}

3. Accessing Namespaces
Used to access functions, classes, or variables inside a namespace:
#include <iostream>
namespace Math {
    int add(int a, int b) { return a + b; }
}

int main() {
    std::cout << Math::add(3, 4) << std::endl; // 7
}

4. Enumerations
With enum class, you must use :: to access enumerators:
enum class Color { Red, Green, Blue };

Color c = Color::Red; // scoped access

In programming, a dummy value usually means a placeholder value that doesn’t carry real meaning but is used temporarily.

#include is a C++ standard library header that provides a collection of utility functions, classes, and templates commonly used in generic programming.

Here is one of the most important components:

std::pair

  • A simple container that holds two values.
  • Useful for returning two results from a function or storing key-value pairs.
for (initialization; condition; update) {
    // code to execute repeatedly
}

Let’s combine a for loop with a switch statement in C++. This is a common pattern when you want to iterate through values and perform different actions depending on the case.

#include <iostream>
using namespace std;

int main() {
    // Loop from 1 to 5
    for (int i = 1; i <= 5; i++) {
        switch (i) {
            case 1:
                cout << "Case 1: Hello" << endl;
                break; // exit switch

            case 2:
                cout << "Case 2: World" << endl;
                break;

            case 3:
                cout << "Case 3: Foo" << endl;
                break;

            case 4:
                cout << "Case 4: Bar" << endl;
                break;

            default:
                cout << "Default case: i = " << i << endl;
                break;
        }
    }
    return 0;

- The for loop runs from i = 1 to i = 5.
- Each iteration enters the switch block.
- The case labels match specific values of i.
- The break ensures the program exits the switch after executing one case (otherwise it would “fall through” to the next case).
- The default handles values not explicitly (clearly or directly, so that the meaning is easy to understand) listed in cases.

Case 1: Hello
Case 2: World
Case 3: Foo
Case 4: Bar
Default case: i = 5

- break inside switch only exits the switch, not the for loop.
- If you want to exit the entire loop from inside a switch, you can use break with a label (C++20) or goto, or simply return if inside main.

for (const auto& op : operations) {
    // use op here
}

- operations → a container (like std::vector, std::deque, std::list, etc.).
- op → each element in the container, one at a time.
- auto& → deduces the element type automatically and binds it by reference (so no copy is made).
- const → ensures you cannot modify the element inside the loop.

if (!schedule.empty())

- schedule → some container (like std::vector, std::deque, std::list, std::map, etc.).
- .empty() → a member function that returns true if the container has no elements, false otherwise.
- ! → logical NOT operator, flips the result.
So the condition means:
 “If schedule is NOT empty, then execute the following block.

Microsoft Copilot

沒有留言:

發佈留言