xtd 0.2.0
Loading...
Searching...
No Matches
xtd::array< type_t, rank_, allocator_t > Class Template Reference
Inheritance diagram for xtd::array< type_t, rank_, allocator_t >:
xtd::basic_array< type_t, allocator_t > xtd::array_abstract_object xtd::iequatable< basic_array< type_t, allocator_t > > xtd::abstract_object xtd::interface xtd::extensions::equality_operators< type_t, iequatable< type_t > > xtd::object

Definition

template<class type_t, xtd::size rank_, class allocator_t>
class xtd::array< type_t, rank_, allocator_t >

Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the base class for all arrays.

Definition
template<class type_t, xtd::size rank_, class allocator_t>
class array : public xtd::basic_array<type_t, allocator_t>;
Base object that represent array.
Definition basic_array.hpp:27
Header
#include <xtd/array>
Namespace
xtd
Library
xtd.core
Remarks
The xtd::array class is not part of the xtd::collections namespaces. However, it is still considered a collection because it is based on the xtd::collections::generic::ilist interface.
An element is a value in an xtd::array. The length of an xtd::array is the total number of elements it can contain. The lower bound of an v is the index of its first element. An xtd::array can have any lower bound, but it has a lower bound of zero by default. A different lower bound can be defined when creating an instance of the xtd::array class using xtd::array::create_instance. A multidimensional xtd::array can have different bounds for each dimension. An array can have a maximum of 32 dimensions.
Unlike the classes in the xtd::collections namespaces, xtd::array has a fixed capacity. To increase the capacity, you must create a new xtd::array object with the required capacity, copy the elements from the old xtd::array object to the new one, and delete the old xtd::array.
The xtd::array class implements the xtd::collections::generic::ilist, xtd::collections::generic::icollection, and xtd::collections::generic::ienumerable generic interfaces. The implementations are provided to arrays at run time, and as a result, the generic interfaces do not appear in the declaration syntax for the xtd::array class. In addition, there are no reference topics for interface members that are accessible only by casting an array to the generic interface type (explicit interface implementations). The key thing to be aware of when you cast an array to one of these interfaces is that members which add, insert, or remove elements throw xtd::not_supported_exception.
The xtd::array::copy method copies elements not only between arrays of the same type but also between standard arrays of different types; it handles type casting automatically.
Some methods, such as xtd::array::create_instance, xtd::array::copy, xtd::array::copy_to, xtd::array::get_value, and xtd::array::set_value, provide overloads that accept 64-bit integers as parameters to accommodate large capacity arrays. xtd::array::long_length and xtd::array::get_long_length return 64-bit integers indicating the length of the array.
The xtd::array is not guaranteed to be sorted. You must sort the xtd::array prior to performing operations (such as xtd::array::binary_search) that require the xtd::array to be sorted.
Examples
The following code example demonstrates different methods to create an array.
Examples
The following code example creates and initializes an Array and displays its properties and its elements.
#include <xtd/xtd>
auto print_values(const array<int>& my_arr) {
for (auto i : my_arr)
console::write("\t{}", i);
}
auto print_values(const array<any_object>& my_arr) {
for (auto o : my_arr)
console::write("\t{}", o);
}
auto main() -> int {
// Creates and initializes a new integer array and a new Object array.
auto my_int_array = array<int> {1, 2, 3, 4, 5};
auto my_obj_array = array<any_object> {26, 27, 28, 29, 30};
// Prints the initial values of both arrays.
console::write_line("Initially,");
console::write("integer array:");
print_values(my_int_array);
console::write("Object array: ");
print_values(my_obj_array);
// Copies the first two elements from the integer array to the Object array.
array<>::copy(my_int_array, my_obj_array, 2);
// Prints the values of the modified arrays.
console::write_line("\nAfter copying the first two elements of the integer array to the Object array,");
console::write("integer array:");
print_values(my_int_array);
console::write("Object array: ");
print_values(my_obj_array);
// Copies the last two elements from the object array to the integer array.
xtd::array<>::copy(my_obj_array, my_obj_array.get_upper_bound(0) - 1, my_int_array, my_int_array.get_upper_bound(0) - 1, 2);
// Prints the values of the modified arrays.
console::write_line("\nAfter copying the last two elements of the Object array to the integer array,");
console::write("integer array:");
print_values(my_int_array);
console::write("Object array: ");
print_values(my_obj_array);
}
// This code produces the following output :
//
// Initially,
// integer array: 1 2 3 4 5
// Object array: 26 27 28 29 30
//
// After copying the first two elements of the integer array to the Object array,
// integer array: 1 2 3 4 5
// Object array: 1 2 28 29 30
//
// After copying the last two elements of the Object array to the integer array,
// integer array: 1 2 3 29 30
// Object array: 1 2 28 29 30
static void copy(const array< source_type_t, source_rank, source_allocator_t > &source_array, const array< destination_type_t, destination_rank, destination_allocator_t > &destination_array)
Copies a range of elements from an xtd::array starting at the first element and pastes them into anot...
Definition array_static.hpp:239
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
static void write(arg_t &&value)
Writes the text representation of the specified value to the standard output stream.
Definition console.hpp:462
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
@ i
The I key.
Definition console_key.hpp:104
@ o
The O key.
Definition console_key.hpp:116
Examples
date_time_now.cpp, date_time_now2.cpp, ienumerable.cpp, and ilist.cpp.

Public Aliases

using value_type
 Represents the array value type.
 
using allocator_type
 Represents the array allocator type.
 
using base_type
 Represents the array base type.
 
using size_type
 Represents the array size type (usually xtd::size).
 
using difference_type
 Represents the array difference type (usually xtd::ptrdiff).
 
using reference
 Represents the reference of array value type.
 
using const_reference
 Represents the const reference of array value type.
 
using pointer
 Represents the pointer of array value type.
 
using const_pointer
 Represents the const pointer of array value type.
 
using iterator
 Represents the iterator of array value type.
 
using const_iterator
 Represents the const iterator of array value type.
 
using reverse_iterator
 Represents the reverse iterator of array value type.
 
using const_reverse_iterator
 Represents the const reverse iterator of array value type.
 

Public Constructors

 array ()=default
 Initializes a new instance of the array class that is empty.
 
 array (const array &array)
 Copy constructor with specified array.
 
 array (array &&array)=default
 Move constructor with specified array.
 
 array (const array< xtd::size, 1 > &lengths)
 Initializes a new instance of the array class with lengths for each rank specified.
 
 array (const array< xtd::size, 1 > &lengths, const type_t &value)
 Initializes a new instance of the array class with lengths for each rank specified.
 

Public Properties

xtd::size rank () const noexcept override
 Gets the rank (number of dimensions) of the array.
 

Public Methods

xtd::string to_string () const noexcept override
 Returns a xtd::string that represents the current object.
 

Public Operators

arrayoperator= (const array &)=default
 Copy assignment operator. Replaces the contents with a copy of the contents of other.
 
arrayoperator= (array &&)=default
 Move assignment operator. Replaces the contents with those of other using move semantics (i.e. the data in other is moved from other into this container). other is in a valid but unspecified state afterwards.
 

Additional Inherited Members

using value_type
 Represents the array value type.
 
using allocator_type
 Represents the array allocator type.
 
using base_type
 Represents the array base type.
 
using size_type
 Represents the array size type (usually xtd::size).
 
using difference_type
 Represents the array difference type (usually xtd::ptrdiff).
 
using reference
 Represents the reference of array value type.
 
using const_reference
 Represents the const reference of array value type.
 
using pointer
 Represents the pointer of array value type.
 
using const_pointer
 Represents the const pointer of array value type.
 
using iterator
 Represents the iterator of array value type.
 
using const_iterator
 Represents the const iterator of array value type.
 
using reverse_iterator
 Represents the reverse iterator of array value type.
 
using const_reverse_iterator
 Represents the const reverse iterator of array value type.
 
size_type count () const noexcept override
 Gets the number of elements contained in the xtd::array <type_t>.
 
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.
 
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_typeitems () const noexcept
 Returns the underlying base type items.
 
virtual base_typeitems () noexcept
 Returns the underlying base type items.
 
virtual size_type length () const noexcept
 Gets a size that represents the total number of elements in all the dimensions of the array.
 
virtual xtd::int64 long_length ()
 Gets a 64-bit integer that represents the total number of elements in all the dimensions of the array.
 
virtual size_type max_length () 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::array::begin(), xtd::array::end()) for the largest container.
 
const xtd::objectsync_root () const noexcept override
 Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollection <type_t>.
 
bool contains (const type_t &value) const noexcept override
 Determines whether an element is in the array.
 
void copy_to (xtd::array< type_t > &array) const
 
void copy_to (xtd::array< type_t > &array, size_type array_index) const override
 Copies the elements of the xtd::array <type_t> to an xtd::array, starting at a particular xtd::array index.
 
void copy_to (const xtd::array< size_type > &indexes, xtd::array< type_t > &array, size_type array_index) const
 
void copy_to (const xtd::array< size_type > &indexes, xtd::array< type_t > &array, size_type array_index, size_type count) const
 
void copy_to (const size_type index, xtd::array< type_t > &array, size_type array_index) const
 
void copy_to (const size_type index, xtd::array< type_t > &array, size_type array_index, size_type count) const
 
bool equals (const object &obj) const noexcept override
 Determines whether the specified object is equal to the current object.
 
bool equals (const basic_array &rhs) const noexcept override
 
virtual void fill (const value_type &value) noexcept
 Assigns the value to all elements in the container.
 
xtd::collections::generic::enumerator< value_typeget_enumerator () const noexcept override
 
constexpr size_type get_length (size_type dimension) const
 Gets the total number of elements in all the dimensions of the array.
 
xtd::array< size_type, 1 > get_lengths () const
 Gets an array of the number of elements of all the dimensions of the array.
 
constexpr xtd::int64 get_long_length (size_type dimension) const
 Gets a 64-bit integer that represents the total number of elements in all the dimensions of the array.
 
constexpr size_type get_lower_bound (size_type dimension) const
 Gets the lower bound of the specified dimension in the array.
 
constexpr size_type get_upper_bound (size_type dimension) const
 Gets the upper bound of the specified dimension in the array.
 
const value_typeget_value (const xtd::array< size_type > &indexes) const
 Gets the value at the specified position in the multidimensional array. The indexes are specified as 32-bit integers array.
 
size_type index_of (const type_t &value) const noexcept override
 Determines the index of a specific item in the xtd::array <type_t>.
 
size_type index_of (const type_t &value, size_type index) const
 
size_type index_of (const type_t &value, size_type index, size_type count) const
 
void resize (size_type new_size, value_type value)
 Resizes the container to contain count elements, does nothing if count == length(). / @param new_size The new size of the container. / @exception xtd::argument_out_of_range_exception xtd::collections::generic::list::capacity is set to a value that is less than xtd::collections::generic::list::count. / @remarks If the current size is greater thancount, the container is reduced to its firstcountelements. / @remarks If the current size is less thancount`, additional default-inserted elements are appended. void resize(size_type new_size) {resize(new_size, value_type {});}.
 
void set_value (const type_t &value, const xtd::array< size_type > &indexes)
 Sets a value to the element at the specified position in the multidimensional array.
 
basic_array< type_t > & sort ()
 Sorts the elements in the entire xtd::collections::generic::list <type_t> using the default comparer.
 
basic_array< type_t > & sort (comparison_t &&comparison)
 Sorts the elements in the entire xtd::collections::generic::list <type_t> using the specified xtd::comparison <type_t>.
 
basic_array< type_t > & sort (const xtd::collections::generic::icomparer< type_t > &comparer)
 Sorts the elements in the entire xtd::collections::generic::list <type_t> using the specified comparer.
 
basic_array< type_t > & sort (xtd::size index, xtd::size count, const xtd::collections::generic::icomparer< type_t > &comparer)
 Sorts the elements in a range of elements in xtd::collections::generic::list <type_t> using the specified comparer.
 
xtd::string to_string () const noexcept override
 Returns a xtd::string that represents the current object.
 
basic_arrayoperator= (const basic_array &other)
 Copy assignment operator. Replaces the contents with a copy of the contents of other.
 
basic_arrayoperator= (basic_array &&other) noexcept=default
 Move assignment operator. Replaces the contents with those of other using move semantics (i.e. the data in other is moved from other into this container). other is in a valid but unspecified state afterwards.
 
basic_arrayoperator= (std::initializer_list< type_t > &items)
 Replaces the contents with those identified by initializer list ilist.
 
const_reference operator[] (size_type index) const override
 Returns a reference to the element at specified location index.
 
reference operator[] (size_type index) override
 Returns a reference to the element at specified location index.
 
 operator const base_type & () const noexcept
 Returns a reference to the underlying base type.
 
 operator base_type & () noexcept
 Returns a reference to the underlying base type.
 
type_t & operator() (const xtd::array< size_type > &indexes)
 Gets the value at the specified position in the multidimensional array. The indexes are specified as a 32-bit integer array.
 
const type_t & operator() (const xtd::array< size_type > &indexes) const
 Gets the value at the specified position in the multidimensional array. The indexes are specified as a 32-bit integer array.
 
 object ()=default
 Create a new instance of the ultimate base class object.
 
virtual xtd::size get_hash_code () const noexcept
 Serves as a hash function for a particular type.
 
virtual type_object get_type () const noexcept
 Gets the type of the current instance.
 
template<class object_t>
xtd::unique_ptr_object< object_t > memberwise_clone () const
 Creates a shallow copy of the current object.
 
virtual bool equals (const type_t &) const noexcept=0
 Indicates whether the current object is equal to another object of the same type.
 
template<class object_a_t, class object_b_t>
static bool equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are considered equal.
 
template<class object_a_t, class object_b_t>
static bool reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are the same instance.
 
 abstract_object ()=default
 Initializes a new instance of the xtd::abstract_object class.
 

Member Typedef Documentation

◆ value_type

template<class type_t, xtd::size rank_, class allocator_t>
using xtd::array< type_t, rank_, allocator_t >::value_type

Represents the array value type.

◆ allocator_type

template<class type_t, xtd::size rank_, class allocator_t>
using xtd::array< type_t, rank_, allocator_t >::allocator_type

Represents the array allocator type.

◆ base_type

template<class type_t, xtd::size rank_, class allocator_t>
using xtd::array< type_t, rank_, allocator_t >::base_type

Represents the array base type.

◆ size_type

template<class type_t, xtd::size rank_, class allocator_t>
using xtd::array< type_t, rank_, allocator_t >::size_type

Represents the array size type (usually xtd::size).

◆ difference_type

template<class type_t, xtd::size rank_, class allocator_t>
using xtd::array< type_t, rank_, allocator_t >::difference_type

Represents the array difference type (usually xtd::ptrdiff).

◆ reference

template<class type_t, xtd::size rank_, class allocator_t>
using xtd::array< type_t, rank_, allocator_t >::reference

Represents the reference of array value type.

◆ const_reference

template<class type_t, xtd::size rank_, class allocator_t>
using xtd::array< type_t, rank_, allocator_t >::const_reference

Represents the const reference of array value type.

◆ pointer

template<class type_t, xtd::size rank_, class allocator_t>
using xtd::array< type_t, rank_, allocator_t >::pointer

Represents the pointer of array value type.

◆ const_pointer

template<class type_t, xtd::size rank_, class allocator_t>
using xtd::array< type_t, rank_, allocator_t >::const_pointer

Represents the const pointer of array value type.

◆ iterator

template<class type_t, xtd::size rank_, class allocator_t>
using xtd::array< type_t, rank_, allocator_t >::iterator

Represents the iterator of array value type.

◆ const_iterator

template<class type_t, xtd::size rank_, class allocator_t>
using xtd::array< type_t, rank_, allocator_t >::const_iterator

Represents the const iterator of array value type.

◆ reverse_iterator

template<class type_t, xtd::size rank_, class allocator_t>
using xtd::array< type_t, rank_, allocator_t >::reverse_iterator

Represents the reverse iterator of array value type.

◆ const_reverse_iterator

template<class type_t, xtd::size rank_, class allocator_t>
using xtd::array< type_t, rank_, allocator_t >::const_reverse_iterator

Represents the const reverse iterator of array value type.

Constructor & Destructor Documentation

◆ array() [1/5]

template<class type_t, xtd::size rank_, class allocator_t>
xtd::array< type_t, rank_, allocator_t >::array ( )
default

Initializes a new instance of the array class that is empty.

Remarks
The array class is not thread safe.
Examples
The following code example demonstrates different methods to create an array.

◆ array() [2/5]

template<class type_t, xtd::size rank_, class allocator_t>
xtd::array< type_t, rank_, allocator_t >::array ( const array< type_t, rank_, allocator_t > & array)
inline

Copy constructor with specified array.

Parameters
arrayThe xtd::array which elements will be inserted from.

◆ array() [3/5]

template<class type_t, xtd::size rank_, class allocator_t>
xtd::array< type_t, rank_, allocator_t >::array ( array< type_t, rank_, allocator_t > && array)
default

Move constructor with specified array.

Parameters
arrayThe xtd::array which elements will be inserted from.

◆ array() [4/5]

template<class type_t, xtd::size rank_, class allocator_t>
xtd::array< type_t, rank_, allocator_t >::array ( const array< xtd::size, 1 > & lengths)
inline

Initializes a new instance of the array class with lengths for each rank specified.

Parameters
lengthsthe lengths for each rank.
Remarks
The array class is not thread safe.
Examples
The following code example demonstrates different methods to create an array.

◆ array() [5/5]

template<class type_t, xtd::size rank_, class allocator_t>
xtd::array< type_t, rank_, allocator_t >::array ( const array< xtd::size, 1 > & lengths,
const type_t & value )
inline

Initializes a new instance of the array class with lengths for each rank specified.

Parameters
lengthsthe lengths for each rank.
Remarks
The array class is not thread safe.
Examples
The following code example demonstrates different methods to create an array.

Member Function Documentation

◆ rank()

template<class type_t, xtd::size rank_, class allocator_t>
xtd::size xtd::array< type_t, rank_, allocator_t >::rank ( ) const
inlineoverridevirtualnoexcept

Gets the rank (number of dimensions) of the array.

Returns
The rank (number of dimensions) of the array.
Examples
The following code example demonstrates methods to get the rank of an array.

Reimplemented from xtd::basic_array< type_t, allocator_t >.

◆ to_string()

template<class type_t, xtd::size rank_, class allocator_t>
xtd::string xtd::array< type_t, rank_, allocator_t >::to_string ( ) const
overridevirtualnoexcept

Returns a xtd::string that represents the current object.

Returns
A string that represents the current object.

Reimplemented from xtd::object.

◆ operator=() [1/2]

template<class type_t, xtd::size rank_, class allocator_t>
array & xtd::array< type_t, rank_, allocator_t >::operator= ( const array< type_t, rank_, allocator_t > & )
default

Copy assignment operator. Replaces the contents with a copy of the contents of other.

Parameters
otherAnother container to use as data source.
Returns
This current instance.

◆ operator=() [2/2]

template<class type_t, xtd::size rank_, class allocator_t>
array & xtd::array< type_t, rank_, allocator_t >::operator= ( array< type_t, rank_, allocator_t > && )
default

Move assignment operator. Replaces the contents with those of other using move semantics (i.e. the data in other is moved from other into this container). other is in a valid but unspecified state afterwards.

Parameters
otherAnother base type container to use as data source.
Returns
This current instance.

The documentation for this class was generated from the following file: