覆盖连续数据的非归属容器。 更多...
| 头: |
#include <QSpan>
|
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
|
| qmake: |
QT += core
|
| Since: | Qt 6.7 |
注意: 此类的所有函数 可重入 .
| const_iterator | |
| const_pointer | |
| const_reference | |
| const_reverse_iterator | |
| difference_type | |
| element_type | |
| iterator | |
| pointer | |
| reference | |
| reverse_iterator | |
| size_type | |
| value_type |
| QSpan () | |
| QSpan (QSpan<S, N> other ) | |
| QSpan (Range && r ) | |
| QSpan (const std::array<S, N> & arr ) | |
| QSpan (q20::type_identity_t<T> (&)[N] arr ) | |
| QSpan (std::array<S, N> & arr ) | |
| QSpan (std::initializer_list<QSpan<T, E>::value_type> il ) | |
| QSpan (std::span<S, N> other ) | |
| QSpan (It first , It last ) | |
| QSpan (It first , qsizetype count ) | |
| QSpan (const QSpan<T, E> & other ) | |
| QSpan (QSpan<T, E> && other ) | |
| ~QSpan () | |
| auto | back () const |
| auto | begin () const |
| auto | cbegin () const |
| auto | cend () const |
| auto | crbegin () const |
| auto | crend () const |
| auto | data () const |
| auto | empty () const |
| auto | end () const |
| auto | first () const |
| auto | first (QSpan<T, E>::size_type n ) const |
| auto | front () const |
| auto | isEmpty () const |
| auto | last () const |
| auto | last (QSpan<T, E>::size_type n ) const |
| auto | rbegin () const |
| auto | rend () const |
| auto | size () const |
| auto | size_bytes () const |
| auto | sliced (QSpan<T, E>::size_type pos ) const |
| auto | sliced (QSpan<T, E>::size_type pos , QSpan<T, E>::size_type n ) const |
| auto | subspan () const |
| auto | subspan () const |
| auto | subspan (QSpan<T, E>::size_type pos ) const |
| auto | subspan (QSpan<T, E>::size_type pos , QSpan<T, E>::size_type n ) const |
| QSpan<T, E> & | operator= (QSpan<T, E> && other ) |
| QSpan<T, E> & | operator= (const QSpan<T, E> & other ) |
| QSpan<T, E>::reference | operator[] (QSpan<T, E>::size_type idx ) const |
| const std::size_t | extent |
(从 6.8 起)
auto
|
as_bytes (QSpan<T, E> s ) |
(从 6.8 起)
auto
|
as_writable_bytes (QSpan<T, E> s ) |
QSpan 引用另一连续容器的连续部分。它充当所有种类连续容器的接口类型,不需要构造拥有容器,譬如 QList 或 std::vector 优先。
由 QSpan 引用的数据可以表示成数组 (或兼容数组的数据结构,譬如 QList , std::vector, QVarLengthArray ,等)。QSpan 本身仅仅存储数据指针,因此用户必须确保 QSpan 对象不会比它们引用的数据更长寿。
不像视图,譬如
QStringView
,
QLatin1StringView
and
QUtf8StringView
, referenced data can be modified through a QSpan object. To prevent this, construct a QSpan over a
const T
(见
Const and Mutable Spans
):
int numbers[] = {0, 1, 2}; QSpan<int> span = numbers; span[0] = 42; // numbers == {42, 1, 2}; QSpan<const int> cspan = numbers; cspan[0] = 0; // ERROR: cspan[0] is read-only
QSpan 可以是 fixed-size or variable-sized .
A variable-sized span is formed by omitting the second template argument (or setting it to
std::dynamic_extent
, which is, however, only available in C++20 builds), as seen in the example above.
A fixed-size span is formed by passing a number as the second template argument:
int numbers[] = {0, 1, 2}; QSpan<int, 3> span = numbers; QSpan<const int, 3> = numbers; // also OK
As the name suggests, a fixed-size span's size () is fixed at compile-time whereas the size () of a variable-sized span is determined only at run-time.
A fixed-size span is not default-constructible (unless its
extent
is zero (0)). A variable-sized span
is
default-constructible and will have
data() == nullptr
and
size() == 0
.
A fixed-size span can be implicitly converted into a variable-sized one. The opposite direction (variable-length into fixed-length) has the precondition that both span's sizes must match.
不像采用拥有容器,
const
is
shallow
in QSpan: you can still modify the data through a const QSpan (but not through a
QSpan<const T>
),和
begin
() 和
end
() are not overloaded on
const
/non-
const
. There are
cbegin
() 和
cend
(), though, that return const_iterators which prevent modification of the data even though
T
is not const:
int numbers[] = {0, 1, 2}; const QSpan<int> span = numbers; span.front() = 42; // OK, numbers[0] == 42 now *span.begin() = 31; // OK, numbers[0] == 31 now *span.cbegin() = -1; // ERROR: cannot assign through a const_iterator
QSpan should be passed by value, not by reference-to-const:
void consume(QSpan<const int> data); // OK void consume(const QSpan<const int> &data); // works, but is non-idiomatic and less efficient
QSpan<T,N>
是
文字类型
, regardless of whether
T
is a Literal Type or not.
QSpan is closely modelled after std::span , but has a few differences which we'll discuss here. Since they both implicitly convert into each other, you're free to choose whichever one you like best in your own code.
size_type
whereas
std::span
使用
size_t
.
std::span
ones are
explicit
.
std::span
can not.
The last two are required for source-compatibility when functions that took owning containers are converted to take QSpan instead, which is a vitally-important use-case in Qt. The use of qsizetype is for consistency with the rest of Qt containers. QSpan template arguments still use size_t to avoid introducing unnecessary error conditions (negative sizes).
QSpan can be constructed from an iterator and size or from an iterator pair, provided the iterators are
compatible
ones. Eventually, this should mean C++20
std::contiguous_iterator
and
std::sentinel_for
, but while Qt still supports C++17, only raw pointers are considered contiguous iterators.
QSpan can also be constructed from a compatible range. A range is compatible if it has compatible iterators .
另请参阅 QList , QStringView , QLatin1StringView ,和 QUtf8StringView .
[alias]
QSpan::
const_iterator
别名化的
const T*
and
const_pointer
,分别。
另请参阅 const_pointer , iterator , const_reverse_iterator ,和 Const and Mutable Spans .
[alias]
QSpan::
const_pointer
别名化的
const T*
and
const element_type*
,分别。
This alias is provided for compatbility with the STL.
另请参阅 element_type , pointer , const_reference , const_iterator ,和 Const and Mutable Spans .
[alias]
QSpan::
const_reference
别名化的
const T&
and
const element_type&
,分别。
This alias is provided for compatbility with the STL.
另请参阅 element_type , reference , const_pointer ,和 Const and Mutable Spans .
[alias]
QSpan::
const_reverse_iterator
别名化的
std::reverse_iterator<const_iterator>
.
另请参阅 const_iterator , reverse_iterator ,和 Const and Mutable Spans .
[alias]
QSpan::
difference_type
An alias for qptrdiff. This
不同于
std::span
.
This alias is provided for compatbility with the STL.
[alias]
QSpan::
element_type
别名化的
T
. Includes the
const
,若有的话。
This alias is provided for compatbility with the STL.
另请参阅 value_type , pointer ,和 Const and Mutable Spans .
[alias]
QSpan::
iterator
别名化的
T*
and
pointer
, respectively. Includes the
const
,若有的话。
另请参阅 pointer , const_iterator , reverse_iterator ,和 Const and Mutable Spans .
[alias]
QSpan::
pointer
别名化的
T*
and
element_type*
, respectively. Includes the
const
,若有的话。
This alias is provided for compatbility with the STL.
另请参阅 element_type , const_pointer , reference , iterator ,和 Const and Mutable Spans .
[alias]
QSpan::
reference
别名化的
T&
and
element_type&
, respectively. Includes the
const
,若有的话。
This alias is provided for compatbility with the STL.
另请参阅 element_type , const_reference , pointer ,和 Const and Mutable Spans .
[alias]
QSpan::
reverse_iterator
别名化的
std::reverse_iterator<iterator>
. Includes the
const
,若有的话。
另请参阅 iterator , const_reverse_iterator ,和 Const and Mutable Spans .
[alias]
QSpan::
size_type
An alias for qsizetype. This
不同于
std::span
.
This alias is provided for compatbility with the STL.
[alias]
QSpan::
value_type
别名化的
T
。排除
const
,若有的话。
This alias is provided for compatbility with the STL.
另请参阅 element_type and Const and Mutable Spans .
[default]
QSpan::
QSpan
(
QSpan
<
T
,
E
> &&
other
)
[default]
QSpan::
QSpan
(const
QSpan
<
T
,
E
> &
other
)
[default]
QSpan
<
T
,
E
> &QSpan::
operator=
(
QSpan
<
T
,
E
> &&
other
)
[default]
QSpan
<
T
,
E
> &QSpan::
operator=
(const
QSpan
<
T
,
E
> &
other
)
[default]
QSpan::
~QSpan
()
These Special Member Functions are implicitly-defined.
注意: Moves are equivalent to copies. Only data () 和 size () are copied from span to span, not the referenced data.
[constexpr noexcept]
template <typename S, size_t N, QSpan<T, E>::if_qualification_conversion<S> = true> QSpan::
QSpan
(const
std::array
<
S
,
N
> &
arr
)
[constexpr noexcept]
template <size_t N> QSpan::
QSpan
(
q20::type_identity_t
<
T
> (&)[
N
]
arr
)
[constexpr noexcept]
template <typename S, size_t N, QSpan<T, E>::if_qualification_conversion<S> = true> QSpan::
QSpan
(
std::array
<
S
,
N
> &
arr
)
构造 QSpan referencing the data in the supplied array arr .
注意: This constructor participates in overload resolution only if
N
or
extent
are
std::dynamic_extent
or otherwise
extent
==
N
S
or
const S
are the same as
T
.
注意:
q20::type_identity_t
is a C++17 backport of C++20's
std::type_identity_t
.
[constexpr noexcept]
template <typename S, size_t N, QSpan<T, E>::if_qualification_conversion<S> = true> QSpan::
QSpan
(
QSpan
<
S
,
N
>
other
)
[constexpr noexcept]
template <typename S, size_t N, QSpan<T, E>::if_qualification_conversion<S> = true> QSpan::
QSpan
(
std::span
<
S
,
N
>
other
)
构造 QSpan referencing the data in the supplied span other .
注意: This constructor participates in overload resolution only if
N
or
extent
are
std::dynamic_extent
or
extent
==
N
S
or
const S
are the same as
T
.
[constexpr noexcept]
auto
QSpan::
empty
() const
[constexpr noexcept]
auto
QSpan::
isEmpty
() const
Returns whether the span is empty, that is, whether
size() == 0
.
These functions do the same thing: empty () is provided for STL compatibility and isEmpty () is provided for Qt compatibility.
另请参阅 size () 和 size_bytes ().
[constexpr]
auto
QSpan::
sliced
(
QSpan
<
T
,
E
>
::size_type
pos
) const
[constexpr]
auto
QSpan::
subspan
(
QSpan
<
T
,
E
>
::size_type
pos
) const
返回
variable-sized
span of size
size() - pos
referencing the remainder of this span after dropping the first
pos
元素。
pos must be non-negative.
This span must hold at least
pos
elements (
E
>=
pos
and
size
() >=
pos
), otherwise the behavior is undefined.
These functions do the same thing: subspan () is provided for STL compatibility and sliced () is provided for Qt compatibility.
另请参阅 subspan (), first (QSpan<T,E>::size_type), and last (QSpan<T,E>::size_type).
[constexpr]
auto
QSpan::
sliced
(
QSpan
<
T
,
E
>
::size_type
pos
,
QSpan
<
T
,
E
>
::size_type
n
) const
[constexpr]
auto
QSpan::
subspan
(
QSpan
<
T
,
E
>
::size_type
pos
,
QSpan
<
T
,
E
>
::size_type
n
) const
返回 variable-sized span of size n referencing the n elements of this span starting at pos .
Both pos and n must be non-negative.
This span must hold at least
pos + n
elements (
E
>=
pos + n
and
size
() >=
pos + n
), otherwise the behavior is undefined.
These functions do the same thing: subspan () is provided for STL compatibility and sliced () is provided for Qt compatibility.
另请参阅 subspan (), first (QSpan<T,E>::size_type), and last (QSpan<T,E>::size_type).
[default]
QSpan::
QSpan
()
默认构造函数。
This constructor is only present if
E
is either zero (0) or
std::dynamic_extent
. In other words: only fixed-zero-sized or variable-sized spans are default-constructible.
另请参阅 extent and Variable-Size and Fixed-Size Spans .
[constexpr]
template <typename Range, QSpan<T, E>::if_compatible_range<Range> = true> QSpan::
QSpan
(
Range
&&
r
)
Constructs a QSpan referencing the data in the supplied range r .
注意:
This constructor participates in overload resolution only if
Range
is
a compatible range
.
[constexpr]
QSpan::
QSpan
(
std::initializer_list
<
QSpan
<
T
,
E
>
::value_type
>
il
)
Constructs a QSpan referencing the data in the supplied initializer list il .
注意:
This constructor participates in overload resolution only if
T
is
const
-qualified.
注意:
This constructor is
noexcept
only if
E
is
std::dynamic_extent
.
注意:
若
E
不是
std::dynamic_extent
and the size of
il
不是
E
, the behavior is undefined.
另请参阅 Const and Mutable Spans .
[constexpr]
template <typename It, QSpan<T, E>::if_compatible_iterator<It> = true> QSpan::
QSpan
(
It
first
,
It
last
)
Constructs a QSpan referencing the data starting at first and having length ( last - first ).
[first, last)
must be a valid range.
注意:
This constructor participates in overload resolution only if
It
is
a compatible iterator
.
[constexpr]
template <typename It, QSpan<T, E>::if_compatible_iterator<It> = true> QSpan::
QSpan
(
It
first
,
qsizetype
count
)
Constructs a QSpan referencing the data starting at first and having length count .
[first, count)
must be a valid range.
注意:
This constructor participates in overload resolution only if
It
is
a compatible iterator
.
[constexpr]
auto
QSpan::
back
() const
Returns a reference to the last element in the span.
The span must not be empty, otherwise the behavior is undefined.
另请参阅 operator[] (), front (), size (), empty (),和 Const and Mutable Spans .
[constexpr noexcept]
auto
QSpan::
begin
() const
Returns an interator pointing at the beginning of the span.
因为 QSpan iterators are just pointers, this is the same as calling data ().
另请参阅 end (), cbegin (), rbegin (), crbegin (), data (),和 Const and Mutable Spans .
[constexpr noexcept]
auto
QSpan::
cbegin
() const
返回 const_iterator pointing to the beginning of the span.
This will return a read-only iterator even if
T
不是
const
:
QSpan<int> span = ~~~; *span.begin() = 42; // OK *span.cbegin() = 42; // ERROR: cannot assign through a const_iterator
另请参阅 cend (), begin (), crbegin (), rbegin (), data (),和 Const and Mutable Spans .
[constexpr noexcept]
auto
QSpan::
cend
() const
返回 const_iterator pointing to one past the end of the span.
另请参阅 cbegin (), end (), crend (), rend (), data (), size (),和 Const and Mutable Spans .
[constexpr noexcept]
auto
QSpan::
crbegin
() const
返回 const_reverse_iterator pointing to the beginning of the reversed span.
另请参阅 crend (), rbegin (), cbegin (), begin (),和 Const and Mutable Spans .
[constexpr noexcept]
auto
QSpan::
crend
() const
返回 const_reverse_iterator pointing to one past the end of the reversed span.
另请参阅 crbegin (), rend (), cend (), end (),和 Const and Mutable Spans .
[constexpr noexcept]
auto
QSpan::
data
() const
Returns a pointer to the beginning of the span.
The same as calling begin ().
另请参阅 begin (), front (),和 Const and Mutable Spans .
[constexpr noexcept]
auto
QSpan::
end
() const
Returns an iterator pointing to one past the end of the span.
因为
QSpan
iterators are just pointers, this it the same as calling
data() + size()
.
另请参阅 begin (), cend (), rend (), crend (), data (), size (),和 Const and Mutable Spans .
[constexpr noexcept(...)]
template <std::size_t Count>
auto
QSpan::
first
() const
返回
fixed-sized
span of size
Count
referencing the first
Count
elements of
*this
.
The span must hold at least
Count
elements (
E
>=
Count
and
size
() >=
Count
), otherwise the behavior is undefined.
注意:
This function is noexcept when
subspan_always_succeeds_v<Count>
is
true
.
另请参阅 first (QSpan<T,E>::size_type), last (),和 subspan ().
[constexpr]
auto
QSpan::
first
(
QSpan
<
T
,
E
>
::size_type
n
) const
返回
variable-sized
span of size
n
referencing the first
n
elements of
*this
.
n must be non-negative.
The span must hold at least
n
elements (
E
>=
n
and
size
() >=
n
), otherwise the behavior is undefined.
另请参阅 first<N> (), last (QSpan<T,E>::size_type), subspan (QSpan<T,E>::size_type), subspan (QSpan<T,E>::size_type, QSpan<T,E>::size_type), and sliced ().
[constexpr]
auto
QSpan::
front
() const
Returns a reference to the first element in the span.
The span must not be empty, otherwise the behavior is undefined.
另请参阅 operator[] (), back (), size (), empty (),和 Const and Mutable Spans .
[constexpr noexcept(...)]
template <std::size_t Count>
auto
QSpan::
last
() const
返回
fixed-sized
span of size
Count
referencing the last
Count
elements of
*this
.
The span must hold at least
Count
elements (
E
>=
Count
and
size
() >=
Count
), otherwise the behavior is undefined.
注意:
This function is noexcept when
subspan_always_succeeds_v<Count>
is
true
.
另请参阅 last (QSpan<T,E>::size_type), first (),和 subspan ().
[constexpr]
auto
QSpan::
last
(
QSpan
<
T
,
E
>
::size_type
n
) const
返回
variable-sized
span of size
n
referencing the last
n
elements of
*this
.
n must be non-negative.
The span must hold at least
n
elements (
E
>=
n
and
size
() >=
n
), otherwise the behavior is undefined.
另请参阅 last (), first (QSpan<T,E>::size_type), subspan (QSpan<T,E>::size_type), subspan (QSpan<T,E>::size_type, QSpan<T,E>::size_type), and sliced ().
[constexpr noexcept]
auto
QSpan::
rbegin
() const
返回 reverse_iterator pointing to the beginning of the reversed span.
另请参阅 rend (), crbegin (), begin (), cbegin (),和 Const and Mutable Spans .
[constexpr noexcept]
auto
QSpan::
rend
() const
返回 reverse_iterator pointing to one past the end of the reversed span.
另请参阅 rbegin (), crend (), end (), cend (),和 Const and Mutable Spans .
[constexpr noexcept]
auto
QSpan::
size
() const
Returns the size of the span, that is, the number of elements it references.
另请参阅 size_bytes (), empty (),和 isEmpty ().
[constexpr noexcept]
auto
QSpan::
size_bytes
() const
Returns the size of the span in bytes, that is, the number of elements multiplied by
sizeof(T)
.
另请参阅 size (), empty (),和 isEmpty ().
[constexpr noexcept(...)]
template <std::size_t Offset, std::size_t Count>
auto
QSpan::
subspan
() const
Returns a span of size
Count
referencing the
Count
elements of this span starting at
Offset
.
若
*this
is a variable-sized span, the return type is a variable-sized span, otherwise it is a fixed-sized span.
This span must hold at least
Offset + Count
elements (
E
>=
Offset + Count
and
size
() >=
Offset + Count
), otherwise the behavior is undefined.
注意:
This function is noexcept when
subspan_always_succeeds_v<Offset + Count>
is
true
.
另请参阅 subspan (QSpan<T,E>::size_type, QSpan<T,E>::size_type), subspan(), first (), last (),和 Variable-Size and Fixed-Size Spans .
[constexpr noexcept(...)]
template <std::size_t Offset>
auto
QSpan::
subspan
() const
Returns a span of size
E - Offset
referencing the remainder of this span after dropping the first
Offset
元素。
若
*this
is a variable-sized span, the return type is a variable-sized span, otherwise it is a fixed-sized span.
This span must hold at least
Offset
elements (
E
>=
Offset
and
size
() >=
Offset
), otherwise the behavior is undefined.
注意:
This function is noexcept when
subspan_always_succeeds_v<Offset>
is
true
.
另请参阅 subspan (QSpan<T,E>::size_type), subspan (), first (), last (),和 Variable-Size and Fixed-Size Spans .
[constexpr]
QSpan
<
T
,
E
>
::reference
QSpan::
operator[]
(
QSpan
<
T
,
E
>
::size_type
idx
) const
Returns a reference to the element at index idx in the span.
The index must be in range, that is, idx >= 0 and idx < size (), otherwise the behavior is undefined.
另请参阅 front (), back (), size (), empty (),和 Const and Mutable Spans .
The second template argument of
QSpan<T, E>
, that is,
E
. This is
std::dynamic_extent
for
variable-sized spans
.
注意:
While all other sizes and indexes in
QSpan
use qsizetype, this variable, like
E
, is actually of type
size_t
, for compatibility with
std::span
and
std::dynamic_extent
.
另请参阅 size ().
[noexcept, since 6.8]
auto
as_bytes
(
QSpan
<
T
,
E
>
s
)
返回
s
作为
QSpan<const std::byte, E'>
whose
size
() is equal to
s.size_bytes()
.
若
E
is
std::dynamic_extent
then so is
E'
。否则,
E' = E * sizeof(T)
.
注意:
q20::dynamic_extent
is a C++17 backport of C++20's
std::dynamic_extent
.
该函数在 Qt 6.8 引入。
另请参阅 as_writable_bytes (), size_bytes (),和 Const and Mutable Spans .
[noexcept, since 6.8]
auto
as_writable_bytes
(
QSpan
<
T
,
E
>
s
)
返回
s
作为
QSpan<std::byte, E'>
whose
size
() is equal to
s.size_bytes()
.
若
E
is
std::dynamic_extent
then so is
E'
。否则,
E' = E * sizeof(T)
.
注意:
此函数仅参与重载解析,若
!std::is_const_v<T>
.
注意:
q20::dynamic_extent
is a C++17 backport of C++20's
std::dynamic_extent
.
该函数在 Qt 6.8 引入。
另请参阅 as_bytes (), size_bytes (),和 Const and Mutable Spans .