common_view
class (C++標準連結庫)
取得可能有不同反覆運算器和 sentinel 類型的範圍,並建立具有相同反覆運算器和 sentinel 類型的檢視。 這適用於呼叫接受反覆運算器配對所指定範圍的 STL 演算法。
語法
template<ranges::view V>
requires (!ranges::common_range<V> && std::copyable<ranges::iterator_t<V>>)
class common_view : public ranges::view_interface<common_view<V>>;
範本參數
V
基礎檢視的類型。
檢視特性
如需下列專案的描述,請參閱 檢視類別特性
特性 | 描述 |
---|---|
範圍配接器 | views::common |
基礎範圍 | 必須滿足 forward_range 或更新版本 |
項目類型 | 與基礎範圍相同 |
檢視反覆運算器類別 | forward_range 或 random_access_range 當基礎範圍滿足 random_access_range 和時 sized_range |
大小 | 只有在基礎範圍滿足時才 sized_range |
為 const -iterable |
只有當基礎範圍是 const 可反覆運算時 |
通用範圍 | Yes |
借用範圍 | 只有在基礎範圍滿足時才 borrowed_range |
成員
成員函式 | 說明 |
---|---|
建構函式C++20 | common_view 建構 。 |
base C++20 |
取得基礎檢視。 |
begin C++20 |
取得檢視中第一個專案的反覆運算器。 |
end C++20 |
取得檢視結尾的 sentinel。 |
size C++20 |
取得檢視中的項目數目。 |
繼承自 view_interface |
說明 |
back C++20 |
取得最後一個專案。 |
data C++20 |
取得第一個專案的指標。 |
empty C++20 |
測試檢視是否為空白。 |
front C++20 |
取得第一個專案。 |
operator[] C++20 |
取得位於指定位置的專案。 |
operator bool C++20 |
測試檢視是否不是空的。 |
備註
建立 common_view
的最佳方式是使用 views::common
範圍配接器。 範圍配接器是建立檢視類別的預定方式。 如果您想要建立自己的自定義檢視類型,則會公開檢視類型。
此檢視適用於將具有不同反覆運算器/sentinel 類型的範圍傳遞至預期相同舊版演算法的範圍。
需求
標頭: <ranges>
(自C++20起)
命名空間:std::ranges
需要編譯程式選項:/std:c++20
或更新版本。
建構函式
建立 common_view
的執行個體。
1) common_view() = default;
2) constexpr explicit common_view(V v);
參數
v
基礎檢視。
如需範本參數類型的相關信息,請參閱 範本參數。
備註
1) 預設會 common_view
建構 。
2) 使用 std::move(v)
從基礎檢視建構 common_view
。 如果 V
是常見的範圍,以避免誤用會對效能造成負面影響,則會產生錯誤。
base
取得基礎檢視的複本。
// Uses a copy constructor to return the underlying view
constexpr V base() const& requires std::copy_constructible<V>;
// Uses a move constructor to return the underlying view
constexpr V base() &&;
begin
取得第一個專案的反覆運算器。
constexpr auto begin();
constexpr auto begin() const requires range<const V>;
傳回值
指向檢視中第一個專案的反覆運算器:
end
取得檢視結尾的 sentinel。
constexpr auto end();
constexpr auto end() const requires ranges::range<const V>;
傳回值
檢視中最後一個專案後面的 sentinel:
size
取得檢視中的項目數目。
constexpr auto size() requires ranges::sized_range<V>;
constexpr auto size() const requires ranges::sized_range<const V>;
參數
無。
傳回值
檢視中的項目數目。