c++中的向量
A Vectors in C++ is an array-like container that can change dynamically in size. Being a part of the C++ STL, a Vector can support various dynamic array operations.
C ++中的Vector是类似于数组的容器,可以动态改变大小。 作为C ++ STL的一部分,Vector可以支持各种动态数组操作。
C ++中的向量概述 (Overview of Vectors in C++)
Vectors, being similar to arrays, are stored in contiguous memory locations. This means that the access time is very quick.
与数组相似的向量存储在连续的内存位置中。 这意味着访问时间非常快。
Vectors use a dynamically allocated array, which means that it must be reallocated if it ever reaches its limit.
向量使用动态分配的数组,这意味着一旦达到极限,就必须重新分配向量。
This reallocation operation is a bit expensive, which is the one disadvantage of Vectors.
这种重新分配操作有点昂贵,这是Vector的一个缺点。
The below image shows a scenario where the Vector reallocation occurs.
下图显示了发生Vector重新分配的场景。
The original vector (<1, 2>
) with a capacity of 2, is now resized dynamically to a capacity of 4 before a third element can be added to the now-empty locations.
现在可以将容量为2的原始向量( <1, 2>
)动态调整为容量4,然后才能将第三个元素添加到现在为空的位置。

用C ++创建向量 (Create a Vector in C++)
To use this container, we must first include the header file vector
.
要使用此容器,我们必须首先包含头文件vector
。
#include <vector>
We can now create a new vector using :
我们现在可以使用创建一个新的向量:
std::vector<type> a;
Here, <type>
can refer to any data type, such as int
, float
, char
, etc.
在这里, <type>
可以引用任何数据类型,例如int
, float
, char
等。
We can also directly assign elements during initialization.
我们还可以在初始化期间直接分配元素。
#include <vector>
using namespace std;
vector<int> a = {1, 2, 3, 4};
This will create an integer vector initialized to <1, 2, 3, 4>
. So the first element is 1, and the last element is 4.
这将创建一个初始化为<1, 2, 3, 4>
的整数矢量。 所以第一个元素是1,最后一个元素是4。
Now that we have our vector, let’s look at some of the vector operations involving it.
现在我们有了向量,让我们看一下涉及它的一些向量运算。
C ++中的向量运算 (Vector operations in C++)
There are various methods that we can use on the vector container, some of them being:
我们可以在向量容器上使用多种方法,其中一些是:
迭代器方法 (Iterator Methods)
- begin() -> Returns an iterator to the beginning begin ()->将迭代器返回到开头
- end() -> Returns an iterator to the end end ()->将迭代器返回到末尾
- rbegin() -> Returns a reverse iterator to the reverse beginning rbegin ()->将反向迭代器返回到反向开始
- rend() -> Returns a reverse iterator to the reverse end rend ()->将反向迭代器返回到反向端点
Here is an example showing the above methods. We cannot directly print a vector using cout
, and must access elements of a vector in a for
loop.
这是显示上述方法的示例。 我们不能使用cout
直接打印矢量,而必须在for
循环中访问矢量的元素。
Iterators can be used for this purpose.
迭代器可用于此目的。
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> a = {1, 2, 3, 4};
for (auto it = a.begin(); it != a.end(); it++)
cout << *it << " ";
return 0;
}
Output
输出量
1 2 3 4
Similarly, we can print the vector in reverse, using rbegin()
and rend()
.
同样,我们可以使用rbegin()
和rend()
反向打印矢量。
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> a = {1, 2, 3, 4};
for (auto it = a.rbegin(); it != a.rend(); it++)
cout << *it << " ";
return 0;
}
Output
输出量
4 3 2 1
容量方法 (Capacity methods)
- size() -> Returns the size size ()->返回大小
- max_size() -> Returns the maximum size that can be allocated max_size ()->返回可以分配的最大大小
- resize(SIZE) -> Resizes the vector to SIZE elements 调整大小 ( SIZE )->将向量调整为SIZE元素
- capacity() -> Returns the size of the allocated memory capacity Capacity ()->返回分配的内存容量的大小
- empty() -> Checks if the vector is empty empty ()->检查向量是否为空
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> a = {1, 2, 3, 4, 5, 6};
for (auto it = a.begin(); it != a.end(); it++)
cout << *it << " ";
cout << endl;
cout << "Is the vector empty? " << (a.empty() ? "Yes" : "No") << endl;
cout << "Size: " << a.size() << endl;
cout << "Capacity: " << a.capacity() << endl;
cout << "Maximum Size: " << a.max_size() << endl;
// Let's resize the vector to 4
a.resize(4);
cout << "Vector after resize(): \n";
for (auto it = a.begin(); it != a.end(); it++)
cout << *it << " ";
cout << endl;
return 0;
}
Output
输出量
1 2 3 4 5 6
Is the vector empty? No
Size: 6
Capacity: 6
Maximum Size: 2305843009213693951
Vector after resize():
1 2 3 4
元素访问方法 (Element Access methods)
- Using
[]
operator. Same as for arrays. 使用[]
运算子。 与数组相同。 - at(INDEX) -> Access an element at position INDEX 在 ( INDEX )->访问位置INDEX处的元素
- front() -> Access the first element front() ->访问第一个元素
- back() -> Access the last element back() ->访问最后一个元素
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> a = {1, 2, 3, 4, 5, 6};
cout << "Access using []:\n";
for (unsigned int i=0; i<a.size(); i++)
cout << a[i] << " ";
cout << endl;
cout << "Access using at():\n";
for (unsigned int i=0; i<a.size(); i++)
cout << a.at(i) << " ";
cout << endl;
cout << "First Element: " << a.front() << endl;
cout << "Last Element: " << a.back() << endl;
return 0;
}
Output
输出量
Access using []:
1 2 3 4 5 6
Access using at():
1 2 3 4 5 6
First Element: 1
Last Element: 6
修饰符方法 (Modifier methods)
- push_back() -> Add element at the end push_back() ->在最后添加元素
- pop_back() -> Delete the last element pop_back() ->删除最后一个元素
- clear() -> Clear content clear() ->清除内容
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> a = {1, 2, 3, 4, 5, 6};
a.push_back(7);
a.push_back(8);
cout << "vector after push_back():\n";
for(unsigned int i=0; i<a.size(); i++)
cout << a[i] << " ";
cout << endl;
a.pop_back();
cout << "vector after pop_back():\n";
for(unsigned int i=0; i<a.size(); i++)
cout << a[i] << " ";
return 0;
}
As you can see, we do some push and pop operations on the vector to dynamically change the size. This shows the dynamic nature of vectors.
如您所见,我们在向量上执行了一些推入和弹出操作,以动态更改大小。 这显示了向量的动态性质。
vector after push_back():
1 2 3 4 5 6 7 8
vector after pop_back():
1 2 3 4 5 6 7
参考资料 (References)
- cplusplus.com page on Vectors Vector上的cplusplus.com页面
翻译自: https://www.journaldev.com/35402/vectors-in-c-plus-plus
c++中的向量