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>>
class xtd::collections::generic::list< type_t, allocator_t >
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.
template<typename type_t>
Represents a collection of objects that can be individually accessed by index.
Definition ilist.h:41
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:71
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.h:22
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
- Header
#include <xtd/collections/generic/list
- Namespace
- xtd::collections::generic
- Library
- xtd.core
- Performance considerations
- As xtd::collections::generic::list <type_t> instantiates and uses only the methods of std::vector, the performance of xtd::collections::generic::list <type_t> is practically identical to that of std::vector.
- Examples
- The following example demonstrates how to add, remove, and insert a simple business object in a xtd::collections::generic::list <type_t>.
#include <xtd/xtd>
public:
string part_name;
int part_id = 0;
part() = default;
part(const string& name, int id) : part_name {name}, part_id {id} {}
string to_string() const noexcept
override {
return string::format(
"ID: {} Name: {}", part_id, part_name);}
bool equals(const object& obj) const noexcept override {return is<part>(obj) && equals(as<part>(obj));}
bool equals(
const part& other)
const noexcept override {
return part_id ==
other.part_id;}
size get_hash_code() const noexcept
override {
return object::get_hash_code();}
};
class example {
public:
static auto main() -> void {
console::write_line("\ncapacity: {0}", parts.capacity());
parts.add(part {"crank arm", 1234});
parts.add(part {"chain ring", 1334});
parts.add(part {"seat", 1434});
parts.add(part {"cassette", 1534});
parts.add(part {"shift lever", 1634});
console::write_line();
for (auto part : parts)
console::write_line("\ncapacity: {0}", parts.capacity());
console::write_line("count: {0}", parts.count());
parts.trim_excess();
console::write_line("\ntrim_excess()");
console::write_line("capacity: {0}", parts.capacity());
console::write_line("count: {0}", parts.count());
parts.clear();
console::write_line("\nclear()");
console::write_line("capacity: {0}", parts.capacity());
console::write_line("count: {0}", parts.count());
}
};
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
size_t size
Represents a size of any object in bytes.
Definition size.h:23
std::string to_string(const value_t &value, const std::string &fmt, const std::locale &loc)
Convert a specified value into a string with specified format and locale.
Definition to_string.h:41
@ other
The operating system is other.
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.h:15
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
The following example demonstrates several properties and methods of the xtd::collections::generic::list <type_t> generic class of type string. (For an example of a xtd::collections::generic::list <type_t> of complex types, see the xtd::collections::generic::list::contains method.)
The parameterless constructor is used to create a list of strings with the default capacity. The xtd::collections::generic::list::capacity property is displayed and then the xtd::collections::generic::list::add method is used to add several items. The items are listed, and the xtd::collections::generic::list::capacity property is displayed again, along with the xtd::collections::generic::list::count property, to show that the capacity has been increased as needed.
The xtd::collections::generic::list::contains method is used to test for the presence of an item in the list, the Insert method is used to insert a new item in the middle of the list, and the contents of the list are displayed again.
The default xtd::collections::generic::list::operator [] is used to retrieve an item, the xtd::collections::generic::list::remove method is used to remove the first instance of the duplicate item added earlier, and the contents are displayed again. The xtd::collections::generic::list::remove method always removes the first instance it encounters.
The xtd::collections::generic::list::trim_excess method is used to reduce the capacity to match the count, and the xtd::collections::generic::list::capacity and xtd::collections::generic::list::count properties are displayed. If the unused capacity had been less than 10 percent of total capacity, the list would not have been resized.
Finally, the xtd::collections::generic::list::clear method is used to remove all items from the list, and the xtd::collections::generic::list::capacity and xtd::collections::generic::list::count properties are displayed.
#include <xtd/xtd>
class example {
public:
static auto main() -> void {
console::write_line("\ncapacity: {0}", dinosaurs.capacity());
dinosaurs.add("Tyrannosaurus");
dinosaurs.add("Amargasaurus");
dinosaurs.add("Mamenchisaurus");
dinosaurs.add("Deinonychus");
dinosaurs.add("Compsognathus");
console::write_line();
for (auto dinosaur : dinosaurs)
console::write_line("\ncapacity: {0}", dinosaurs.capacity());
console::write_line("count: {0}", dinosaurs.count());
console::write_line("\ncontains(\"Deinonychus\"): {0}", dinosaurs.contains("Deinonychus"));
console::write_line("\ninsert(2, \"Compsognathus\")");
dinosaurs.insert(2, "Compsognathus");
console::write_line();
for (auto dinosaur : dinosaurs)
console::write_line("\ndinosaurs[3]: {0}", dinosaurs[3]);
console::write_line("\nremove(\"Compsognathus\")");
dinosaurs.remove("Compsognathus");
console::write_line();
for (auto dinosaur : dinosaurs)
dinosaurs.trim_excess();
console::write_line("\ntrim_excess()");
console::write_line("capacity: {0}", dinosaurs.capacity());
console::write_line("count: {0}", dinosaurs.count());
dinosaurs.clear();
console::write_line("\nclear()");
console::write_line("capacity: {0}", dinosaurs.capacity());
console::write_line("count: {0}", dinosaurs.count());
}
};
- Examples
- action.cpp, animation.cpp, array_list.cpp, array_list2.cpp, assert_does_not_throw.cpp, assert_throws.cpp, assert_throws_any.cpp, assume_does_not_throw.cpp, assume_throws.cpp, assume_throws_any.cpp, auto_reset_event.cpp, border_style.cpp, boxing.cpp, calculator.cpp, collection_assert.cpp, collection_assert_all_items_are_instances_of.cpp, collection_assert_all_items_are_not_null.cpp, collection_assert_all_items_are_unique.cpp, collection_assert_are_equal.cpp, collection_assert_are_equivalent.cpp, collection_assert_are_not_equal.cpp, collection_assert_are_not_equivalent.cpp, collection_assert_contains.cpp, collection_assert_does_not_contain.cpp, collection_assert_is_empty.cpp, collection_assert_is_not_empty.cpp, collection_assert_is_ordered.cpp, collection_assume.cpp, collection_assume_all_items_are_instances_of.cpp, collection_assume_all_items_are_not_null.cpp, collection_assume_all_items_are_unique.cpp, collection_assume_are_equal.cpp, collection_assume_are_equivalent.cpp, collection_assume_are_not_equal.cpp, collection_assume_are_not_equivalent.cpp, collection_assume_contains.cpp, collection_assume_does_not_contain.cpp, collection_assume_is_empty.cpp, collection_assume_is_not_empty.cpp, collection_assume_is_ordered.cpp, collection_valid.cpp, collection_valid_all_items_are_instances_of.cpp, collection_valid_all_items_are_not_null.cpp, collection_valid_all_items_are_unique.cpp, collection_valid_are_equal.cpp, collection_valid_are_equivalent.cpp, collection_valid_are_not_equal.cpp, collection_valid_are_not_equivalent.cpp, collection_valid_contains.cpp, collection_valid_does_not_contain.cpp, collection_valid_is_empty.cpp, collection_valid_is_not_empty.cpp, collection_valid_is_ordered.cpp, colored_forms.cpp, colored_tab_pages.cpp, console_clear.cpp, console_color2.cpp, console_color3.cpp, console_color4.cpp, console_firework.cpp, console_song.cpp, console_song2.cpp, console_song3.cpp, console_unicode.cpp, date_time_add_hours.cpp, date_time_add_minutes.cpp, date_time_now.cpp, date_time_now2.cpp, date_time_today.cpp, default_trace_listener.cpp, dot_matrix_display.cpp, draw_point.cpp, environment_exit.cpp, environment_program_exit.cpp, fibonacci_series.cpp, file_info_move_to.cpp, font_families.cpp, form_paint.cpp, form_show.cpp, format_class.cpp, format_class_object.cpp, format_sequence_containers.cpp, fourteen_segment_display.cpp, generic_icollection.cpp, generic_ienumerable.cpp, generic_ienumerable2.cpp, generic_ienumerator.cpp, generic_ilist.cpp, generic_list.cpp, generic_list2.cpp, generic_list3.cpp, generic_list4.cpp, generic_list_as_read_only.cpp, graph_control.cpp, graphics.cpp, ienumerable.cpp, ienumerator.cpp, iobservable_iobserver.cpp, ip_address.cpp, labels_and_unicode_text.cpp, lock_guard.cpp, main.cpp, main3.cpp, main4.cpp, manual_reset_event.cpp, monitor.cpp, mutex.cpp, nine_segment_display.cpp, open_file_box.cpp, painting.cpp, random1.cpp, random2.cpp, random3.cpp, read_only_collection.cpp, semaphore.cpp, seven_segment_display.cpp, sixteen_segment_display.cpp, socket_tcp_ip_v4.cpp, socket_tcp_ip_v4_without_thread.cpp, socket_tcp_ip_v6.cpp, socket_udp_ip_v4.cpp, socket_udp_ip_v6.cpp, sprintf_class.cpp, sprintf_class_with_specified_formating.cpp, sprintf_class_with_specified_formating_with_to_string.cpp, startup3.cpp, startup4.cpp, stopwatch.cpp, test_forms.cpp, udp_client_ip_v4.cpp, udp_client_ip_v6.cpp, user_control.cpp, valid_does_not_throw.cpp, valid_throws.cpp, valid_throws_any.cpp, and wiggly.cpp.
|
| list () noexcept=default |
| Initializes a new instance of the xtd::collections::generic::list class that is empty.
|
|
| list (const allocator_type &alloc) noexcept |
| Constructs an empty container with the given allocator.
|
|
| list (size_type count, const type_t &value, const allocator_type &alloc=allocator_type()) |
| Constructs the container with specified count copies of elements with specified value.
|
|
| list (size_type count, const allocator_type &alloc=allocator_type()) |
| Constructs the container with specified count default-inserted instances of type_t. No copies are made.
|
|
template<typename input_iterator_t > |
| list (input_iterator_t first, input_iterator_t last, const allocator_type &alloc=allocator_type()) |
| Constructs the container with the contents of the range [first, last).
|
|
| list (const xtd::collections::generic::ienumerable< type_t > &collection, const allocator_type &alloc=allocator_type()) |
| Initializes a new instance of the xtd::collections::generic::list <type_t> class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.
|
|
| list (const list &list) |
| Default copy constructor with specified list.
|
|
| list (const base_type &list) |
| Copy constructor with specified base type list.
|
|
| list (const list &list, const allocator_type &alloc) |
| Default copy constructor with specified list, and allocator.
|
|
| list (const base_type &list, const allocator_type &alloc) |
| Default copy constructor with specified base type list, and allocator.
|
|
| list (std::initializer_list< type_t > items, const allocator_type &alloc=allocator_type()) |
| Constructs the container with the contents of the specified initializer list, and allocator.
|
|
| list (list &&other) |
| Move constructor with specified list.
|
|
| list (base_type &&other) |
| Move constructor with specified base type list.
|
|
| list (list &&other, const allocator_type &alloc) |
| Move constructor with specified list, and allocator.
|
|
| list (base_type &&other, const allocator_type &alloc) |
| Move constructor with specified base tyoe list, and allocator.
|
|
|
virtual reference | back () |
| Returns a reference to the last element in the container.
|
|
virtual const_reference | back () const |
| Returns a reference to the last element in the container.
|
|
const_iterator | begin () const noexcept override |
| Returns an iterator to the first element of the enumarable.
|
|
iterator | begin () noexcept override |
| Returns an iterator to the first element of the enumarable.
|
|
virtual size_type | capacity () const noexcept |
| Gets the total number of elements the internal data structure can hold without resizing.
|
|
virtual void | capacity (size_type value) |
| Sets the total number of elements the internal data structure can hold without resizing.
|
|
const_iterator | cbegin () const noexcept override |
| Returns an iterator to the first element of the enumarable.
|
|
const_iterator | cend () const noexcept override |
| Returns an iterator to the element following the last element of the enumarable.
|
|
size_type | count () const noexcept override |
| Gets the number of elements contained in the xtd::collections::generic::list <type_t>.
|
|
virtual const_reverse_iterator | crbegin () const noexcept |
| Returns a reverse iterator to the first element of the reversed vector. It corresponds to the last element of the non-reversed vector. If the vector is empty, the returned iterator is equal to xtd::collections::generic::list::rend().
|
|
virtual const_reverse_iterator | crend () const noexcept |
| Returns a reverse iterator to the element following the last element of the reversed vector. It corresponds to the element preceding the first element of the non-reversed vector. This element acts as a placeholder, attempting to access it results in undefined behavior.
|
|
virtual pointer | data () noexcept |
| Returns pointer to the underlying array serving as element storage.
|
|
virtual const_pointer | data () const noexcept |
| Returns pointer to the underlying array serving as element storage.
|
|
virtual bool | empty () const noexcept |
| Checks if the container has no elements, i.e. whether xtd::collections::generic::list::begin() == xtd::collections::generic::list::end().
|
|
const_iterator | end () const noexcept override |
| Returns an iterator to the element following the last element of the enumarable.
|
|
iterator | end () noexcept override |
| Returns an iterator to the element following the last element of the enumarable.
|
|
virtual reference | front () |
| Returns a reference to the first element in the container.
|
|
virtual const_reference | front () const |
| Returns a reference to the first element in the container.
|
|
bool | is_fixed_size () const noexcept override |
| Gets a value indicating whether the xtd::collections::generic::ilist <type_t> has a fixed size.
|
|
bool | is_read_only () const noexcept override |
| Gets a value indicating whether the xtd::collections::generic::icollection <type_t> is read-only.
|
|
bool | is_synchronized () const noexcept override |
| Gets a value indicating whether access to the xtd::collections::generic::icollection <type_t> is synchronized (thread safe).
|
|
virtual const base_type & | items () const noexcept |
| Returns the underlying base type items.
|
|
virtual base_type & | items () noexcept |
| Returns the underlying base type items.
|
|
virtual size_type | max_size () const noexcept |
| Returns the maximum number of elements the container is able to hold due to system or library implementation limitations, i.e. std::distance(xtd::collections::generic::list::begin(), xtd::collections::generic::list::end()) for the largest container.
|
|
virtual reverse_iterator | rbegin () noexcept |
| Returns a reverse iterator to the first element of the reversed vector. It corresponds to the last element of the non-reversed vector. If the vector is empty, the returned iterator is equal to xtd::collections::generic::list::rend().
|
|
virtual const_reverse_iterator | rbegin () const noexcept |
| Returns a reverse iterator to the first element of the reversed vector. It corresponds to the last element of the non-reversed vector. If the vector is empty, the returned iterator is equal to xtd::collections::generic::list::rend().
|
|
virtual reverse_iterator | rend () noexcept |
| Returns a reverse iterator to the element following the last element of the reversed vector. It corresponds to the element preceding the first element of the non-reversed vector. This element acts as a placeholder, attempting to access it results in undefined behavior.
|
|
virtual const_reverse_iterator | rend () const noexcept |
| Returns a reverse iterator to the element following the last element of the reversed vector. It corresponds to the element preceding the first element of the non-reversed vector. This element acts as a placeholder, attempting to access it results in undefined behavior.
|
|
virtual size_type | size () const noexcept |
| Returns the number of elements in the container, i.e. std::distance(xtd::collections::generic::list::begin(), xtd::collections::generic::list::end()).
|
|
const xtd::object & | sync_root () const noexcept override |
| Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollection <type_t>.
|
|
|
void | add (const type_t &item) override |
| Adds an object to the end of the xtd::collections::generic::list <type_t>.
|
|
void | add_range (const xtd::collections::generic::ienumerable< type_t > &enumerable) |
| Adds copy of elements from the specified collection to the end of the xtd::collections::generic::list <type_t>.
|
|
void | add_range (std::initializer_list< type_t > il) |
| Adds copy of elements from the specified collection to the end of the xtd::collections::generic::list <type_t>.
|
|
void | assign (size_type count, const type_t &value) |
| Replaces the contents with count copies of value value.
|
|
template<typename input_iterator_t > |
void | assign (input_iterator_t first, input_iterator_t last) |
| Replaces the contents with copies of those in the range [first, last).
|
|
read_only_collection | as_read_only () const noexcept |
| Returns a read-only xtd::collections::object_model::read_only_collection <type_t> wrapper for the current collection.
|
|
virtual void | assign (std::initializer_list< type_t > items) |
| Replaces the contents with the elements from the initializer list items.
|
|
virtual reference | at (size_type index) |
| Returns a reference to the element at specified location pos, with bounds checking.
|
|
virtual const_reference | at (size_type index) const |
| Returns a reference to the element at specified location pos, with bounds checking.
|
|
void | clear () override |
| Removes all items from the xtd::collections::generic::icollection <type_t>.
|
|
bool | contains (const type_t &value) const noexcept override |
| Determines whether the xtd::collections::generic::icollection <type_t> contains a specific value.
|
|
virtual void | copy_to (xtd::array< type_t > &array) const |
| Copies the entire xtd::collections::generic::list <type_t> to a compatible one-dimensional array.
|
|
void | copy_to (xtd::array< type_t > &array, size_type array_index) const override |
| Copies the elements of the xtd::collections::generic::icollection <type_t> to an xtd::array, starting at a particular xtd::array index.
|
|
virtual void | copy_to (size_type index, xtd::array< type_t > &array, size_type array_index, size_type count) const |
| Copies the entire xtd::collections::generic::list <type_t> to a compatible one-dimensional array, starting at the specified index of the target array.
|
|
template<typename... args_t> |
iterator | emplace (const_iterator pos, args_t &&... args) |
| Inserts a new element into the container directly before pos .
|
|
template<typename... args_t> |
reference | emplace_back (args_t &&... args) |
| Appends a new element to the end of the container. The element is constructed through std::allocator_traits::construct, which typically uses placement-new to construct the element in-place at the location provided by the container. The arguments args... are forwarded to the constructor as std::forward<Args>(args)... .
|
|
bool | equals (const object &obj) const noexcept override |
| Determines whether the specified object is equal to the current object.
|
|
bool | equals (const list &rhs) const noexcept override |
|
virtual iterator | erase (const_iterator pos) |
| Erases the specified elements from the container.
|
|
virtual iterator | erase (const_iterator first, const_iterator last) |
| Erases the specified elements from the container.
|
|
virtual allocator_type | get_allocator () const |
| Returns the allocator associated with the container.
|
|
virtual base_type & | get_base_type () noexcept |
| Returns the underlying base type.
|
|
virtual const base_type & | get_base_type () const noexcept |
| Returns the underlying base type.
|
|
enumerator< value_type > | get_enumerator () const noexcept override |
| Returns an enumerator that iterates through a collection.
|
|
list | get_range (size_type index, size_type count) |
| Creates a shallow copy of a range of elements in the source xtd::collections::generic::list <type_t>.
|
|
size_type | index_of (const type_t &value) const noexcept override |
| Determines the index of a specific item in the xtd::collections::generic::list <type_t>.
|
|
virtual size_type | index_of (const type_t &value, size_type index) const |
| Determines the index of a specific item in the xtd::collections::generic::list <type_t>.
|
|
virtual size_type | index_of (const type_t &value, size_type index, size_type count) const |
| Determines the index of a specific item in the xtd::collections::generic::list <type_t>.
|
|
virtual iterator | insert (const_iterator pos, const type_t &value) |
| Inserts elements at the specified location in the container.
|
|
virtual iterator | insert (const_iterator pos, const type_t &&value) |
| Inserts elements at the specified location in the container.
|
|
virtual iterator | insert (const_iterator pos, size_type count, const type_t &value) |
| Inserts elements at the specified location in the container.
|
|
template<typename input_iterator_t > |
iterator | insert (const_iterator pos, input_iterator_t first, input_iterator_t last) |
| Inserts elements at the specified location in the container.
|
|
virtual iterator | insert (const_iterator pos, const std::initializer_list< type_t > &items) |
| Inserts elements at the specified location in the container.
|
|
void | insert (size_type index, const type_t &value) override |
| Inserts an element into the xtd::collections::generic::list <type_t> at the specified index.
|
|
virtual void | insert_range (size_type index, const xtd::collections::generic::ienumerable< type_t > &enumerable) |
| Inserts copy of elements from a collection into the xtd::collections::generic::list <type_t> at the specified index.
|
|
virtual void | insert_range (size_type index, const std::initializer_list< type_t > &items) |
| Inserts copy of elements from a collection into the xtd::collections::generic::list <type_t> at the specified index.
|
|
virtual void | pop_back () |
| Removes the last element of the container.
|
|
virtual void | push_back (const type_t &value) |
| Appends the given element value to the end of the container.
|
|
virtual void | push_back (type_t &&value) |
| Appends the given element value to the end of the container.
|
|
bool | remove (const type_t &item) override |
| Removes the first occurrence of a specific object from the xtd::collections::generic::icollection <type_t>.
|
|
void | remove_at (size_type index) override |
| Removes the element at the specified index of the xtd::collections::generic::list <type_t>.
|
|
virtual void | remove_range (size_type index, size_type count) |
| Removes a range of elements from the xtd::collections::generic::list <type_t>.
|
|
virtual void | reserve (size_type new_cap) |
| Increase the capacity of the vector (the total number of elements that the vector can hold without requiring reallocation) to a value that's greater or equal to new_cap . If new_cap is greater than the current capacity(), new storage is allocated, otherwise the function does nothing.
|
|
virtual void | resize (size_type count) |
| Resizes the container to contain count elements, does nothing if count == size(). @param count The new size of the container. @remarks If the current size is greater than count, the container is reduced to its first countelements. @remarks If the current size is less than count`, additional default-inserted elements are appended.
|
|
virtual void | resize (size_type count, const value_type &value) |
| Resizes the container to contain count elements, does nothing if count == size(). @param count The new size of the container. @param value The value to initialize the new elements with. @remarks If the current size is greater than count, the container is reduced to its first countelements. @remarks If the current size is less than count, additional copies of value` are appended.
|
|
virtual void | shrink_to_fit () |
| Requests the removal of unused capacity.
|
|
virtual void | swap (list &other) noexcept |
| Exchanges the contents and capacity of the container with those of other. Does not invoke any move, copy, or swap operations on individual elements.
|
|
virtual xtd::array< value_type > | to_array () const noexcept |
| Copies the elements of the xtd::collections::generic::list <type_t> to a new array.
|
|
string | to_string () const noexcept override |
| Returns a xtd::string that represents the current object.
|
|
virtual void | trim_excess () |
| Sets the capacity to the actual number of elements in the xtd::collections::generic::list <type_t>, if that number is less than a threshold value.
|
|
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>>
|
inlineoverridevirtualnoexcept |
Gets the number of elements contained in the xtd::collections::generic::list <type_t>.
- Returns
- The number of elements contained in the xtd::collections::generic::list <type_t>.
- Examples
- The following example demonstrates how to add, remove, and insert a simple business object in a xtd::collections::generic::list <type_t>.
#include <xtd/xtd>
public:
string part_name;
int part_id = 0;
part() = default;
part(const string& name, int id) : part_name {name}, part_id {id} {}
string to_string() const noexcept
override {
return string::format(
"ID: {} Name: {}", part_id, part_name);}
bool equals(const object& obj) const noexcept override {return is<part>(obj) && equals(as<part>(obj));}
bool equals(
const part& other)
const noexcept override {
return part_id ==
other.part_id;}
size get_hash_code() const noexcept
override {
return object::get_hash_code();}
};
class example {
public:
static auto main() -> void {
console::write_line("\ncapacity: {0}", parts.capacity());
parts.add(part {"crank arm", 1234});
parts.add(part {"chain ring", 1334});
parts.add(part {"seat", 1434});
parts.add(part {"cassette", 1534});
parts.add(part {"shift lever", 1634});
console::write_line();
for (auto part : parts)
console::write_line("\ncapacity: {0}", parts.capacity());
console::write_line("count: {0}", parts.count());
parts.trim_excess();
console::write_line("\ntrim_excess()");
console::write_line("capacity: {0}", parts.capacity());
console::write_line("count: {0}", parts.count());
parts.clear();
console::write_line("\nclear()");
console::write_line("capacity: {0}", parts.capacity());
console::write_line("count: {0}", parts.count());
}
};
The following example demonstrates several properties and methods of the xtd::collections::generic::list <type_t> generic class of type string. (For an example of a xtd::collections::generic::list <type_t> of complex types, see the xtd::collections::generic::list::contains method.)
The parameterless constructor is used to create a list of strings with the default capacity. The xtd::collections::generic::list::capacity property is displayed and then the xtd::collections::generic::list::add method is used to add several items. The items are listed, and the xtd::collections::generic::list::capacity property is displayed again, along with the xtd::collections::generic::list::count property, to show that the capacity has been increased as needed.
The xtd::collections::generic::list::contains method is used to test for the presence of an item in the list, the Insert method is used to insert a new item in the middle of the list, and the contents of the list are displayed again.
The default xtd::collections::generic::list::operator [] is used to retrieve an item, the xtd::collections::generic::list::remove method is used to remove the first instance of the duplicate item added earlier, and the contents are displayed again. The xtd::collections::generic::list::remove method always removes the first instance it encounters.
The xtd::collections::generic::list::trim_excess method is used to reduce the capacity to match the count, and the xtd::collections::generic::list::capacity and xtd::collections::generic::list::count properties are displayed. If the unused capacity had been less than 10 percent of total capacity, the list would not have been resized.
Finally, the xtd::collections::generic::list::clear method is used to remove all items from the list, and the xtd::collections::generic::list::capacity and xtd::collections::generic::list::count properties are displayed.
#include <xtd/xtd>
class example {
public:
static auto main() -> void {
console::write_line("\ncapacity: {0}", dinosaurs.capacity());
dinosaurs.add("Tyrannosaurus");
dinosaurs.add("Amargasaurus");
dinosaurs.add("Mamenchisaurus");
dinosaurs.add("Deinonychus");
dinosaurs.add("Compsognathus");
console::write_line();
for (auto dinosaur : dinosaurs)
console::write_line("\ncapacity: {0}", dinosaurs.capacity());
console::write_line("count: {0}", dinosaurs.count());
console::write_line("\ncontains(\"Deinonychus\"): {0}", dinosaurs.contains("Deinonychus"));
console::write_line("\ninsert(2, \"Compsognathus\")");
dinosaurs.insert(2, "Compsognathus");
console::write_line();
for (auto dinosaur : dinosaurs)
console::write_line("\ndinosaurs[3]: {0}", dinosaurs[3]);
console::write_line("\nremove(\"Compsognathus\")");
dinosaurs.remove("Compsognathus");
console::write_line();
for (auto dinosaur : dinosaurs)
dinosaurs.trim_excess();
console::write_line("\ntrim_excess()");
console::write_line("capacity: {0}", dinosaurs.capacity());
console::write_line("count: {0}", dinosaurs.count());
dinosaurs.clear();
console::write_line("\nclear()");
console::write_line("capacity: {0}", dinosaurs.capacity());
console::write_line("count: {0}", dinosaurs.count());
}
};
Implements xtd::collections::generic::icollection< type_t >.
- Examples
- ienumerable.cpp, and ienumerator.cpp.
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>>
Sets the capacity to the actual number of elements in the xtd::collections::generic::list <type_t>, if that number is less than a threshold value.
- Examples
- The following example demonstrates how to add, remove, and insert a simple business object in a xtd::collections::generic::list <type_t>.
#include <xtd/xtd>
public:
string part_name;
int part_id = 0;
part() = default;
part(const string& name, int id) : part_name {name}, part_id {id} {}
string to_string() const noexcept
override {
return string::format(
"ID: {} Name: {}", part_id, part_name);}
bool equals(const object& obj) const noexcept override {return is<part>(obj) && equals(as<part>(obj));}
bool equals(
const part& other)
const noexcept override {
return part_id ==
other.part_id;}
size get_hash_code() const noexcept
override {
return object::get_hash_code();}
};
class example {
public:
static auto main() -> void {
console::write_line("\ncapacity: {0}", parts.capacity());
parts.add(part {"crank arm", 1234});
parts.add(part {"chain ring", 1334});
parts.add(part {"seat", 1434});
parts.add(part {"cassette", 1534});
parts.add(part {"shift lever", 1634});
console::write_line();
for (auto part : parts)
console::write_line("\ncapacity: {0}", parts.capacity());
console::write_line("count: {0}", parts.count());
parts.trim_excess();
console::write_line("\ntrim_excess()");
console::write_line("capacity: {0}", parts.capacity());
console::write_line("count: {0}", parts.count());
parts.clear();
console::write_line("\nclear()");
console::write_line("capacity: {0}", parts.capacity());
console::write_line("count: {0}", parts.count());
}
};
The following example demonstrates several properties and methods of the xtd::collections::generic::list <type_t> generic class of type string. (For an example of a xtd::collections::generic::list <type_t> of complex types, see the xtd::collections::generic::list::contains method.)
The parameterless constructor is used to create a list of strings with the default capacity. The xtd::collections::generic::list::capacity property is displayed and then the xtd::collections::generic::list::add method is used to add several items. The items are listed, and the xtd::collections::generic::list::capacity property is displayed again, along with the xtd::collections::generic::list::count property, to show that the capacity has been increased as needed.
The xtd::collections::generic::list::contains method is used to test for the presence of an item in the list, the Insert method is used to insert a new item in the middle of the list, and the contents of the list are displayed again.
The default xtd::collections::generic::list::operator [] is used to retrieve an item, the xtd::collections::generic::list::remove method is used to remove the first instance of the duplicate item added earlier, and the contents are displayed again. The xtd::collections::generic::list::remove method always removes the first instance it encounters.
The xtd::collections::generic::list::trim_excess method is used to reduce the capacity to match the count, and the xtd::collections::generic::list::capacity and xtd::collections::generic::list::count properties are displayed. If the unused capacity had been less than 10 percent of total capacity, the list would not have been resized.
Finally, the xtd::collections::generic::list::clear method is used to remove all items from the list, and the xtd::collections::generic::list::capacity and xtd::collections::generic::list::count properties are displayed.
#include <xtd/xtd>
class example {
public:
static auto main() -> void {
console::write_line("\ncapacity: {0}", dinosaurs.capacity());
dinosaurs.add("Tyrannosaurus");
dinosaurs.add("Amargasaurus");
dinosaurs.add("Mamenchisaurus");
dinosaurs.add("Deinonychus");
dinosaurs.add("Compsognathus");
console::write_line();
for (auto dinosaur : dinosaurs)
console::write_line("\ncapacity: {0}", dinosaurs.capacity());
console::write_line("count: {0}", dinosaurs.count());
console::write_line("\ncontains(\"Deinonychus\"): {0}", dinosaurs.contains("Deinonychus"));
console::write_line("\ninsert(2, \"Compsognathus\")");
dinosaurs.insert(2, "Compsognathus");
console::write_line();
for (auto dinosaur : dinosaurs)
console::write_line("\ndinosaurs[3]: {0}", dinosaurs[3]);
console::write_line("\nremove(\"Compsognathus\")");
dinosaurs.remove("Compsognathus");
console::write_line();
for (auto dinosaur : dinosaurs)
dinosaurs.trim_excess();
console::write_line("\ntrim_excess()");
console::write_line("capacity: {0}", dinosaurs.capacity());
console::write_line("count: {0}", dinosaurs.count());
dinosaurs.clear();
console::write_line("\nclear()");
console::write_line("capacity: {0}", dinosaurs.capacity());
console::write_line("count: {0}", dinosaurs.count());
}
};