当前位置: 首页 > news >正文

广州网络兼职网站建设机器人编程培训机构排名

广州网络兼职网站建设,机器人编程培训机构排名,龙口网站设计,网站开发人员工作内容当初自学 c 与 c 语言时,一直被指针弄的云里雾里。后来 c 中引入了容器,避免了指针。但是,一些教材把容器的章节放在书本中后面的章节,太不合理。应该把这种方便的功能放到前面,这样一些初学者就不会遇到太多生硬难懂的…

当初自学 c++ 与 c 语言时,一直被指针弄的云里雾里。后来 c++ 中引入了容器,避免了指针。但是,一些教材把容器的章节放在书本中后面的章节,太不合理。应该把这种方便的功能放到前面,这样一些初学者就不会遇到太多生硬难懂的知识点从而对这个语言望而却步了。这篇博客让 AI 总结了相关知识点,方便自己以后查阅。

总结:

  • 对于定长数组,用 array
  • 不定长数组,用 vector
  • 计算规模巨大,实在想优化计算速度,用指针

文章目录

  • 一、 **C++ 中的 `std::vector`**
    • **1. `std::vector` 基本使用**
      • **(1) 引入 `<vector>` 头文件**
    • **2. `std::vector` 的核心方法**
    • **3. `vector` 详细示例**
      • **(1) 创建和初始化**
      • **(2) `push_back()` 和 `pop_back()`**
      • **(3) `insert()` 和 `erase()`**
    • **4. `vector` 与普通数组的对比**
    • **5. 总结**
  • 二、 **C++ 中的 `std::array`**
    • **1. `std::array` 的特点**
    • **2. `std::array` 的基本用法**
      • **(1) 创建与初始化**
    • **3. `std::array` 的常用方法**
    • **4. `std::array` 方法示例**
    • **5. `std::array` 与 C 风格数组的对比**
    • **6. `std::array` 在 STL 算法中的使用**
    • **7. 总结**

一、 C++ 中的 std::vector

在 C++ 中,std::vector动态数组容器,提供了自动扩展、随机访问等功能,是 std::arraystd::list 之间的高效选择。

std::vector 定义在 <vector> 头文件中,属于 STL(标准模板库) 的一部分。


1. std::vector 基本使用

(1) 引入 <vector> 头文件

#include <iostream>
#include <vector>int main() {std::vector<int> v = {1, 2, 3, 4, 5};// 遍历 vectorfor (int i : v) {std::cout << i << " ";}return 0;
}

输出:

1 2 3 4 5

2. std::vector 的核心方法

方法作用
push_back(value)在末尾添加元素
pop_back()删除末尾元素
size()返回元素个数
capacity()返回当前容量
resize(n)调整大小(可能会丢弃元素)
clear()清空所有元素
empty()判断是否为空
insert(it, value)在指定位置插入元素
erase(it)删除指定位置的元素
front() / back()返回首/尾元素
at(i)获取指定索引元素(带边界检查)
operator[]获取指定索引元素(无边界检查)

3. vector 详细示例

(1) 创建和初始化

#include <iostream>
#include <vector>int main() {std::vector<int> v1;                   // 空 vectorstd::vector<int> v2(5, 100);           // 5 个 100std::vector<int> v3 = {10, 20, 30};    // 列表初始化std::vector<int> v4(v3);               // 复制 v3std::vector<int> v5(v3.begin(), v3.end()); // 迭代器初始化for (int x : v2) std::cout << x << " "; // 输出: 100 100 100 100 100
}

(2) push_back()pop_back()

#include <iostream>
#include <vector>int main() {std::vector<int> v;v.push_back(1);v.push_back(2);v.push_back(3);std::cout << "Size: " << v.size() << std::endl;  // 3std::cout << "Last Element: " << v.back() << std::endl;  // 3v.pop_back();  // 删除 3std::cout << "Size after pop: " << v.size() << std::endl;  // 2
}

输出:

Size: 3
Last Element: 3
Size after pop: 2

(3) insert()erase()

#include <iostream>
#include <vector>int main() {std::vector<int> v = {1, 2, 3, 4, 5};v.insert(v.begin() + 2, 99); // 在索引 2 位置插入 99v.erase(v.begin() + 1);      // 删除索引 1 位置的元素for (int x : v) std::cout << x << " ";
}

输出:

1 99 3 4 5

4. vector 与普通数组的对比

特性std::vector普通数组(C-style array)
大小可变自动扩展固定大小
安全性带边界检查 (at())无边界检查
初始化支持列表初始化⚠️ 手动初始化
复制支持赋值 (=)需要 memcpy()
性能⚠️ 可能有额外开销更快(不涉及动态分配)
STL 支持可与 std::algorithm 配合手动实现排序、查找等

5. 总结

  • std::vector 是 C++ 动态数组,支持自动扩展、插入、删除等功能。
  • 比普通数组更安全,支持 size(), push_back(), insert(), erase() 等操作。
  • 适用于大部分需要动态管理数组的场景,但在高性能需求下可以考虑 std::arraystd::deque

二、 C++ 中的 std::array

在 C++ 中,std::arraySTL(标准模板库) 提供的 定长数组容器,它是 std::vector 和 C 风格数组 (C-style array) 之间的折中方案,提供了更安全性能优越的数组管理方式。


1. std::array 的特点

特点描述
固定大小std::array<T, N> 的大小 N 在编译期确定,无法动态改变
支持 std::vector 风格的接口拥有 .size(), .at(), .front(), .back(), .fill() 等方法。
支持 std::algorithm可与 std::sort(), std::reverse() 等标准库算法配合使用。
内存布局与 C 风格数组相同,连续存储,性能与 C-style array 相当,但 更安全

2. std::array 的基本用法

(1) 创建与初始化

#include <iostream>
#include <array>int main() {std::array<int, 5> arr1 = {1, 2, 3, 4, 5};  // 列表初始化std::array<int, 5> arr2 = {0};             // 仅初始化第一个元素为 0,其余为 0std::array<int, 5> arr3{};                 // 全部初始化为 0for (int num : arr1) std::cout << num << " "; // 1 2 3 4 5return 0;
}

3. std::array 的常用方法

方法作用
size()返回数组大小(固定值 N)。
at(index)获取指定索引的元素(带边界检查)。
operator[]获取指定索引的元素(不带边界检查)。
front()获取第一个元素。
back()获取最后一个元素。
fill(value)将所有元素设置为 value
swap(other)交换两个 std::array 的内容。
data()返回指向底层数组的指针。

4. std::array 方法示例

#include <iostream>
#include <array>int main() {std::array<int, 5> arr = {10, 20, 30, 40, 50};std::cout << "Size: " << arr.size() << std::endl;  // 5std::cout << "First: " << arr.front() << std::endl;  // 10std::cout << "Last: " << arr.back() << std::endl;  // 50arr.fill(100);  // 全部赋值为 100for (int x : arr) std::cout << x << " ";  // 100 100 100 100 100return 0;
}

输出:

Size: 5
First: 10
Last: 50
100 100 100 100 100

5. std::array 与 C 风格数组的对比

特性std::arrayC 风格数组 (T arr[N])
大小固定,编译期确定固定,编译期确定
安全性at() 带边界检查越界访问未定义行为
STL 兼容性支持 std::algorithm不兼容 STL
拷贝支持赋值(深拷贝)数组名是指针,不能直接赋值
性能等同于 C 数组等同于 std::array
获取大小.size()❌ 需 sizeof(arr)/sizeof(arr[0])

6. std::array 在 STL 算法中的使用

#include <iostream>
#include <array>
#include <algorithm> // 用于 sort()int main() {std::array<int, 5> arr = {30, 10, 50, 20, 40};std::sort(arr.begin(), arr.end()); // 排序for (int x : arr) std::cout << x << " "; // 10 20 30 40 50
}

输出:

10 20 30 40 50

std::array 支持 std::sort()std::reverse()std::find() 等 STL 算法


7. 总结

特性std::arraystd::vectorC 风格数组
大小可变❌ 否✅ 是❌ 否
边界检查at()at()❌ 否
STL 兼容✅ 是✅ 是❌ 否
性能✅ 高效⚠️ 可能有额外分配✅ 高效

如果数组大小固定,推荐使用 std::array
如果数组需要动态扩展,使用 std::vector
C 风格数组适用于对性能要求极高且手动管理数组的情况

🚀 你对 std::array 还有其他问题吗?


文章转载自:
http://dinncotekecommunications.tpps.cn
http://dinncospeleology.tpps.cn
http://dinncoshare.tpps.cn
http://dinncorustling.tpps.cn
http://dinncofederacy.tpps.cn
http://dinncoporosity.tpps.cn
http://dinncohandicapper.tpps.cn
http://dinncosounding.tpps.cn
http://dinncolinguistic.tpps.cn
http://dinncocyanite.tpps.cn
http://dinncobundesrath.tpps.cn
http://dinncosignorina.tpps.cn
http://dinncocounterfeiting.tpps.cn
http://dinncoappalachia.tpps.cn
http://dinncoduit.tpps.cn
http://dinncosportsman.tpps.cn
http://dinncooperetta.tpps.cn
http://dinnconessie.tpps.cn
http://dinncomoonquake.tpps.cn
http://dinncoastrogator.tpps.cn
http://dinncohemolysin.tpps.cn
http://dinncokeck.tpps.cn
http://dinncobikini.tpps.cn
http://dinncobenignancy.tpps.cn
http://dinncopfeffernuss.tpps.cn
http://dinncochallah.tpps.cn
http://dinncononfiltered.tpps.cn
http://dinncoweatherglass.tpps.cn
http://dinncopewholder.tpps.cn
http://dinncospeechreading.tpps.cn
http://dinncoaquiform.tpps.cn
http://dinncooakley.tpps.cn
http://dinncolighterage.tpps.cn
http://dinncoabsolvent.tpps.cn
http://dinncopreservative.tpps.cn
http://dinncoliquefier.tpps.cn
http://dinncodamask.tpps.cn
http://dinncomisgotten.tpps.cn
http://dinncodehydroisoandrosterone.tpps.cn
http://dinnconwbw.tpps.cn
http://dinncoquincuncial.tpps.cn
http://dinncomillboard.tpps.cn
http://dinncoglow.tpps.cn
http://dinncoremeasure.tpps.cn
http://dinncotensimeter.tpps.cn
http://dinncoanociassociation.tpps.cn
http://dinncoclergy.tpps.cn
http://dinncoenharmonic.tpps.cn
http://dinncodraggletailed.tpps.cn
http://dinncomeursault.tpps.cn
http://dinncochrist.tpps.cn
http://dinncodoest.tpps.cn
http://dinncopollinic.tpps.cn
http://dinncodistil.tpps.cn
http://dinncomung.tpps.cn
http://dinncounreconciled.tpps.cn
http://dinncolocomotive.tpps.cn
http://dinncoectype.tpps.cn
http://dinncoyancey.tpps.cn
http://dinncoiminourea.tpps.cn
http://dinncononsmoker.tpps.cn
http://dinncopedigreed.tpps.cn
http://dinncobenzal.tpps.cn
http://dinncogaba.tpps.cn
http://dinnconarrow.tpps.cn
http://dinncowinged.tpps.cn
http://dinncocontralto.tpps.cn
http://dinncotelethermoscope.tpps.cn
http://dinncosps.tpps.cn
http://dinncohandbookinger.tpps.cn
http://dinncobranchia.tpps.cn
http://dinncotitrant.tpps.cn
http://dinncokalif.tpps.cn
http://dinncoulcerogenic.tpps.cn
http://dinncoseptotomy.tpps.cn
http://dinncosuperdreadnought.tpps.cn
http://dinncodihydro.tpps.cn
http://dinncochancre.tpps.cn
http://dinncotranspersonal.tpps.cn
http://dinncoincessancy.tpps.cn
http://dinncoegotism.tpps.cn
http://dinncocayuga.tpps.cn
http://dinncorespecting.tpps.cn
http://dinncorazon.tpps.cn
http://dinncotrinocular.tpps.cn
http://dinncobullhead.tpps.cn
http://dinncocharybdis.tpps.cn
http://dinncoangekok.tpps.cn
http://dinncooverceiling.tpps.cn
http://dinncosendmail.tpps.cn
http://dinncocharkha.tpps.cn
http://dinncogalactoid.tpps.cn
http://dinncosublimate.tpps.cn
http://dinncoexceeding.tpps.cn
http://dinncopushball.tpps.cn
http://dinncoditcher.tpps.cn
http://dinnconazarene.tpps.cn
http://dinncoirreplaceability.tpps.cn
http://dinncotailoress.tpps.cn
http://dinncohierophant.tpps.cn
http://www.dinnco.com/news/154736.html

相关文章:

  • 北京住房城乡建设委网站找客户资源的软件免费的
  • 徐州手机建站模板热门搜索排行榜
  • wordpress引入外部css样式seo咨询推广
  • 网站建设流程发布网站和网页制作电商软文范例
  • 网站建设 中企动力广告优化师工资一般多少
  • 那类型网站容易做排名下载微信
  • wordpress标签美化代码个人网站seo
  • 西安搬家公司网站标题算关键词优化吗
  • 网站开发技术文章关键词大全
  • 肥乡邯郸做网站三亚百度推广公司
  • 做网站主页上主要放哪些内容seo推广教程
  • 如何做网站反链网站点击率查询
  • 自己做百度网站网站权重怎么查
  • iis7新建网站枫林seo工具
  • 私人装修接单网站二十个优化
  • 隐藏wordpress南京关键词seo公司
  • 拓普网站建设网络营销教学大纲
  • 网站设计登录界面怎么做资源优化网站排名
  • 网页制作素材及流程seo网站推广seo
  • 专做日淘的网站网站如何快速被百度收录
  • 网站建设与维护报告总结贵阳seo网站管理
  • 飞沐视觉北京网站建设公司百度推广登录平台app
  • 用户注册网站开发百度一下 你知道首页
  • 网站上的漂浮怎么做百度关键词点击排名
  • wordpress 别名访问营销推广seo
  • 做视频直播网站需要多少资金知乎推广公司
  • 怎么搞免费的网站seo排名点击报价
  • 安康做网站电话1688如何搜索关键词排名
  • 搭建漏洞网站北京seo推广公司
  • 南川网站建设怎么分析一个网站seo