std::initializer_list 類模板是在 C++ 11 中添加的,包含許多內置函數,用於使用初始值設定項列表執行各種操作。它提供了諸如 size()、begin()、end() 和構造函數之類的成員函數來構造、迭代和訪問初始值設定項列表的元素。
要使用initializer_list,您需要包含<initializer_list>C++ 程序中的標頭。
C++ 中的 std::initializer_list
在 C++ 中,std::initializer_list 是一個類模板,它允許我們使用值列表初始化輕量級對象。初始值設定項列表用於以方便的方式為變量、數組、類、函數、類的構造函數以及向量等標準容器設置值。
用法
initializer_list<T> name_of_list= { };
- 支撐初始化r用於構造initializer_list的對象。
- 它通常作為數組的包裝器來實現。
- 與向量等標準容器不同,複製初始值設定項列表的對象不會將整個元素複製到複製列表中。但初始值設定項列表的原始對象和複製對象都包含相同的元素。
C++ 中的 std::initializer_list 示例
示例 1:
下麵的示例演示了 C++ 中初始化列表的使用。
C++
// C++ program to demonstrate the use of initializer list
#include <initializer_list>
#include <iostream>
using namespace std;
int main()
{
// Initializing an object using initializer_list
initializer_list<int> num = { 2, 4, 6, 8, 10, 12 };
// Accessing elements
cout << "Numbers in the list are: ";
for (int it : num) {
cout << it << " ";
}
return 0;
}
Numbers in the list are: 2 4 6 8 10 12
Note Member initializer list and initializer_list are not the same. Both are different, it should not be confused with each other.
示例 2:
編寫程序來說明如何使用 initializer_list 來構造對象。
C++
// C++ program to illustrate the use of initializer_list in
// object construction
#include <iostream>
using namespace std;
#include <initializer_list>
// array type container constructed using initializer list
template <typename T> class MyContainer {
public:
// Constructor taking initializer_list as a parameter
MyContainer(initializer_list<T> values)
: list(values)
{
}
// Function to print all elements
void printList() const
{
for (const T& value : list) {
cout << value << " ";
}
cout << endl;
}
private:
initializer_list<T> list;
};
// diver code
int main()
{
// Creating an instance of MyContainer with
// initializer_list of int type
MyContainer<int> intContainer = { 1, 2, 3, 4, 5 };
cout << "Elements of Integer type are: ";
intContainer.printList();
cout << endl;
// Creating an instance of MyContainer with
// initializer_list of double type
cout << "Elements of double type are: ";
MyContainer<double> doubleContainer
= { 1.1, 2.2, 3.3, 4.4, 5.5 };
doubleContainer.printList();
cout << endl;
return 0;
}
Elements of Integer type are: 1 2 3 4 5 Elements of double type are: 1.1 2.2 3.3 4.4 5.5
std::initializer_list的成員函數
以下是std::initialzer_list類的一些常用成員函數:
S. 編號 |
函數名稱 |
說明 |
---|---|---|
1 |
begin() |
返回指向初始值設定項列表的第一個元素的指針。 |
2 |
end() |
返回指向初始值設定項列表的最後一個元素的指針。 |
3 |
size() |
size() 函數返回初始值設定項列表中存在的元素數量。 |
4 |
empty() |
如果初始值設定項列表為空,則此函數返回 true。否則為假。 |
5 |
data() |
返回指向底層數組容器的指針。 |
initializer_list的應用
除了構造對象之外,初始化列表還可用於以下情況:
1. 可變函數參數
<initializer_list> 用於將可變數量的參數傳遞給函數。
示例
下麵的示例演示了將初始值設定項列表傳遞給函數。
C++
// C++ program to demonstrate the passing of initializer
// list to a function.
#include <iostream>
using namespace std;
#include <initializer_list>
void myFunction(initializer_list<int> myList)
{
// Print the size (length) of myList
cout << "Size of myList: " << myList.size();
cout << "\n";
// Print elements of myList
cout << "Elements of myList: ";
// iterate to all the values of myList
for (int value : myList) {
// Print value at each iteration
cout << value << " ";
}
}
int main()
{
// Using initializer list when calling a function
myFunction({ 1, 2, 3, 4, 5 });
return 0;
}
Size of myList: 5 Elements of myList: 1 2 3 4 5
2. 將數據存儲在連續內存中
initializer_list 容器的元素可用於存儲數據,因為它是一個輕量級容器。
示例
下麵的示例演示了如何使用基於範圍的循環來訪問 initializer_list 的元素。
C++
// C++ program to demonstrate the use of range based loops to access elements of initializer_list.
#include <iostream>
using namespace std;
#include <initializer_list>
int main() {
initializer_list<int> list = {1, 2, 3, 4, 5};
// Using range-based for loop
for (int value : list) {
cout << value << " ";
}
cout << endl;
return 0;
}
1 2 3 4 5
3. 初始化標準容器
initializer_list 可用於使用元素列表初始化標準容器 就像向量一樣。
示例
下麵的示例演示了如何使用initializer_list來初始化標準容器。
C++
// C++ program to demonstrate the use of initializer_list to
// initialize standard containers.
#include <iostream>
#include <vector>
using namespace std;
#include <initializer_list>
void printVector(initializer_list<int> list)
{
// initialize vector using initializer_list
vector<int> myVector(list);
// Printing the elements of vector
for (int value : myVector) {
cout << value << " ";
}
cout << endl;
}
int main()
{
// pass initializer_list to function
printVector({ 1, 2, 3, 4, 5 });
return 0;
}
1 2 3 4 5
4. 初始化列表作為返回類型
initializer_list 可以用作從任何函數返回列表的返回類型。它允許函數返回多個值。
示例
下麵的示例演示了如何使用 initializer_list 作為返回類型。
C++
// C++ program to demonstratethe use of initializer_list as
// return type.
#include <initializer_list>
#include <iostream>
#include <vector>
using namespace std;
initializer_list<int> getNumbers()
{
return { 1, 2, 3, 4, 5 };
}
int main()
{
auto num = getNumbers();
// Use the generated numbers
for (auto it : num) {
cout << it << " ";
}
return 0;
}
1 2 3 4 5
initializer_list的限製
初始化列表也有一些與之相關的限製:
- 尺寸無法更改:initializer_list的大小在編譯時是固定的。它不具有矢量等標準容器的動態特性。初始化程序的大小一旦創建就無法更改。
- 無法隨機訪問元素:initializer_list僅支持前向迭代。我們無法使用索引作為標準容器來訪問所需的或隨機的元素。
- 不可變元素:initializer_list 中的元素是不可變的。創建列表後,將無法修改這些值。任何通過迭代器或任何其他方式修改元素的嘗試都將導致編譯錯誤。
相關用法
- C++11 std::move_iterator用法及代碼示例
- C++17 std::clamp用法及代碼示例
- C++17 std::lcm用法及代碼示例
- C++17 std::variant用法及代碼示例
- C++14 std::integer_sequence用法及代碼示例
- C++14 std::quoted用法及代碼示例
- C++17 std::cyl_bessel_i用法及代碼示例
- C++14 std::make_unique用法及代碼示例
- C++ cos()用法及代碼示例
- C++ sin()用法及代碼示例
- C++ asin()用法及代碼示例
- C++ atan()用法及代碼示例
- C++ atan2()用法及代碼示例
- C++ acos()用法及代碼示例
- C++ tan()用法及代碼示例
- C++ sinh()用法及代碼示例
- C++ ceil()用法及代碼示例
- C++ tanh()用法及代碼示例
- C++ fmod()用法及代碼示例
- C++ acosh()用法及代碼示例
- C++ asinh()用法及代碼示例
- C++ floor()用法及代碼示例
- C++ atanh()用法及代碼示例
- C++ log()用法及代碼示例
- C++ trunc()用法及代碼示例
注:本文由純淨天空篩選整理自manikandansanmugam大神的英文原創作品 std::initializer_list in C++ 11。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。