#include <py2cpp/lict.hpp>
template<typename T>
Lict class
Dict-like data structure by std::vector and Range.
Template parameters | |
---|---|
T |
The Lict
class is a custom implementation of an unordered mapping with integer keys and generic values, which adapts a vector to behave like a dictionary.
Public types
- using key_type = size_t
- using value_type = T
-
using iterator = py::
Range<key_ type>::iterator -
using const_iterator = py::
Range<key_ type>::iterator
Constructors, destructors, conversion operators
- Lict(std::vector<T> lst) explicit
- Constructor for a dictionary-like adaptor for a vector.
Public functions
-
auto operator[](const key_
type& key) const -> const T& - This function allows you to access an element in a Lict object by its index.
-
auto operator[](const key_
type& key) -> T& - This function sets the value at a given index in a list-like object.
-
auto at(const key_
type& key) const -> const T& - This function allows you to access an element in a Lict object by its index.
- auto begin() const -> iterator
- auto end() const -> iterator
-
auto contains(const key_
type& key) const -> bool - The
contains
function checks if a given value is present in therng
attribute of the object. - auto size() const -> size_t
- This function returns the length of the
rng
attribute of the object. - auto values() const -> const auto&
- The
values
function returns an iterator that yields the elements of thelst
attribute of theLict
object. - auto values() -> auto&
- The
values
function returns an iterator that yields the elements of thelst
attribute of theLict
object. - auto items() -> auto
- The function returns an enumeration of the items in the list.
Function documentation
template<typename T>
py:: Lict<T>:: Lict(std::vector<T> lst) explicit
Constructor for a dictionary-like adaptor for a vector.
Parameters | |
---|---|
lst in | The lst parameter is a vector. It is used to initialize the self.lst attribute of the class |
template<typename T>
const T& py:: Lict<T>:: operator[](const key_ type& key) const
This function allows you to access an element in a Lict object by its index.
Parameters | |
---|---|
key in | The key parameter is of type size_t and it represents the index of the element that you want to retrieve from the list |
Returns | the item at the specified index in the lst attribute. |
Examples:
auto a = Lict({1, 4, 3, 6}); a[2]
3
template<typename T>
T& py:: Lict<T>:: operator[](const key_ type& key)
This function sets the value at a given index in a list-like object.
Parameters | |
---|---|
key in | The key parameter represents the index at which the new value should be set in the list |
Examples:
auto a = Lict({1, 4, 3, 6}); a[2] = 7 a[2]
7
template<typename T>
const T& py:: Lict<T>:: at(const key_ type& key) const
This function allows you to access an element in a Lict object by its index.
Parameters | |
---|---|
key in | The key parameter is of type size_t and it represents the index of the element that you want to retrieve from the list |
Returns | the item at the specified index in the lst attribute. |
Examples:
auto a = Lict({1, 4, 3, 6}); a.at(2)
3
template<typename T>
bool py:: Lict<T>:: contains(const key_ type& key) const
The contains
function checks if a given value is present in the rng
attribute of the object.
Parameters | |
---|---|
key in | The key parameter represents the value that we want to check if it is present in the self.rng attribute |
Returns | The method is returning a boolean value, indicating whether the given value is present in the self.rng attribute. |
Examples:
auto a = Lict({1, 4, 3, 6}); a.contains(2)
true
template<typename T>
const auto& py:: Lict<T>:: values() const
The values
function returns an iterator that yields the elements of the lst
attribute of the Lict
object.
Returns | The values method returns a const reference to the vector object. |
---|
Examples:
const auto a = Lict({1, 4, 3, 6}); for (const auto &i : a.values()) {
... fmt::print(i); ... } 1 4 3 6
template<typename T>
auto& py:: Lict<T>:: values()
The values
function returns an iterator that yields the elements of the lst
attribute of the Lict
object.
Returns | The values method returns a reference to the vector object. |
---|
Examples:
auto a = Lict({1, 4, 3, 6}); for (auto& i : a.values()) {
... i += 1; ... fmt::print(i); ... } 2 5 4 7
template<typename T>
auto py:: Lict<T>:: items()
The function returns an enumeration of the items in the list.
Returns | : The items method is returning an enumeration of the lst attribute. |
---|
Examples:
auto a = Lict({1, 4, 3, 6}); for (auto& [key, value] : a.items()) {
... fmt::print(key, value); ... } (0, 1) (1, 4) (2, 3) (3, 6)