[ Web Proxy ]
URL:
Viewing: http://ja.cppreference.com/cpp/string/basic_string/at [Back]  [Original]

std::basic_string<CharT,Traits,Allocator>::at - cppreference.com
cppreference.com

std::basic_string<CharT,Traits,Allocator>::at

: cppreference.com
 
C++
(C++20)
(C++20)
(C++17)
(C++11)
(C++11)
(C++11)
 
 
std::basic_string
 
<tbody> </tbody> <tbody class="t-dcl-rev "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev "> </tbody><tbody> </tbody>
reference at( size_type pos );
(C++20)
constexpr reference at( size_type pos );
(C++20)
const_reference at( size_type pos ) const;
(C++20)
constexpr const_reference at( size_type pos ) const;
(C++20)

pos std::out_of_range

pos -

pos >= size() std::out_of_range

#include <stdexcept>
#include <iostream>
#include <string>

int main()
{
    std::string s("message"); // for capacity

    s = "abc";
    s.at(2) = 'x'; // ok
    std::cout << s << '\n';

    std::cout << "string size = " << s.size() << '\n';
    std::cout << "string capacity = " << s.capacity() << '\n';

    try {
        // throw, even if capacity allowed to access element
        s.at(3) = 'x';
    }
    catch (std::out_of_range const& exc) {
        std::cout << exc.what() << '\n';
    }
}

:

abx
string size = 3
string capacity = 7
basic_string::at


() [edit]

Web Proxy Viewer  |  New URL  |  Original Page