Introduction(介绍)
This article aims to introduce the std::vector as well as cover some of the most common vector member functions and how to use them properly. The article will also discuss predicates and function pointers as used in common iterator algorithms such as remove_if() and for_each(). After reading this article, the reader should be able to use the vector container effectively and should find no use for dynamic C-style arrays again.
这边文章主旨是介绍std::vector和其常用的成员函数,以及如何使用它们。
Vector Overview
The vector is part of the C++ Standard Template Library (STL); an amalgamation of general-purpose, templatized classes and functions that implement a plethora of common data structures and algorithms. The vector is considered a container class. Like containers in real-life, these containers are objects that are designed to hold other objects. In short, a vector is a dynamic array designed to hold objects of any type, and capable of growing and shrinking as needed.
In order to use vector, you need to include the following header file:
#include <vector>
The vector is part of the std namespace, so you need to qualify the name. This can be accomplished as shown here:
using std::vector; vector<int> vInts;
or you can fully qualify the name like this:
std::vector<int> vInts;
I do however suggest that you refrain from declaring global namespaces such as:
using namespace std;
This pollutes the global namespace and may cause problems in later implementations. As for the interface to the vector container, I have listed the member functions and operators of vector in the tables below.
Vector Member Functions(Vector的成员函数)
| Function | Description |
assign | Erases a vector and copies the specified elements to the empty vector. |
at | Returns a reference to the element at a specified location in the vector. |
back | Returns a reference to the last element of the vector. |
begin | Returns a random-access iterator to the first element in the container. |
capacity | Returns the number of elements that the vector could contain without allocating more storage. |
clear | Erases the elements of the vector. |
empty | Tests if the vector container is empty. |
end | Returns a random-access iterator that points just beyond the end of the vector. |
erase | Removes an element or a range of elements in a vector from specified positions. |
front | Returns a reference to the first element in a vector. |
get_allocator | Returns an object to the allocator class used by a vector. |
insert | Inserts an element or a number of elements into the vector at a specified position. |
max_size | Returns the maximum length of the vector. |
pop_back | Deletes the element at the end of the vector. |
push_back | Adds an element to the end of the vector. |
rbegin | Returns an iterator to the first element in a reversed vector. |
rend | Returns an iterator to the end of a reversed vector. |
resize | Specifies a new size for a vector. |
reserve | Reserves a minimum length of storage for a vector object. |
size | Returns the number of elements in the vector. |
swap | Exchanges the elements of two vectors. |
vector | Constructs a vector of a specific size or with elements of a specific value or with a specific allocator or as a copy of some other vector. |
Vector Operators
| Operator | Description |
operator[] | Returns a reference to the vector element at a specified position. |
本文详细介绍C++标准模板库中的std::vector容器及其常用成员函数,并解释如何正确使用这些功能。通过本文的学习,读者将能有效运用vector容器,替代传统C风格动态数组。

10万+

被折叠的 条评论
为什么被折叠?



