Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/basic string/at"

From cppreference.com
< cpp‎ | string‎ | basic string
m (Catch exceptions by const reference)
(Added LWG issue #2207 DR.)
 
(12 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{cpp/string/basic_string/title | at}}
+
{{cpp/string/basic_string/title|at}}
 
{{cpp/string/basic_string/navbar}}
 
{{cpp/string/basic_string/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl |  
+
{{dcla|anchor=no|num=1|constexpr=c++20|
reference      at( size_type pos );
+
CharT& at( size_type pos );
 
}}
 
}}
{{dcl |  
+
{{dcla|anchor=no|num=2|constexpr=c++20|
const_reference at( size_type pos ) const;
+
const CharT& at( size_type pos ) const;
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
Returns a reference to the character at specified location {{tt|pos}}. Bounds checking is performed, exception of type {{lc|std::out_of_range}} will be thrown on invalid access.
+
Returns a reference to the character at specified location {{c|pos}}. Bounds checking is performed, exception of type {{lc|std::out_of_range}} will be thrown on invalid access.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | pos | position of the character to return}}
+
{{par|pos|position of the character to return}}
 
{{par end}}
 
{{par end}}
  
Line 22: Line 22:
 
===Exceptions===
 
===Exceptions===
 
Throws {{lc|std::out_of_range}} if {{c|1=pos >= size()}}.
 
Throws {{lc|std::out_of_range}} if {{c|1=pos >= size()}}.
 +
 +
{{cpp/strong exception safety guarantee|plural=yes}}
  
 
===Complexity===
 
===Complexity===
Line 27: Line 29:
  
 
===Example===
 
===Example===
{{
+
{{example
example
+
|code=
| code=
+
#include <stdexcept>
+
 
#include <iostream>
 
#include <iostream>
 +
#include <stdexcept>
 
#include <string>
 
#include <string>
  
Line 37: Line 38:
 
{
 
{
 
     std::string s("message"); // for capacity
 
     std::string s("message"); // for capacity
 
+
   
 
     s = "abc";
 
     s = "abc";
     s.at(2) = 'x'; // ok
+
     s.at(2) = 'x'; // OK
 
     std::cout << s << '\n';
 
     std::cout << s << '\n';
 
+
   
 
     std::cout << "string size = " << s.size() << '\n';
 
     std::cout << "string size = " << s.size() << '\n';
 
     std::cout << "string capacity = " << s.capacity() << '\n';
 
     std::cout << "string capacity = " << s.capacity() << '\n';
 
+
   
     try {
+
     try
         // throw, even if capacity allowed to access element
+
    {
 +
         // This will throw since the requested offset is greater than the current size.
 
         s.at(3) = 'x';
 
         s.at(3) = 'x';
 
     }
 
     }
     catch (std::out_of_range const& exc) {
+
     catch (std::out_of_range const& exc)
 +
    {
 
         std::cout << exc.what() << '\n';
 
         std::cout << exc.what() << '\n';
 
     }
 
     }
 
}
 
}
| output=
+
|p=true
 +
|output=
 
abx
 
abx
 
string size = 3
 
string size = 3
Line 60: Line 64:
 
}}
 
}}
  
===See also===
+
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=847|std=C++98|before=there was no exception safety guarantee|after=added strong exception safety guarantee}}
 +
{{dr list item|wg=lwg|dr=2207|std=C++98|before=the behavior was undefined if {{c|1=pos >= size()}}|after=always throws an exception in this case}}
 +
{{dr list end}}
  
 +
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/string/basic_string/dsc operator_at }}
+
{{dsc inc|cpp/string/basic_string/dsc operator_at}}
 +
{{dsc inc|cpp/string/basic_string_view/dsc {{SUBPAGENAMEE}}}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/string/basic string/at]]
+
{{langlinks|de|es|fr|it|ja|pl|pt|ru|zh}}
[[es:cpp/string/basic string/at]]
+
[[fr:cpp/string/basic string/at]]
+
[[it:cpp/string/basic string/at]]
+
[[ja:cpp/string/basic string/at]]
+
[[pl:cpp/string/basic string/at]]
+
[[pt:cpp/string/basic string/at]]
+
[[ru:cpp/string/basic string/at]]
+
[[zh:cpp/string/basic string/at]]
+

Latest revision as of 21:45, 14 November 2024

 
 
 
std::basic_string
Member functions
Element access
basic_string::at
Iterators
Capacity
Modifiers
Search
Operations
Constants
Non-member functions
I/O
Comparison
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Numeric conversions
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Literals
Helper classes
Deduction guides (C++17)

 
CharT& at( size_type pos );
(1) (constexpr since C++20)
const CharT& at( size_type pos ) const;
(2) (constexpr since C++20)

Returns a reference to the character at specified location pos. Bounds checking is performed, exception of type std::out_of_range will be thrown on invalid access.

Contents

[edit] Parameters

pos - position of the character to return

[edit] Return value

Reference to the requested character.

[edit] Exceptions

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

If an exception is thrown for any reason, these functions have no effect (strong exception safety guarantee).

[edit] Complexity

Constant.

[edit] Example

#include <iostream>
#include <stdexcept>
#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
    {
        // This will throw since the requested offset is greater than the current size.
        s.at(3) = 'x';
    }
    catch (std::out_of_range const& exc)
    {
        std::cout << exc.what() << '\n';
    }
}

Possible output:

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

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 847 C++98 there was no exception safety guarantee added strong exception safety guarantee
LWG 2207 C++98 the behavior was undefined if pos >= size() always throws an exception in this case

[edit] See also

accesses the specified character
(public member function) [edit]
accesses the specified character with bounds checking
(public member function of std::basic_string_view<CharT,Traits>) [edit]