c++用数组初始化向量
In this article, we’ll take a look at some of the ways to initialize a vector in C++. There are a bunch of ways to do this, so we’ll go through each approach.
在本文中,我们将介绍一些在C ++中初始化向量的方法。 有很多方法可以做到这一点,因此我们将介绍每种方法。
Let’s get started!
让我们开始吧!
方法1 :(推荐):使用初始化列表(C ++ 11及更高版本) (Method 1: (Recommended): Use an Initializer List (C++11 and above))
If your compiler supports the C++ version above C++11, you can simply initialize the vector using the {} notation.
如果您的编译器支持C ++ 11以上的C ++版本,则可以简单地使用{}表示法初始化向量 。
Since std::vector is a class, to initialize an object of this class using the above fashion, this refers to an Initializer List in C++.
由于std :: vector是一个类,因此要使用上述方式初始化该类的对象,因此它指的是C ++中的Initializer List 。
Here is an example using the initializer list declaration:
这是使用初始化列表声明的示例:
std::vector vec = { 1, 2, 3, 4, 5 };
Since initializer lists were introduced only in C++11, you need a minimum compiler version supporting at least C++11 to use this method.
由于初始化程序列表仅在C ++ 11中引入,因此您需要至少支持C ++ 11的最低编译器版本才能使用此方法。
Here is an example program to demonstrate this type of initialization:
这是一个示例程序来演示这种类型的初始化:
#include
#include int main() {// Initialize a vector using Initializer Listsstd::vector vec &#61; { 1, 2, 3, 4, 5 };// Use a range based for loop to print elements of the vectorfor (const auto &i: vec) {// Using access by reference to avoid copying// Using const since we&#39;re not modifying elements of the vectorstd::cout <}
Here, we use a range-based for-loop to print the vector elements.
在这里&#xff0c;我们使用基于范围的for循环来打印矢量元素。
Output
输出量
1
2
3
4
5
方法2&#xff1a;借助数组&#xff08;C &#43;&#43; 0x&#xff09;在C &#43;&#43;中初始化Vector (Method 2: Initialize a Vector in C&#43;&#43; with the help of an array (C&#43;&#43;0x))
If you’re using a C&#43;&#43;0x (C&#43;&#43;03, C&#43;&#43;07, etc) based compiler, you can still achieve the initialization using arrays.
如果您使用的是基于C &#43;&#43; 0x &#xff08;C &#43;&#43; 03&#xff0c;C &#43;&#43; 07等&#xff09;的编译器&#xff0c;则仍可以使用数组实现初始化。
int tmp[] &#61; { 1, 2, 3, 4, 5 };
std::vector vec( tmp, tmp &#43; sizeof(tmp)/sizeof(tmp[0]) );
Test this on a C&#43;&#43;0x based compiler, to verify that it works.
在基于C &#43;&#43; 0x的编译器上进行测试&#xff0c;以验证其是否有效。
#include
#include using namespace std;int main() {// Initialize a vector using Arraysint tmp[] &#61; { 1, 2, 3, 4, 5 };std::vector vec(tmp, tmp &#43; sizeof(tmp)/sizeof(tmp[0]));// Range based for loops not supported in older compilers!// Nor is auto!for (std::vector::iterator it &#61; vec.begin(); it !&#61; vec.end(); &#43;&#43;it) {std::cout <<*it <}
If you’re on Linux (g&#43;&#43; based compiler), compile and run using:
如果您使用的是Linux&#xff08;基于g &#43;&#43;的编译器&#xff09;&#xff0c;请使用以下命令进行编译和运行&#xff1a;
gcc -o test.out test.cpp -std&#61;c&#43;&#43;0x
./test.out
Output
输出量
1
2
3
4
5
方法3&#xff1a;使用库&#xff08;C &#43;&#43; 11及更高版本&#xff09; (Method 3: Using the library (C&#43;&#43;11 and above))
We can also use the library to achieve this.
我们还可以使用库来实现此目的。
The namespace has the list_of
construct to initialize vectors.
名称空间具有用于初始化向量的list_of
构造。
#include // Initialize a vector of {1, 2, 3, 4, 5}
std::vector vec &#61; boost::assign::list_of(1)(2)(3)(4)(5);
We can also use the overloaded &#43;
operator to declare a vector and add elements.
我们还可以使用重载的&#43;
运算符声明一个向量并添加元素。
// Courtesy: https://stackoverflow.com/a/2236233#include // Declare a vector
std::vector vec;// Add elements to it
vec &#43;&#61; 1, 2, 3, 4, 5;
However, this type of operator overloading is not advisable, as it can be confusing for many readers, as a person reading this code may also think that the first element will add to 1, the second element adding to 2, etc.
但是&#xff0c;这种类型的运算符重载是不可取的 &#xff0c;因为它可能会使许多读者感到困惑&#xff0c;因为阅读此代码的人可能还会认为第一个元素将加1&#xff0c;第二个元素加2&#xff0c;依此类推。
结论 (Conclusion)
In this article, we learned how we could initialize a vector in the C&#43;&#43; language in different ways.
在本文中&#xff0c;我们学习了如何以不同的方式用C &#43;&#43;语言初始化向量。
参考资料 (References)
- StackOverflow Question on initializing vectors 关于初始化向量的StackOverflow问题
- JournalDev Article on Initializer Lists JournalDev关于初始化程序列表的文章
翻译自: https://www.journaldev.com/37575/initialize-a-vector-in-c-plus-plus
c&#43;&#43;用数组初始化向量