single_view
class (C++標準連結庫)
只有一個項目的檢視。 此檢視適用於呼叫需要在其中至少有一個元素的檢視中提供之程式代碼的測試用途。
語法
template<std::copy_constructible T>
requires std::is_object_v<T>
class single_view : public ranges::view_interface<single_view<T>>
範本參數
T
項目的類型。
檢視特性
如需下列專案的描述,請參閱 檢視類別特性
特性 | 描述 |
---|---|
範圍配接器 | views::single |
基礎範圍 | 無 |
項目類型 | 建立 時 single_view 指定 |
檢視反覆運算器類別 | contiguous_range |
大小 | 一律會傳回 1 |
為 const -iterable |
Yes |
通用範圍 | Yes |
借用範圍 | No |
成員
成員函式 | 說明 |
---|---|
建構函式C++20 | single_view 建構 。 |
begin C++20 |
取得 專案的反覆運算器。 |
data C++20 |
取得專案的指標。 |
end C++20 |
取得檢視結尾的 sentinel。 |
size C++20 |
取得項目數目。 一律傳回 1 。 |
繼承自 view_interface |
說明 |
back C++20 |
取得專案。 |
empty C++20 |
測試檢視是否空白(一律傳 false 回 )。 |
front C++20 |
取得專案。 |
operator[] C++20 |
取得位於指定位置的專案(只有位置 0 有效)。 |
operator bool C++20 |
測試檢視是否不是空的 (一律傳 false 回)。 |
備註
建立 single_view
的最佳方式是使用 views::single
範圍配接器。 範圍配接器是建立檢視類別的預定方式。 如果您想要建立自己的自定義檢視類型,則會公開檢視類型。
中的值 single_view
可以修改,除非範本值為 const
。 例如: single_view<const float> sv{3.14} // this value can't be modified because it's const
。
需求
標頭: <ranges>
(自C++20起)
命名空間:std::ranges
需要編譯程式選項:/std:c++20
或更新版本。
建構函式
建立 single_view
的執行個體。
1) single_view() = default;
2) constexpr explicit single_view(const T& t);
3) constexpr explicit single_view(T&& t);
4) template<class... Args>
requires constructible_from<T, Args...>
constexpr single_view(in_place_t, Args&&... args);
參數
t
專案值。
如需範本參數類型的相關信息,請參閱 範本參數。
備註
建立 single_view
的最佳方式是使用 views::single
範圍配接器。
1) 使用 single_view
預設建構之指定型別的單一專案建立 。 例如,single_view<float> sv{}
使用預設建構為 0.0
的 型float
別單一項目來建立 single_view
。
2) 使用 single_view
從指定自變數複製初始化之指定型別的單一專案建立 。 例如,single_view<myObjectType> sv{myObject}
使用從自變數複製初始化之類型的myObjectType
單一項目來建立 single_view
。
3) 使用 single_view
從 自變數移動初始化之指定型別的單一專案建立 。
4) 使用 初始化之指定型(std::forward<Args>(args)...)
別的單一項目來建立 single_view
。
例 single_view
/// requires /std:c++20 or higher
#include <ranges>
#include <iostream>
#include <string>
#include <tuple>
int main()
{
std::ranges::single_view<int> sv{7};
std::cout << sv.front() << " " << *sv.data() << "\n"; // 7 7
std::ranges::single_view<std::tuple<int, std::string>> sv2{{6502, "8-bit"}};
std::cout << std::get<0>(sv2[0]) << " " << std::get<1>(sv2[0]) << "\n"; // 6502 8-bit
}
7 7
6502 8-bit
begin
取得檢視中單一專案的指標。
constexpr T* begin() noexcept;
constexpr const T* begin() const noexcept;
參數
無。
傳回值
內單一專案的 single_view
指標。
data
取得 中 single_view
單一專案的指標。
constexpr T* data() noexcept;
constexpr const T* data() const noexcept;
參數
無。
傳回值
中項目指標 single_view
。
end
取得項目之後 sentinel 的指標。
constexpr T* end() noexcept;
constexpr const T* end() const noexcept;
參數
無。
傳回值
緊接在 元素後面的 sentinel 指標。
size
取得檢視中的項目數目。 一律傳回 1
。
static constexpr size_t size() noexcept;
參數
無。
傳回值
1