template<template< typename U, typename V, typename... Args > class ObjectType = std::map, template< typename U, typename... Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
Returns a const reference to the element at specified location idx, with bounds checking.
- Parameters
-
| [in] | idx | index of the element to access |
- Returns
- const reference to the element at index idx
- Exceptions
-
| type_error.304 | if the JSON value is not an array; in this case, calling at with an index makes no sense. See example below. |
| out_of_range.401 | if the index idx is out of range of the array; that is, idx >= size(). See example below. |
- Exception safety
- Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
- Complexity
- Constant.
- Since
- version 1.0.0
- Example
- The example below shows how array elements can be read using
at(). It also demonstrates the different exceptions that can be thrown. 8 const json array = {
"first",
"2nd",
"third",
"fourth"};
11 std::cout << array.at(2) <<
'\n';
18 const json str =
"I am a string";
19 std::cout << str.at(0) <<
'\n';
23 std::cout << e.what() <<
'\n';
30 std::cout << array.at(5) <<
'\n';
34 std::cout << e.what() <<
'\n';
basic_json<> json
default JSON class
detail::out_of_range out_of_range
exception indicating access out of the defined range
detail::type_error type_error
exception indicating executing a member function with a wrong type
Output (play with this example online): "third"
[json.exception.type_error.304] cannot use at() with string
[json.exception.out_of_range.401] array index 5 is out of range
The example code above can be translated withg++ -std=c++11 -Isrc doc/examples/
at__size_type_const.cpp -o
at__size_type_const
Definition at line 3831 of file json.hpp.