原文来自: 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 数组

赋值

  1. int myArray[3] = {1, 2, 3};

数组与指针

a[1]等价于*(a+1)

  1. std::cout << std::boolalpha << (myArray[0] == *myArray) << std::endl;
  2. std::cout << std::boolalpha << (myArray[1] == *(myArray + 1) << std::endl;
  3. // Outputs:
  4. // true
  5. // true

数组大小

  1. int myArray[5] = {1, 2, 3, 4, 5};
  2. size_t arraySize = sizeof(myArray) / sizeof(int);
  3. std::cout << "C-style array size: " << arraySize << std::endl;
  4. // Outputs:
  5. // C-style array size: 5
  1. #include <iostream>
  2. void printSize(int someArray[5]) {
  3. std::cout << sizeof(someArray)/sizeof(int) << std::endl;
  4. }
  5. int main() {
  6. int myArray[5] = {1, 2, 3, 4, 5};
  7. printSize(myArray);
  8. }
  9. // Outputs:
  10. // 2

第二个例子中, 函数没有返回正确的数组大小. 这是因为数组作为输入参数时, 传入的是一个size_t大小的指针, 在具体机器上大小可能为8字节, 而int类型大小是4个字节, 因为第二个例子相当于sizeof(size_t) / sizeof(int), 输出结果为2.

遍历循环

与上面一个小节类似, 在同一个作用范围内, 数组可以用C++11的遍历循环,

  1. int main() {
  2. int myArray[5] = {1, 2, 3, 4, 5};
  3. for (int &i : myArray) {
  4. std::cout << i << ", " << std::endl;
  5. }
  6. }

但是当数组被传入其他函数作为变量时, 遍历循环失效

  1. #include <iostream>
  2. void printElements(int someArray[5]) {
  3. for (int &i : someArray) {
  4. std::cout << i << ", " << std::endl;
  5. }
  6. }
  7. int main() {
  8. int myArray[5] = {1, 2, 3, 4, 5};
  9. printElements(myArray);
  10. }

原因还是一样, 被传入的是指针.

std::array

std::array是一个包装后的C数组, 在编译的时候必须确定数组大小.

声明数组

  1. #include <array>
  2. #include <iostream>
  3. void printElements(const std::array<int, 5> &someArray) {
  4. for (const int &i : someArray) {
  5. std::cout << i << ", " << std::endl;
  6. }
  7. std::cout << "Size: " << someArray.size() << std::endl;
  8. }
  9. int main() {
  10. std::array<int, 5> myArray = {1, 2, 3, 4, 5};
  11. printElements(myArray);
  12. }
  13. // Outputs:
  14. // 1,
  15. // 2,
  16. // 3,
  17. // 4,
  18. // 5,
  19. // Size: 5

std::vector

std::array是C数组的封装, std::vector则完全不同于原来的C数组, 是heap上的动态数组, 数组大小在编译的时候可以不确定.

std::array可以看成如此封装

  1. int a[5];

std::vector则是

  1. int *a = net int[5];

C++ Arrays, std::array, std::vector 总结的更多相关文章

  1. C++中的数组array和vector,lambda表达式,C字符串加操作,C++中新类型数组(数组缓存),多元数组,new缓冲

     使用C++风格的数组.不须要管理内存. array要注意不要溢出,由于它是栈上开辟内存. array适用于不论什么类型 #include<iostream> #include< ...

  2. std::array,std::vector,基于范围的for循环

    std::array除了有传统数组支持随机访问.效率高.存储大小固定等特点外,还支持迭代器访问.获取容量.获得原始指针等高级功能.而且它还不会退化成指针T *给开发人员造成困惑. for( 元素名变量 ...

  3. C++ std::array

    std::array template < class T, size_t N > class array; Code Example #include <iostream> ...

  4. 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++> 给出排序好的 ...

  5. C++语言中std::array的神奇用法总结,你需要知道!

    摘要:在这篇文章里,将从各个角度介绍下std::array的用法,希望能带来一些启发. td::array是在C++11标准中增加的STL容器,它的设计目的是提供与原生数组类似的功能与性能.也正因此, ...

  6. c++编译错误C2971:"std::array":array_size:包含非静态存储不能用作废类型参数;参见“std::array”的声明

    在Qt5中这段代码编写有两种方式:一个编译成功,一个失败 成功版本: static constexpr size_t block_size = 0x2000;//8KB static constexp ...

  7. std::array中的std::get<n>()

    模板函数std::get<n>()是一个辅助函数,它能够获取到容器的第 n 个元素.模板参数的实参必须是一个在编译时可以确定的常量表达式,编译时会对它检查. get<n>()模 ...

  8. 将std::array转换成std::tuple

    template<typename Array, std::size_t... Index> decltype(auto) array2tuple_impl(const Array& ...

  9. C++ std::array 基本用法

    #include <iostream> #include <string> #include <array> using namespace std; // htt ...

随机推荐

  1. Mybatis面试题合集及答案

    Mybatis面试题合集及答案 1.#{}和${}的区别是什么? 答:${}是Properties文件中的变量占位符,它可以用于标签属性值和sql内部,属于静态文本替换,比如${driver}会被静态 ...

  2. Cocos2d-X多线程(3) cocos2dx中的线程安全

    在使用多线程时,总会遇到线程安全的问题.cocos2dx 3.0系列中新加入了一个专门处理线程安全的函数performFunctionInCocosThread(),他是Scheduler类的一个成员 ...

  3. python学习之面向对象(三)

    6.8 类的结构细化 6.8.1 类的私有成员 类中的私有成员包括:私有类的属性,私有对象属性,私有类方法 私有静态属性 类的内部可以访问,类的外部不可以访问,派生类中不可以访问 class A: _ ...

  4. 【VS开发】ActiveX开发注意事项

    [VS开发]ActiveX开发注意事项 标签:[VS开发] 注意:必须在工程的app文件的InitInstance()中加入如下代码,否则动态创建控件不会成功: AfxEnableControlCon ...

  5. ios模拟器快捷键

    shift+cmd+h  返回桌面 cmd+5或者4或者3  可以直接调节大小 cmd+R运行项目 cmd+R弹出键盘 ios模拟器弹出键盘 在xcode6中, 模拟器中的键盘和电脑的键盘可以进行绑定 ...

  6. [转帖]linux学习问题总结

    linux学习问题总结 https://www.cnblogs.com/chenfangzhi/p/10661946.html 学习作者的思路 目录 一.环境变量和普通变量的区别 二.rsyslog和 ...

  7. mysql修改max_allowed_packet数据包最大值

    在windows环境下!!!! 1.找到my.inc文件,不是你的安装目录路径,是C:\ProgramData\MySQL\MySQL Server 5.7这个路径,注意 ProgramData 文件 ...

  8. 浅谈React工作原理

    浅谈React工作原理:https://www.cnblogs.com/yikuu/p/9660932.html 转自:https://cloud.tencent.com/info/63f656e0b ...

  9. idea工具

    1. 使用IntelliJ IDEA 配置JDK(入门)   https://blog.csdn.net/nobb111/article/details/77116259 2. idea 调试快捷键  ...

  10. <<C++ Primer>> 第四章 表达式

    术语表 第 4 章 表达式 算术转换(arithmetic conversion): 从一种算术类型转换成另一种算术类型.在二元运算符的上下文中,为了保留精度,算术转换通常把较小的类型转换成较大的类型 ...