C++ Arrays, std::array, std::vector 总结
原文来自: https://shendrick.net/Coding Tips/2015/03/15/cpparrayvsvector.html @Seth Hendrick
Original article: https://shendrick.net/Coding Tips/2015/03/15/cpparrayvsvector.html @Seth Hendrick
C-Style 数组
赋值
int myArray[3] = {1, 2, 3};
数组与指针
a[1]等价于*(a+1)
std::cout << std::boolalpha << (myArray[0] == *myArray) << std::endl;
std::cout << std::boolalpha << (myArray[1] == *(myArray + 1) << std::endl;
// Outputs:
// true
// true
数组大小
int myArray[5] = {1, 2, 3, 4, 5};
size_t arraySize = sizeof(myArray) / sizeof(int);
std::cout << "C-style array size: " << arraySize << std::endl;
// Outputs:
// C-style array size: 5
#include <iostream>
void printSize(int someArray[5]) {
std::cout << sizeof(someArray)/sizeof(int) << std::endl;
}
int main() {
int myArray[5] = {1, 2, 3, 4, 5};
printSize(myArray);
}
// Outputs:
// 2
第二个例子中, 函数没有返回正确的数组大小. 这是因为数组作为输入参数时, 传入的是一个size_t大小的指针, 在具体机器上大小可能为8字节, 而int类型大小是4个字节, 因为第二个例子相当于sizeof(size_t) / sizeof(int), 输出结果为2.
遍历循环
与上面一个小节类似, 在同一个作用范围内, 数组可以用C++11的遍历循环,
int main() {
int myArray[5] = {1, 2, 3, 4, 5};
for (int &i : myArray) {
std::cout << i << ", " << std::endl;
}
}
但是当数组被传入其他函数作为变量时, 遍历循环失效
#include <iostream>
void printElements(int someArray[5]) {
for (int &i : someArray) {
std::cout << i << ", " << std::endl;
}
}
int main() {
int myArray[5] = {1, 2, 3, 4, 5};
printElements(myArray);
}
原因还是一样, 被传入的是指针.
std::array
std::array是一个包装后的C数组, 在编译的时候必须确定数组大小.
声明数组
#include <array>
#include <iostream>
void printElements(const std::array<int, 5> &someArray) {
for (const int &i : someArray) {
std::cout << i << ", " << std::endl;
}
std::cout << "Size: " << someArray.size() << std::endl;
}
int main() {
std::array<int, 5> myArray = {1, 2, 3, 4, 5};
printElements(myArray);
}
// Outputs:
// 1,
// 2,
// 3,
// 4,
// 5,
// Size: 5
std::vector
std::array是C数组的封装, std::vector则完全不同于原来的C数组, 是heap上的动态数组, 数组大小在编译的时候可以不确定.
std::array可以看成如此封装
int a[5];
std::vector则是
int *a = net int[5];
C++ Arrays, std::array, std::vector 总结的更多相关文章
- C++中的数组array和vector,lambda表达式,C字符串加操作,C++中新类型数组(数组缓存),多元数组,new缓冲
使用C++风格的数组.不须要管理内存. array要注意不要溢出,由于它是栈上开辟内存. array适用于不论什么类型 #include<iostream> #include< ...
- std::array,std::vector,基于范围的for循环
std::array除了有传统数组支持随机访问.效率高.存储大小固定等特点外,还支持迭代器访问.获取容量.获得原始指针等高级功能.而且它还不会退化成指针T *给开发人员造成困惑. for( 元素名变量 ...
- C++ std::array
std::array template < class T, size_t N > class array; Code Example #include <iostream> ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- C++语言中std::array的神奇用法总结,你需要知道!
摘要:在这篇文章里,将从各个角度介绍下std::array的用法,希望能带来一些启发. td::array是在C++11标准中增加的STL容器,它的设计目的是提供与原生数组类似的功能与性能.也正因此, ...
- c++编译错误C2971:"std::array":array_size:包含非静态存储不能用作废类型参数;参见“std::array”的声明
在Qt5中这段代码编写有两种方式:一个编译成功,一个失败 成功版本: static constexpr size_t block_size = 0x2000;//8KB static constexp ...
- std::array中的std::get<n>()
模板函数std::get<n>()是一个辅助函数,它能够获取到容器的第 n 个元素.模板参数的实参必须是一个在编译时可以确定的常量表达式,编译时会对它检查. get<n>()模 ...
- 将std::array转换成std::tuple
template<typename Array, std::size_t... Index> decltype(auto) array2tuple_impl(const Array& ...
- C++ std::array 基本用法
#include <iostream> #include <string> #include <array> using namespace std; // htt ...
随机推荐
- pytest -- 测试的参数化
目录 1. @pytest.mark.parametrize标记 1.1. empty_parameter_set_mark选项 1.2. 多个标记组合 1.3. 标记测试模块 2. pytest_g ...
- Elasticsearch 安装 Head 插件
引子:在上一篇文章Elasticsearch入门(一):CentOS 7.6 安装ES 7.0.0中,我们讲了如何在CentOS 7.6环境下安装 Elasticsearch 7.0.下面,我将讲一讲 ...
- mysql——多表——内连接查询
内连接查询:可以查询两个或者两个以上的表,当两个表中存在表示相同意义的字段时,可以通过该字段来连接这两个表: 当该字段的值相等时,就查询出该记录. 前期准备两个表: ), d_id ), name ) ...
- 异常1(Exception)
父类 :Throwable(可抛出的) 有两个子类:Error(错误) Exception(异常) Error是所有错误类的父类,Exception是所有异常类的父类. 如图所示: 格式: ...
- CentOS添加使用
在本机安装虚拟机,虚拟机安装CentSO.也可以装双系统,双系统问题更多 环境:win7 64 位 1.查看电脑是否可虚拟化(在百度查) 2.查看电脑是否打开虚拟机设置,如果没有,百度如何开启 打开虚 ...
- 初步学习jquery学习笔记(一)
什么是jquery? Jquery是javascript的一个函数库包含以下功能: html元素选取 html元素的操作 css操作 html事件的函数 javacript的特效 html的遍历和修改 ...
- Longest Subsequence CodeForces - 632D (lcm)
大意: 给定序列$a$, 求选出最长的一个子序列, 使得lcm不超过m. 刚开始想复杂了, 想着枚举gcd然后背包, 这样复杂度就是$O(\sum\limits_{i=1}^m \frac{m\sig ...
- 10.css3动画--过渡动画--trasition
Transition简写属性. Transition-property规定应用过渡的css属性的名称. . Transition-timing-function过渡效果的时间曲线,默认是ease. L ...
- eclipse中web项目tomcat的设置
1. 出现的问题: web开发中(eclipse环境),为本地项目添加tomcat,我们一般都会选择直接添加.在本次开发中突然遇到一个问题:因为项目涉及到文件上传,我利用MultipartFile进 ...
- vue打包时,assets目录 和static目录下文件的处理区别(nodeModule中插件源码修改后,打包后的文件应放在static目录)
为了回答这个问题,我们首先需要了解Webpack如何处理静态资产.在 *.vue 组件中,所有模板和CSS都会被 vue-html-loader 及 css-loader 解析,并查找资源URL.例如 ...