Defined in header <fkYAML/node.hpp>
fkyaml::ordered_map::at¶
template <typename KeyType>
mapped_type& at(KeyType&& key) noexcept;
template <typename KeyType>
const mapped_type& at(KeyType&& key) const
Accesses an element with the given key.
This function throws a [fkyaml::exception
] if the given key does not exist in the ordered_map object.
Template Parameters¶
- KeyType
- A type compatible with the key type.
Parameters¶
key
[in]- A key to the target value.
Return Value¶
(Constant) reference to a mapped_type
object associated with the given key.
Examples¶
Example
#include <iostream>
#include <fkYAML/node.hpp>
int main() {
fkyaml::ordered_map<std::string, fkyaml::node> om = {{"foo", 123}, {"bar", "baz"}};
std::cout << om.at("foo") << std::endl;
std::cout << om.at("bar") << std::endl;
// accesses with a unknown key will throw an exception.
try {
fkyaml::node& n = om.at("baz");
}
catch (const fkyaml::exception& e) {
std::cout << e.what() << std::endl;
}
return 0;
}
output: