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

南京网站设计开发平台信息发布

南京网站设计开发,平台信息发布,wordpress分页调用,辽宁省工程造价一、std::vector (1)vector与其适用场景 作用: std::vector 是 C 标准模板库(STL)中的动态数组,它的大小可以动态增长或缩小,并且提供了自动内存管理和丰富的操作函数。 特点: 自…

一、std::vector

(1)vector与其适用场景

作用:
std::vector 是 C++ 标准模板库(STL)中的动态数组,它的大小可以动态增长或缩小,并且提供了自动内存管理和丰富的操作函数

特点:

  • 自动管理内存(不需要手动 new/delete)。
  • 动态扩展(不像普通数组需要固定大小)。
  • 支持随机访问O(1) 复杂度)。
  • 丰富的内置函数(插入、删除、查找、排序等)。

适用场景:

  • 适用于需要动态增长的数组
  • 适用于需要快速访问(O(1))的情况
  • 适用于需要内存自动管理的情况

包含头文件

#include <vector>

(2)vector vs 数组

特性vector数组 (int arr[])
大小动态调整固定大小
内存管理自动手动管理
元素访问O(1)O(1)
安全性at() 提供边界检查访问越界可能崩溃
功能具备 insert, erase, sort 等功能需要手写

二、std::vector 的创建

函数作用
vector<T> vec;默认构造一个空vector
vector<T> vec(n);创建一个大小为nvector,默认初始化元素
vector<T> vec(n, val);创建一个大小为n,值为valvector
vector<T> vec = {val1, val2, ...};使用列表初始化
vector<T> vec(other_vec);拷贝构造
vector<T> vec(other_vec.begin(), other_vec.end());迭代器范围构造

(1) 定义 vector

示例:

#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>void printVector(const vector<int> v)
{for (int x : v){cout << x << " ";}cout << endl;
}int main() {vector<int> v1;              // 创建空 vectorvector<int> v2(5);           // 创建包含 5 个元素的 vector,默认值为 0vector<int> v3(5, 100);      // 创建 5 个 100vector<int> v4 = {1, 2, 3};  // 直接初始化vector<int> v5(v4);          // 复制另一个 vectorprintVector(v1);printVector(v2);printVector(v3);printVector(v4);printVector(v5);system("pause");return 0;
}

(2) 向 vector 添加元素

示例:

#include <iostream>
using namespace std;
#include <vector>int main() {vector<int> v;v.push_back(10);   // 添加元素v.push_back(20);v.emplace_back(30); // 效率更高for (int x : v){cout << x << " ";}cout << endl;system("pause");return 0;
}

注意:

  • push_back:先构造一个临时对象,然后拷贝/移动到容器中。
  • emplace_back直接在容器的内存空间中构造对象,避免临时对象的创建和拷贝/移动,提高效率

(3) 访问 vector 元素

函数作用
vec[i]索引访问,无越界检查
vec.at(i)索引访问,带越界检查(越界抛异常
vec.front()返回第一个元素
vec.back()返回最后一个元素
vec.data()返回指向底层数组的指针

示例:

#include <iostream>
using namespace std;
#include <vector>int main() {vector<int> v;v.push_back(10);   // 添加元素v.push_back(20);v.emplace_back(30); // 效率更高cout << v[0] << endl;       // 直接访问cout << v.at(1) << endl;    // 安全访问,带边界检查cout << v.front() << endl;  // 获取第一个元素cout << v.back() << endl;   // 获取最后一个元素system("pause");return 0;
}

三、遍历 vector

(1) 使用 for 循环

示例:

#include <iostream>
using namespace std;
#include <vector>int main() {vector<int> v = {10, 20, 30, 40, 50};  // 直接初始化for (int i = 0; i < v.size(); i++){cout << v[i] << " ";}cout << endl;system("pause");return 0;
}

(2) 使用范围 for

作用:

  • C++11 引入了范围基 for 循环(range-based for loop),用于简洁、直观地遍历容器(如 vector、list、map 等)。

语法:

for (元素类型 变量 : 容器) {// 操作变量
}

遍历方式:

  • 值遍历for (auto x : container) —— 拷贝元素(适用于基本类型)。
  • 引用遍历for (auto& x : container) —— 避免拷贝,提高效率。
  • 常引用遍历for (const auto& x : container) —— 保护数据,防止修改。

示例:

#include <iostream>
using namespace std;
#include <vector>int main() {vector<int> v = {10, 20, 30, 40, 50};  // 直接初始化for (int x : v) {cout << x << " ";}cout << endl;system("pause");return 0;
}

(3) 使用迭代器

函数作用
vec.begin()返回指向首元素的迭代器
vec.end()返回指向尾后元素的迭代器
vec.rbegin()返回指向末元素的反向迭代器
vec.rend()返回指向首元素前的反向迭代器

示例:

#include <iostream>
using namespace std;
#include <vector>int main() {vector<int> v = {10, 20, 30, 40, 50};  // 直接初始化for (auto it = v.begin(); it != v.end(); ++it){cout << *it << " ";}cout << endl;system("pause");return 0;
}

四、 std::vector 的常用操作

(1) 插入、删除

函数作用
vec.push_back(val)尾部添加元素
vec.emplace_back(args...)尾部原地构造元素(比push_back更高效)
vec.insert(pos, val)pos位置插入val
vec.insert(pos, n, val)pos位置插入nval
vec.insert(pos, first, last)pos位置插入迭代器范围的元素
vec.erase(pos)删除pos位置的元素
vec.erase(first, last)删除[first, last)范围内的元素
vec.pop_back()删除尾部元素
vec.clear()清空所有元素

示例:

#include <iostream>
using namespace std;
#include <vector>void printVector(vector<int> v)
{for (auto it = v.begin(); it != v.end(); ++it){cout << *it << " ";}cout << endl;
}int main() {vector<int> v = {1, 2, 3, 4};printVector(v);// 插入元素v.insert(v.begin() + 2, 100); // 在索引 2 位置插入 100, (1,2,100,3,4)printVector(v);// 删除元素v.erase(v.begin());           // 删除第一个元素 (2,100,3,4)printVector(v);v.erase(v.begin() + 2);       // 删除索引 2 处的元素 (2,100,4)printVector(v);// 清空 vectorv.clear();printVector(v);system("pause");return 0;
}

(2) vector 的大小

函数作用
vec.size()返回当前元素数量
vec.empty()判断是否为空
vec.capacity()返回当前分配的存储容量
vec.shrink_to_fit()减少capacity使其等于size
vec.reserve(n)预分配至少n个元素的空间
vec.resize(n)调整大小为n,新元素默认构造
vec.resize(n, val)调整大小为n,新元素初始化为val

示例:

#include <iostream>
using namespace std;
#include <vector>void printVector(vector<int> v)
{for (auto it = v.begin(); it != v.end(); ++it){cout << *it << " ";}cout << endl;
}int main() {vector<int> v = {1, 2, 3, 4};cout << v.size() << endl;     // 获取元素个数cout << v.capacity() << endl; // 获取当前容量cout << v.empty() << endl;    // 是否为空v.resize(10);      // 重新设定大小,默认填充 0printVector(v);v = {1, 2, 3, 4};v.resize(10, 5);   // 填充 5printVector(v);system("pause");return 0;
}

(3)vector 的排序

函数作用
vec.size()返回当前元素数量
sort(vec.begin(), vec.end())排序
reverse(vec.begin(), vec.end())逆序
find(vec.begin(), vec.end(), val)查找元素
binary_search(vec.begin(), vec.end(), val)二分查找(需排序)

示例:

#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>void printVector(vector<int> v)
{for (auto it = v.begin(); it != v.end(); ++it){cout << *it << " ";}cout << endl;
}int main() {vector<int> v = { 3, 1, 4, 5, 2 };printVector(v);sort(v.begin(), v.end());  // 升序排序printVector(v);reverse(v.begin(), v.end());  // 逆序printVector(v);system("pause");return 0;
}

(4) 交换 vector

示例:

#include <iostream>
using namespace std;
#include <vector>void printVector(vector<int> v)
{for (auto it = v.begin(); it != v.end(); ++it){cout << *it << " ";}cout << endl;
}int main() {vector<int> a = {1, 2, 3};vector<int> b = {4, 5, 6};cout << "交换前: " << endl;printVector(a);printVector(b);a.swap(b);  // 交换 a 和 b 的内容cout << "交换后: " << endl;printVector(a);printVector(b);system("pause");return 0;
}

注意:
a.swap(b); 交换 a 和 b 的所有元素,避免数据拷贝,提高性能。


文章转载自:

http://yjZgvL5u.xmyrn.cn
http://2zofwzpH.xmyrn.cn
http://2e926jYu.xmyrn.cn
http://MVHPxA69.xmyrn.cn
http://1HkxPd0S.xmyrn.cn
http://vIkJ4px2.xmyrn.cn
http://y4kGv0Ft.xmyrn.cn
http://YykkiFXM.xmyrn.cn
http://2K3snx55.xmyrn.cn
http://nSPQcbCt.xmyrn.cn
http://kaADVoCk.xmyrn.cn
http://C6lL776a.xmyrn.cn
http://lYeZ3l89.xmyrn.cn
http://BXAnODTn.xmyrn.cn
http://TVGBx6fb.xmyrn.cn
http://05Oo0r7h.xmyrn.cn
http://FVcjORcE.xmyrn.cn
http://DIUhh4Km.xmyrn.cn
http://GaNPq7ii.xmyrn.cn
http://zjQhd87K.xmyrn.cn
http://wegsxxpF.xmyrn.cn
http://L3Gc6OrH.xmyrn.cn
http://HIfizpPB.xmyrn.cn
http://RJdbajsh.xmyrn.cn
http://hHQ9WBMf.xmyrn.cn
http://9vhmZvgh.xmyrn.cn
http://0QLwSD5Q.xmyrn.cn
http://KVw7SrQv.xmyrn.cn
http://wf5Axbk5.xmyrn.cn
http://s5QErUvI.xmyrn.cn
http://www.dtcms.com/wzjs/608657.html

相关文章:

  • 个人网站公司网站区别经营区别wp如何做双语网站
  • 如何制作淘宝客网站杭州网站设计网站
  • 上海网站建设定制公司PHP网站开发工程师
  • 配资网站开发博兴县建设局网站
  • 公司建设门户网站的意义潘家园做网站的公司
  • 网站建设 wix为什么要用h5建站
  • 啊里云服务器怎么做网站开发公司和建材商促销活动
  • 安徽住房和城乡建设厅新网站php wordpress乱码
  • 网站建设项目总结报告百度竞价推广怎么收费
  • 济南网站建设优化熊掌号河北seo网站优化价格
  • 公司做网站文案怎么写网站开发的相关技能有哪些
  • 购买已备案网站做非法移动网站建设渠道
  • 有域名后如何建网站海口网站建设网站制作
  • 赤峰做企业网站公司上海中学门户网站登陆
  • 龙华公司网站建设做网站珊瑚橙颜色怎么搭配好看
  • 彩票网站建设开发网络管理员正在设计新的无布局
  • 做图片站 把图片放到其它网站可以吗汉字logo设计生成器
  • 网站建设方案说明网站建设文案怎么设计
  • 电子商务网站用什么语言开发企业网站的推广形式有哪些
  • 实力网站建设wordpress 伪静态分页
  • 遵义网站建设公司招聘wordpress怎么进行页面修改
  • 自助建站平台wordpress 主题 强大
  • 可以做哪方面的网站商城app搭建
  • 网站建设技术公司排名合肥手机网站制作建设
  • 512m内存做网站wordpress怎么加表格
  • 联合会网站建设崇明建设机械网站
  • 怎么样做好网站建设网站备案怎么找人备
  • 网站默认主页设置法国 wordpress
  • 公司品牌网站设计关于插画的网站
  • 电动车网站模板左侧菜单设置设置 wordpress