vector

本文详细介绍C++标准模板库中的std::vector容器及其常用成员函数,并解释如何正确使用这些功能。通过本文的学习,读者将能有效运用vector容器,替代传统C风格动态数组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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的成员函数)

FunctionDescription
assignErases a vector and copies the specified elements to the empty vector.
atReturns a reference to the element at a specified location in the vector.
backReturns a reference to the last element of the vector.
beginReturns a random-access iterator to the first element in the container.
capacityReturns the number of elements that the vector could contain without allocating more storage.
clearErases the elements of the vector.
emptyTests if the vector container is empty.
endReturns a random-access iterator that points just beyond the end of the vector.
eraseRemoves an element or a range of elements in a vector from specified positions.
frontReturns a reference to the first element in a vector.
get_allocatorReturns an object to the allocator class used by a vector.
insertInserts an element or a number of elements into the vector at a specified position.
max_sizeReturns the maximum length of the vector.
pop_backDeletes the element at the end of the vector.
push_backAdds an element to the end of the vector.
rbeginReturns an iterator to the first element in a reversed vector.
rendReturns an iterator to the end of a reversed vector.
resizeSpecifies a new size for a vector.
reserveReserves a minimum length of storage for a vector object.
sizeReturns the number of elements in the vector.
swapExchanges the elements of two vectors.
vectorConstructs 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

OperatorDescription
operator[]Returns a reference to the vector element at a specified position.

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值