搜尋此網誌

2025年12月23日星期二

Iterators

In C++, containers are part of the Standard Template Library (STL). They are class templates that store collections of objects and provide methods to access, insert, and manage them. The main categories are sequence containers, associative containers, and unordered associative containers.

In computer science, traversing means systematically visiting every element in a data structure (like an array, tree, or list) to process or use its data, often with loops or recursion.

recursion: the process of repeating a function, each time applying it to the result of the previous stage

In C++, iterators are pointer-like objects used to traverse containers (like vector, list, map) and access their elements. They provide a uniform way to loop through different container types without worrying about their internal structure.

Pointers are one particular type of iterators. Pointers are not just memory addresses; in the STL world, they are treated as the simplest and most efficient iterators.

A pointer is a variable that stores the memory address of another variable. It can be reassigned to point to different variables.

#include <iostream>

using namespace std;


int main() {

    int x = 10;


    // Declare a pointer to int

    int* ptr = &x;   // ptr stores the address of x


    cout << "Value of x: " << x << endl;

    cout << "Address of x: " << &x << endl;

    cout << "Pointer ptr holds: " << ptr << endl;

    cout << "Value at ptr (dereference): " << *ptr << endl;


    // Modify x using the pointer

    *ptr = 20;

    cout << "New value of x: " << x << endl;


    return 0;

}


Output:

Value of x: 10

Address of x: 0x7ffee3b8c8ac   // (example address, varies)

Pointer ptr holds: 0x7ffee3b8c8ac

Value at ptr (dereference): 10

New value of x: 20


References must always refer to a valid object (cannot be null).

alias: used when a person, especially a criminal or an actor, is known by two names

#include <iostream>

using namespace std;


void addFive(int& ref) {

    // ref is a reference to the original variable

    ref = ref + 5;

}


int main() {

    int x = 10;


    // Create a reference to x

    int& alias = x;


    cout << "Original x = " << x << endl;

    cout << "Reference alias = " << alias << endl;


    // Modify x through the reference

    alias = 20;

    cout << "After alias change, x = " << x << endl;


    // Pass by reference to a function

    addFive(x);

    cout << "After function call, x = " << x << endl;


    return 0;

}


Result:

Original x = 10

Reference alias = 10

After alias change, x = 20

After function call, x = 25


void addFive(int& ref)

void → The function doesn’t return a value.

addFive → The function’s name.

int& ref → The parameter is a reference to an integer.

This means the function receives an alias to the caller’s variable.

Any changes made to ref inside the function directly affect the original variable


C++ defines five main categories of iterators: input, output, forward, bidirectional, and random access. Each category specifies what operations are allowed (reading, writing, moving forward/backward, jumping).

int x;

cin >> x;   // read a number from the keyboard into x


int y = 42;

cout << y;   // write the number 42 to the screen


begin()

returns an iterator to the first element of the container.

end()

returns an iterator to one past the last element of the container.

It does not point to the last element itself, but to a position just beyond it.

rbegin()

returns a reverse iterator to the last element of the container.

rend()

returns a reverse iterator to one before the first element.


Microsoft Copilot

沒有留言:

發佈留言