xtd 0.2.0
Loading...
Searching...
No Matches

◆ copy_to()

template<typename type_t >
void xtd::collections::object_model::read_only_collection< type_t >::copy_to ( xtd::array< type_t > &  array,
xtd::size  array_index 
) const
inlineoverridevirtual

Copies the entire xtd::collections::object_model::read_only_collection <type_t> to a compatible one-dimensional Array, starting at the specified index of the target array.

Parameters
arrayThe one-dimensional xtd::array that is the destination of the elements copied from xtd::collections::object_model::read_only_collection <type_t>. The xtd::array must have zero-based indexing.
array_indexThe zero-based index in array at which copying begins.
Exceptions
xtd::argument_exceptionThe number of elements in the source xtd::collections::object_model::read_only_collection <type_t> is greater than the available space from index to the end of the destination array.
Examples
The following code example demonstrates several members of the xtd::collections::object_model::read_only_collection <type_t> class. The code example creates a xtd::collections::generic::list <type_t> of strings and adds four dinosaur names to it. The code example then wraps the list in a xtd::collections::object_model::read_only_collection <type_t>.

After demonstrating the xtd::collections::object_model::read_only_collection::count, xtd::collections::object_model::read_only_collection::contains, xtd::collections::object_model::read_only_collection:: opertor [], and xtd::collections::generic::ilist::index_of members, the code example shows that the xtd::collections::object_model::read_only_collection <type_t> is just a wrapper for the original xtd::collections::generic::list <type_t> by adding a new item to the xtd::collections::generic::list <type_t> and displaying the contents of the xtd::collections::object_model::read_only_collection <type_t>.

Finally, the code example creates an array larger than the collection and uses the xtd::collections::object_model::read_only_collection::copy_to method to insert the elements of the collection into the middle of the array.

#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"
// ""
void add(const type_t &item) override
Adds an object to the end of the xtd::collections::generic::list <type_t>.
Definition list.h:386
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:72
Represents the standard input, output, and error streams for console applications.
Definition console.h:36
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.h:15
std::vector< type_t > array
Definition __array_definition.h:18
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Remarks
This method uses xtd::array::copy to copy the elements.
The elements are copied to the xtd::array in the same order that the enumerator iterates through the xtd::collections::object_model::read_only_collection <type_t>.
This method is an O(n) operation, where n is xtd::collections::object_model::read_only_collection::count.

Implements xtd::collections::generic::icollection< type_t >.