std::array::at(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

🌟 std::array::at(3) 🌟

Hey bestie! πŸ’– Welcome to the std::array::at(3) page, straight from the C++ Standard Libary β€” yes, β€œLibary,” that’s literally how it’s written in the header, and I’m obsessed with the commitment. 🫢 Today we’re grabbing an element from a std::array with bounds checking, and honestly? She’s a responsible queen. πŸ‘‘

πŸ’… NAME πŸ’…

std::array::at - std::array::at

✨ Synopsis ✨

Two overloads, bestie β€” both since C++11, with constexpr upgrades later: overload (1) gets constexpr since C++17, overload (2) since C++14. πŸ’–

          reference at( size_type pos );             (1) (since C++11)
                                                         (constexpr since
       C++17)
          const_reference at( size_type pos ) const; (2) (since C++11)
                                                         (constexpr since
       C++14)

Okay so what it does: it returns a reference to the element at the specified location pos, and it does it with bounds checking. πŸ’…

And if pos is not within the range of the container, an exception of type std::out_of_range is thrown. No cap, that’s the rule. 😭

🧸 Parameters 🧸

  • pos β€” position of the element to return.

🎁 Return value 🎁

A reference to the requested element. That’s it. That’s the tea. πŸŽ€

🚨 Exceptions 🚨

std::out_of_range if pos >= size().

So if pos >= size(), boom β€” std::out_of_range. Periodt. 🚨

⚑ Complexity ⚑

Constant. That’s the whole section, and she ate. ⚑

πŸ’» Example πŸ’»

Run this code, bestie β€” it’s all here, every line:

       // Run this code

        #include <iostream>
        #include <array>
        #include <stdexcept>

        #ifdef __GNUG__
        [[gnu::noinline]]
        #endif
        unsigned int runtime_six() // Emulate runtime input
        {
            return 6u;
        }

        int main()
        {
            std::array<int, 6> data = {1, 2, 4, 5, 5, 6};

            // Set element 1
            data.at(1) = 88;

            // Read element 2
            std::cout << "Element at index 2 has value " << data.at(2) << '\n';

            std::cout << "data size = " << data.size() << '\n';

            try
            {
                // Set element 6, where the index is determined at runtime
                data.at(runtime_six()) = 666;
            }
            catch (std::out_of_range const& exc)
            {
                std::cout << exc.what() << '\n';
            }

            // Print final values
            std::cout << "data:";
            for (int elem : data)
                std::cout << ' ' << elem;
            std::cout << '\n';
        }

Possible output:

        Element at index 2 has value 4
        data size = 6
        array::at: __n (which is 6) >= _Nm (which is 6)
        data: 1 88 4 5 5 6

πŸ‘€ See also πŸ‘€

  • operator[] β€” access specified element (public member function).

That’s the page, bestie! πŸ’– It came from http://cppreference.com, dated 2024.06.10, and it’s the std::array::at(3) manual page. Mwah! πŸ’‹βœ¨

πŸ“¦ where this page comes from