#include <xtd/xtd>
auto main() -> int {
auto items = list {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
println("items[range {{3_i, ~2_i}}) = {}", items[range {3_i, ~2_i}]);
println(
"items[{{0, 8}}] = {}", items[{0, 8}]);
println(
"items[{{1_i, ~4_i}}][{{1_i, ~1_i}}] = {}", items[{1_i, ~4_i}][{1_i, ~1_i}]);
println(
"items[{{1_i, ~4_i}}].where(_ % 2 == 0).select(_ * _) = {}", items[{1_i, ~4_i}].where(_ % 2 == 0).
select(_ * _));
}
Represents a range that has start and end indexes.
Definition range.hpp:38
static auto end_at(index_type end) noexcept -> xtd::range
Creates a xtd::range object starting from the first element in the collection to a specified end inde...
Definition range.hpp:142
static auto all() noexcept -> xtd::range
Gets a xtd::range object that starts from the first element to the end.
Definition range.hpp:133
static auto start_at(index_type start) noexcept -> xtd::range
Creates a new xtd::range object starting from a specified start index to the end of the collection.
Definition range.hpp:160
constexpr auto select
The xtd::ranges::views::select instance.
Definition select.hpp:40
auto println(FILE *file) -> void
Writes the current line terminator to the file output stream using the specified format information.
Definition println.hpp:15
static constexpr auto from_end(xtd::integer auto value)
Creates an xtd::ndex from the end of a collection at a specified index position.
Definition index.hpp:181
static constexpr auto from_start(xtd::integer auto value)
Creates an xtd::ndex from the start of a collection at a specified index position.
Definition index.hpp:186
The following example shows how to use range literal operator. #include <xtd/xtd>
auto main() -> int {
auto items = list {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
println(R
"(items["0..10"_r] = {})", items["0..10"_r]);
println(R
"(items["2..6"_r] = {})", items["2..6"_r]);
println(R
"(items["0..~0"_r] = {})", items["0..~0"_r]);
println(R
"(items["2..~4"_r] = {})", items["2..~4"_r]);
println(R
"(items["~10..~0"_r] = {})", items["~10..~0"_r]);
println(R
"(items["~8..~4"_r] = {})", items["~8..~4"_r]);
}
The following example shows how to create your own collection with #include <xtd/xtd>
struct persona {
string name;
int age;
};
class persona_collection {
public:
persona_collection() = default;
persona_collection(std::initializer_list<persona> items) : items_ {items} {}
auto to_string() const noexcept ->
string {
return string::format(
"[{}]", string::join(
", ", items_));}
auto operator [](usize index) -> persona& {return items_[index];}
auto operator [](usize index) const -> const persona& {return items_[index];}
auto operator [](
const xtd::range& range) -> span<persona> {
return span<persona> {items_,
range};}
auto operator [](
const xtd::range& range)
const -> read_only_span<persona> {
return read_only_span<persona> {items_,
range};}
private:
list<persona> items_;
};
auto main() -> int {
auto items = persona_collection {{.name = "Oliver Queen", .age = 24}, {.name = "Laurel Lance", .age = 23}, {.name = "John \"Dig\" Diggle", .age = 27}, {.name = "Thea Queen", .age = 20}, {.name = "Felicity Smoak", .age = 22}};
println(
"items[range::all()] = {}", items[range::all()]);
println(
"items[range {{2_i, ~0_i}}] = {}", items[range {2_i, ~0_i}]);
println(
"items[range {{0, 4}}] = {}", items[range {0, 4}]);
println(R
"(items["1..3"_r] = {})", items["1..3"_r]);
}
virtual auto to_string() const -> xtd::string
Returns a xtd::string that represents the current object.
auto range(type_t count)
Generates a sequence of integral numbers within a specified range.
Definition range.hpp:39
auto to_string() const noexcept -> xtd::string override
Returns the string representation of this xtd::read_only_span <type_t> object.
Definition read_only_span.hpp:354