JSON for Modern C++  3.7.0

◆ cbegin()

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>
const_iterator nlohmann::basic_json::cbegin ( ) const
inlinenoexcept

Returns a const iterator to the first element.

range-begin-end.svg
Illustration from cppreference.com
Returns
const iterator to the first element
Complexity
Constant.
Requirements
This function helps basic_json satisfying the Container requirements:
  • The complexity is constant.
  • Has the semantics of const_cast<const basic_json&>(*this).begin().
Example
The following code shows an example for cbegin().
1 #include <iostream>
2 #include <nlohmann/json.hpp>
3 
4 using json = nlohmann::json;
5 
6 int main()
7 {
8  // create an array value
9  const json array = {1, 2, 3, 4, 5};
10 
11  // get am iterator to the first element
13 
14  // serialize the element that the iterator points to
15  std::cout << *it << '\n';
16 }
basic_json<> json
default JSON class
Definition: json.hpp:2355
static basic_json array(initializer_list_t init={})
explicitly create an array from an initializer list
Definition: json.hpp:15905
const_iterator cbegin() const noexcept
returns a const iterator to the first element
Definition: json.hpp:18540
iter_impl< const basic_json > const_iterator
a const iterator for a basic_json container
Definition: json.hpp:14685

Output (play with this example online):
1
The example code above can be translated with
g++ -std=c++11 -Isingle_include doc/examples/cbegin.cpp -o cbegin 
See also
begin() – returns an iterator to the beginning
end() – returns an iterator to the end
cend() – returns a const iterator to the end
Since
version 1.0.0

Definition at line 18540 of file json.hpp.