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

◆ add_range() [2/2]

template<typename type_t , typename allocator_t = xtd::collections::generic::helpers::allocator<typename std::conditional<std::is_same<bool, type_t>::value, char, type_t>::type>>
void xtd::collections::generic::list< type_t, allocator_t >::add_range ( std::initializer_list< type_t >  il)
inline

Adds copy of elements from the specified collection to the end of the xtd::collections::generic::list <type_t>.

Parameters
ilThe collection whose elements should be added to the end of the xtd::collections::generic::list <type_t>.
Examples
The following code example demonstrates the xtd::collections::generic::list <type_t> constructor and various methods of the xtd::collections::generic::list <type_t> class that act on ranges. An array of strings is created and passed to the constructor, populating the list with the elements of the array. The xtd::collections::generic::list::capacity property is then displayed, to show that the initial capacity is exactly what is required to hold the input elements.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::collections::generic;
class example {
public:
static auto main() -> void {
auto input = array<string> {"Brachiosaurus", "Amargasaurus", "Mamenchisaurus"};
auto dinosaurs = list<string>(input);
console::write_line("\ncapacity: {0}", dinosaurs.capacity());
console::write_line();
for (auto dinosaur : dinosaurs)
console::write_line(dinosaur);
console::write_line("\nadd_range(dinosaurs)");
dinosaurs.add_range(dinosaurs);
console::write_line();
for(auto dinosaur : dinosaurs)
console::write_line(dinosaur);
console::write_line("\nremove_range(2, 2)");
dinosaurs.remove_range(2, 2);
console::write_line();
for(auto dinosaur : dinosaurs)
console::write_line(dinosaur);
input = {"Tyrannosaurus", "Deinonychus", "Velociraptor"};
console::write_line("\ninsert_range(3, input)");
dinosaurs.insert_range(3, input);
console::write_line();
for(auto dinosaur : dinosaurs)
console::write_line(dinosaur);
console::write_line("\noutput = dinosaurs.get_range(2, 3).to_array()");
auto output = dinosaurs.get_range(2, 3).to_array();
console::write_line();
for (auto dinosaur : output)
console::write_line(dinosaur);
}
};
startup_(example::main);
// This code produces the following output :
//
//
// capacity: 3
//
// Brachiosaurus
// Amargasaurus
// Mamenchisaurus
//
// add_range(dinosaurs)
//
// Brachiosaurus
// Amargasaurus
// Mamenchisaurus
// Brachiosaurus
// Amargasaurus
// Mamenchisaurus
//
// remove_range(2, 2)
//
// Brachiosaurus
// Amargasaurus
// Amargasaurus
// Mamenchisaurus
//
// insert_range(3, input)
//
// Brachiosaurus
// Amargasaurus
// Amargasaurus
// Tyrannosaurus
// Deinonychus
// Velociraptor
// Mamenchisaurus
//
// output = dinosaurs.get_range(2, 3).to_array()
//
// Amargasaurus
// Tyrannosaurus
// Deinonychus
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
The order of the elements in the collection is preserved in the xtd::collections::generic::list <type_t>.
If the new xtd::collections::generic::list::count (the current xtd::collections::generic::list::count plus the size of the collection) will be greater than xtd::collections::generic::list::capacity, the capacity of the xtd::collections::generic::list <type_t> is increased by automatically reallocating the internal array to accommodate the new elements, and the existing elements are copied to the new array before the new elements are added.
If the xtd::collections::generic::list <type_t> can accommodate the new elements without increasing the xtd::collections::generic::list::capacity, this method is an O(n) operation, where n is the number of elements to be added. If the capacity needs to be increased to accommodate the new elements, this method becomes an O(n + m) operation, where n is the number of elements to be added and m is xtd::collections::generic::list::count.