名前空間
変種
操作

std::span<T, Extent>::last

From cppreference.com
< cpp‎ | container‎ | span
 
 
 
 
template< std::size_t Count >
constexpr std::span<element_type, Count> last() const;
(1) (C++20以降)
constexpr std::span<element_type, std::dynamic_extent> last( size_type Count ) const;
(2) (C++20以降)

このspanの最後の Count 要素に対するビューを取得します。 Count > Extent の場合、プログラムは形式エラーとなります。 Count > size() の場合、動作は未定義です。

[編集] 返り値

r.data() == this->data() + (this->size() - Count) && r.size() == Count となるような、 *this の最後の Count 要素に対するビューであるspan r

[編集]

#include <iostream>
#include <span>
#include <string_view>
 
void println(std::string_view const title, auto const& container)
{
    std::cout << title << '[' << std::size(container) << "]{ ";
    for (auto const& elem : container)
        std::cout << elem << ", ";
    std::cout << "};\n";
};
 
void run(std::span<const int> span)
{
    println("span: ", span);
 
    std::span<const int, 3> span_last = span.last<3>();
    println("span.last<3>(): ", span_last);
 
    std::span<const int, std::dynamic_extent> span_last_dynamic = span.last(2);
    println("span.last(2): ", span_last_dynamic);
}
 
int main()
{
    int a[8]{1, 2, 3, 4, 5, 6, 7, 8};
    println("int a", a);
    run(a);
}

出力

int a[8]{ 1, 2, 3, 4, 5, 6, 7, 8, };
span: [8]{ 1, 2, 3, 4, 5, 6, 7, 8, };
span.last<3>(): [3]{ 6, 7, 8, };
span.last(2): [2]{ 7, 8, };

[編集] 関連項目

シーケンスの最初の N 個の要素からなるサブスパンを取得します
(public member function) [編集]
サブスパンを取得します
(public member function) [編集]
English 日本語 中文(简体) 中文(繁體)