YouTip LogoYouTip

Cpp Libs Array At

[![Image 1: C++ Container Classes ](#) C++ Container Classes ](#) * * * Among all the ways to access array elements, `at` is the safest because it provides **bounds checking**. `at` is a member function of the container class used to **return the element at the specified position** while performing bounds checking. If the index is out of bounds, it throws a `std::out_of_range` exception. `at` implements safe element access, allowing you to catch and handle out-of-bounds errors at runtime. **Word Definition**: `at` means "at position", i.e., getting the element at the specified position. * * * ## Basic Syntax and Parameters `at` is a member function of the container class, so you need to have a container object first, then call it using the dot operator `.`. ### Syntax Format reference at(size_type pos); const_reference at(size_type pos) const; ### Parameter Description * **Parameter**: `pos` * Type: `size_type` (unsigned integer type, usually `size_t`) * Description: The position (index) of the element to access. Index starts from 0, and the maximum valid index is `size() - 1`. ### Function Description * **Return Value**: Returns a **reference** to the element at the specified position. If the container is a const container, it returns a const reference. * **Effect**: Returns the element at the specified position. If the index is out of valid range (`pos >= size()`), it throws a `std::out_of_range` exception. * **Difference from operator[]**: `at` performs bounds checking, `operator[]` does not perform bounds checking (out-of-bounds behavior is undefined). * * * ## Examples Let's thoroughly master the usage of `at` through a series of examples from simple to complex. ### Example 1: Basic Usage - Accessing Elements ## Example #include #include #include int main(){ // 1. Create an array and initialize it std::array numbers ={10, 20, 30, 40, 50}; std::cout<<"arraysize is: "<< numbers.size()<< std::endl; // 2. Use at to access elements std::cout<<"First element (at(0)): "<< numbers.at(0)<< std::endl; std::cout<<"Second element (at(1)): "<< numbers.at(1)<< std::endl; std::cout<<"Third element (at(2)): "<< numbers.at(2)<< std::endl; std::cout<<"Last element (at(4)): "<< numbers.at(4)<< std::endl; // 3. Use a loop to access all elements std::cout<<"All elements: "; for(size_t i =0; i < numbers.size();++i){ std::cout<< numbers.at(i)<<" "; } std::cout<< std::endl; return 0; } **Expected Output:** arraysize is: 5First element (at(0)): 10Second element (at(1)): 20Third element (at(2)): 30Last element (at(4)): 50All elements: 10 20 30 40 50 **Code Analysis:** 1. `numbers.at(0)` returns the first element `10` (index starts from 0). 2. `numbers.at(4)` returns the last element `50` (because `size()` is 5, valid indices are 0-4). 3. Using a loop with `at` can safely traverse all elements. ### Example 2: Catching Out-of-Bounds Exception The main advantage of `at` is that it can throw exceptions, allowing us to handle out-of-bounds errors. ## Example #include #include #include int main(){ std::array numbers ={10, 20, 30}; // Try to access out-of-bounds position try{ std::cout<<"Attempting to access at(10)..."<< std::endl; int value = numbers.at(10);// This will throw an exception std::cout<<"Value: "<< value << std::endl; } catch(const std::out_of_range& e){ std::cout<<"Exception caught: "<< e.what()<< std::endl; } // Normal access try{ std::cout<<"Access at(2): "<< numbers.at(2)<< std::endl; } catch(const std::out_of_range& e){ std::cout<<"Exception caught: "<< e.what()<= _Nm (which is 3)Access at(2): 30 **Code Analysis:** * When
← Cpp Libs Array DataCpp Libs Vector Erase β†’