vector

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

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.

 
【最优潮流】直流最优潮流(OPF)课设(Matlab代码实现)内容概要:本文档主要围绕“直流最优潮流(OPF)课设”的Matlab代码实现展开,属于电力系统优化领域的教学与科研实践内容。文档介绍了通过Matlab进行电力系统最优潮流计算的基本原理与编程实现方法,重点聚焦于直流最优潮流模型的构建与求解过程,适用于课程设计或科研入门实践。文中提及使用YALMIP等优化工具包进行建模,并提供了相关资源下载链接,便于读者复现与学习。此外,文档还列举了大量与电力系统、智能优化算法、机器学习、路径规划等相关的Matlab仿真案例,体现出其服务于科研仿真辅导的综合性平台性质。; 适合人群:电气工程、自动化、电力系统及相关专业的本科生、研究生,以及从事电力系统优化、智能算法应用研究的科研人员。; 使用场景及目标:①掌握直流最优潮流的基本原理与Matlab实现方法;②完成课程设计或科研项目中的电力系统优化任务;③借助提供的丰富案例资源,拓展在智能优化、状态估计、微电网调度等方向的研究思路与技术手段。; 阅读建议:建议读者结合文档中提供的网盘资源,下载完整代码与工具包,边学习理论边动手实践。重点关注YALMIP工具的使用方法,并通过复现文中提到的多个案例,加深对电力系统优化问题建模与求解的理解。
05-29
在 C++ 中,vector 是一个动态数组容器,可以存储任意类型的数据。使用 vector 可以方便地进行动态内存分配和管理,而不需要手动进行内存分配和释放。 vector 提供了一系列的成员函数,可以方便地进行元素的插入、删除、查找和访问等操作。以下是一些常用的操作: 1. 创建 vector 对象: ```c++ #include <vector> std::vector<int> vec; // 创建一个空的 vector std::vector<int> vec1(10); // 创建一个包含 10 个默认值的 vector std::vector<int> vec2(10, 0); // 创建一个包含 10 个值为 0 的 vector std::vector<int> vec3 = {1, 2, 3, 4}; // 创建一个包含 4 个元素的 vector,初始值为 1, 2, 3, 4 std::vector<int> vec4(vec3); // 创建一个与 vec3 完全相同的新 vector ``` 2. 访问 vector 中的元素: ```c++ std::vector<int> vec = {1, 2, 3, 4}; std::cout << vec[0] << std::endl; // 访问第一个元素,输出 1 std::cout << vec.at(2) << std::endl; // 访问第三个元素,输出 3 std::cout << vec.front() << std::endl; // 访问第一个元素,输出 1 std::cout << vec.back() << std::endl; // 访问最后一个元素,输出 4 ``` 3. 插入和删除元素: ```c++ std::vector<int> vec = {1, 2, 3, 4}; vec.push_back(5); // 在末尾插入元素 5 vec.insert(vec.begin() + 2, 10); // 在第三个位置插入元素 10 vec.erase(vec.begin() + 1); // 删除第二个元素 ``` 4. 获取 vector 中的信息: ```c++ std::vector<int> vec = {1, 2, 3, 4}; std::cout << vec.size() << std::endl; // 输出 vector 的大小,即元素个数,输出 4 std::cout << vec.empty() << std::endl; // 判断 vector 是否为空,输出 0(因为不为空) ``` vector 还提供了一些其他的函数,如排序、反转、查找等,具体可以参考 C++ 标准库的文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值