xtd 0.2.0
Loading...
Searching...
No Matches
read_only_collection.cpp

Shows how to use xtd::collections::object_model::read_only_collection class.

#include <xtd/xtd>
using namespace xtd;
using namespace xtd::collections::generic;
class example {
public:
static auto main() -> void {
auto dinosaurs = list<string> {};
dinosaurs.add("Tyrannosaurus");
dinosaurs.add("Amargasaurus");
dinosaurs.add("Deinonychus");
dinosaurs.add("Compsognathus");
auto read_only_dinosaurs = dinosaurs.as_read_only();
for (auto dinosaur : read_only_dinosaurs)
console::write_line(dinosaur);
console::write_line("\ncount: {0}", read_only_dinosaurs.count());
console::write_line("\ncontains(\"Deinonychus\"): {0}", dinosaurs.contains("Deinonychus"));
console::write_line("\nread_only_dinosaurs[3]: {0}", dinosaurs[3]);
console::write_line("\nindex_of(\"Compsognathus\"): {0}", read_only_dinosaurs.index_of("Compsognathus"));
console::write_line("\nInsert into the wrapped List:");
console::write_line("insert(2, \"Oviraptor\")");
dinosaurs.insert(2, "Oviraptor");
console::write_line();
for (auto dinosaur : read_only_dinosaurs)
console::write_line(dinosaur);
auto dino_array = array<string>(read_only_dinosaurs.count() + 2);
read_only_dinosaurs.copy_to(dino_array, 1);
console::write_line("\nCopied array has {0} elements:", dino_array.size());
for (auto dinosaur : dino_array )
console::write_line("\"{0}\"", dinosaur);
}
};
startup_(example::main);
// This code produces the following output :
//
// Tyrannosaurus
// Amargasaurus
// Deinonychus
// Compsognathus
//
// count: 4
//
// contains("Deinonychus"): true
//
// read_only_dinosaurs[3]: Compsognathus
//
// index_of("Compsognathus"): 3
//
// Insert into the wrapped List:
// insert(2, "Oviraptor")
//
// Tyrannosaurus
// Amargasaurus
// Oviraptor
// Deinonychus
// Compsognathus
//
// Copied array has 7 elements:
// ""
// "Tyrannosaurus"
// "Amargasaurus"
// "Oviraptor"
// "Deinonychus"
// "Compsognathus"
// ""
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:58
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:71
void add(const type_t &item) override
Adds an object to the end of the xtd::collections::generic::list <type_t>.
Definition list.hpp:385
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:175
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:15
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10