C++STL容器 – std::vector元素访问方式总结
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++STL容器 – std::vector元素访问方式总结
原文链接:https://www.stubbornhuang.com/1977/
发布于:2022年02月22日 16:37:19
修改于:2022年02月22日 16:37:19

1 std::vector元素访问方式
std::vector提供多种方式访问容器中的元素。std::vector的at函数,[]操作符,front函数,back函数都可以对std::vector的元素进行读写操作。
1.1 at函数
访问指定索引元素,并在访问时进行索引边界检查。如果索引越界,将抛出std::out_of_range异常。
std::vector<int> myVector{1,2,3,4,5};
for (int i = 0; i < myVector.size(); ++i)
{
std::cout << myVector.at(i) << " ";
}
std::cout << std::endl;
for (int i = 0; i < myVector.size(); ++i)
{
myVector.at(i) = myVector.at(i) - 1;
std::cout << myVector.at(i) << " ";
}
std::cout << std::endl;
结果:
1 2 3 4 5
0 1 2 3 4
可通过以下代码捕获索引越界异常:
try
{
myVector.at(6) = 666;
}
catch (std::out_of_range const& exception_info)
{
std::cout << exception_info.what() << std::endl;
}
1.2 []操作符
访问指定索引元素,但是不进行索引越界检查。
std::vector<int> myVector{1,2,3,4,5};
for (int i = 0; i < myVector.size(); ++i)
{
std::cout << myVector[i] << " ";
}
std::cout << std::endl;
for (int i = 0; i < myVector.size(); ++i)
{
myVector[i] = myVector[i] - 1;
std::cout << myVector[i] << " ";
}
std::cout << std::endl;
结果:
1 2 3 4 5
0 1 2 3 4
1.3 front函数
访问std::vector的第一个元素。
std::vector<int> myVector{1,2,3,4,5};
std::cout << myVector.front() << std::endl;
myVector.front() = 10;
std::cout << myVector.front() << std::endl;
结果:
1
10
1.4 back函数
访问std::vector的最后一个元素。
std::vector<int> myVector{1,2,3,4,5};
std::cout << myVector.back() << std::endl;
myVector.back() = 10;
std::cout << myVector.front() << std::endl;
结果:
5
10
1.5 data函数
返回指向用作元素存储的基础数组的指针。对于非空容器,返回指向第一个元素的地址的指针。
std::vector<int> myVector{1,2,3,4,5};
const unsigned int vec_size = myVector.size();
const int* vec_pointer = myVector.data();
for (int i = 0; i < vec_size; ++i)
{
std::cout << vec_pointer[i] << " ";
}
std::cout << std::endl;
结果:
1 2 3 4 5
当前分类随机文章推荐
- C++ - 使用Crypto++/CryptoPP加解密库对字符串或者文件进行AES256加密 阅读2953次,点赞1次
- C++11 - 解析并获取可变参数模板中的所有参数 阅读1126次,点赞0次
- C++ – 字节数组byte[]或者unsigned char[]与float的相互转换 阅读1684次,点赞0次
- C++ - 阿拉伯数字字符串转换为中文读法的中文字符串,支持小数点 阅读1161次,点赞0次
- C++ - 求解std::vector
中topk数值以及topk数值对应的索引 阅读2173次,点赞0次 - C++ - std::map - 存储动态指针时正确释放内存 阅读4039次,点赞1次
- C++ - 使用std::chrono获取当前秒级/毫秒级/微秒级/纳秒级时间戳 阅读2964次,点赞0次
- C++ 11 - final关键字简要介绍 阅读1876次,点赞0次
- C++ - 将Unicode std::wstring字符串转换为Unicode std::string转义字符,类似于\uxxxx的形式 阅读1417次,点赞0次
- C++11/std::condition_variable - 生产者消费者模型 阅读2715次,点赞0次
全站随机文章推荐
- 资源分享 - Artificial Intelligence - A Modern Approach , Fourth Edition 英文高清PDF下载 阅读4051次,点赞0次
- OpenCV - 打开摄像头并用窗口显示摄像头的内容 阅读2845次,点赞0次
- 资源分享 - 深度学习 花书 AI圣经(Deep Learning) 英文pdf下载 阅读2964次,点赞1次
- 资源分享 - Game Programming Patterns 英文高清PDF下载 阅读1505次,点赞0次
- 资源分享 - Mathematical Basics of Motion and Deformation in Computer Graphics , Second Edition 英文PDF下载 阅读451次,点赞0次
- 资源分享 - Real-Time 3D Graphics with WebGL 2 , Second Edition英文高清PDF下载 阅读806次,点赞0次
- Python - 不依赖第三方库对类对象进行json序列化与反序列化 阅读1308次,点赞0次
- TensorRT - onnx_graphsurgeon工具库的安装与API简介 阅读512次,点赞0次
- 资源分享 - Physically Based Rendering From Theory To Implementation (Second Edition)英文高清PDF下载 阅读2604次,点赞2次
- 资源分享 - Video Game Optimization 英文高清PDF下载 阅读1229次,点赞0次
评论
167