TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ operator[]() [5/8]

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, class BinaryType = std::vector<std::uint8_t>>
reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::operator[] ( size_type  idx)
inline

access specified array element

Returns a reference to the element at specified location idx.

Note
If idx is beyond the range of the array (i.e., idx >= size()), then the array is silently filled up with null values to make idx a valid reference to the last stored element.
Parameters
[in]idxindex of the element to access
Returns
reference to the element at index idx
Exceptions
type_error.305if the JSON value is not an array or null; in that cases, using the [] operator with an index makes no sense.

@complexity Constant if idx is in the range of the array. Otherwise linear in idx - size().

@liveexample{The example below shows how array elements can be read and written using [] operator. Note the addition of null values.,operatorarray__size_type}

Since
version 1.0.0

Definition at line 21385 of file json.hpp.

21386 {
21387 // implicitly convert null value to an empty array
21388 if (is_null())
21389 {
21390 m_type = value_t::array;
21391 m_value.array = create<array_t>();
21393 }
21394
21395 // operator[] only works for arrays
21396 if (JSON_HEDLEY_LIKELY(is_array()))
21397 {
21398 // fill up array with null values if given idx is outside range
21399 if (idx >= m_value.array->size())
21400 {
21401#if JSON_DIAGNOSTICS
21402 // remember array size before resizing
21403 const auto previous_size = m_value.array->size();
21404#endif
21405 m_value.array->resize(idx + 1);
21406#if JSON_DIAGNOSTICS
21407 // set parent for values added above
21408 set_parents(begin() + static_cast<typename iterator::difference_type>(previous_size), static_cast<typename iterator::difference_type>(idx + 1 - previous_size));
21409#endif
21410 }
21411
21412 return m_value.array->operator[](idx);
21413 }
21414
21415 JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), *this));
21416 }
iterator begin() noexcept
returns an iterator to the first element
Definition: json.hpp:22377
void assert_invariant(bool check_parents=true) const noexcept
checks the class invariants
Definition: json.hpp:18935
json_value m_value
the value of the current element
Definition: json.hpp:24928
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:20432
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:24883
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:20251
typename BasicJsonType::difference_type difference_type
a type to represent differences between iterators
Definition: json.hpp:11740
@ array
array (ordered collection of values)