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

C++(4)

四、模板与容器

1. 模板

1.1 函数模板

#include <iostream>
using namespace std;// 函数模板声明
template<typename T>  // 也可使用 class
T add(T a, T b) {return a + b;
}int main() {string a = "hello";string b = "world";cout << add(a, b) << endl;    // 输出 hello worldcout << add(1, 2) << endl;    // 输出 3cout << add(1.3, 2.2) << endl;// 输出 3.5return 0;
}

1.2 类模板(类内声明类外定义)

#include <iostream>
using namespace std;// 类模板声明
template<class T>
class Test {
private:T val;
public:Test(T val);                // 构造函数声明T get_val();                // 获取值声明void set_val(const T &val); // 设置值声明
};// 类模板成员函数定义
template<class T>
Test<T>::Test(T val) : val(val) {}template<class T>
T Test<T>::get_val() {return val;
}template<class T>
void Test<T>::set_val(const T &val) {this->val = val;
}int main() {Test<int> t1(20);cout << t1.get_val() << endl;t1.set_val(10);cout << t1.get_val() << endl;Test<double> t2(20.3);cout << t2.get_val() << endl;return 0;
}

2. 容器

2.1 标准模板库(STL)

STL包含三大组件:

  • ​算法​​(algorithm)
  • ​容器​​(container)
  • ​迭代器​​(iterator)

2.2 顺序容器

2.2.1 array数组
#include <array>
#include <iostream>
using namespace std;int main() {array<int, 5> arr = {1, 2, 3}; // 后两位自动初始化为0// 访问元素cout << arr[0] << endl;    // 1cout << arr.at(4) << endl; // 0(推荐使用at())// 修改元素arr[3] = 200;// 遍历方式for(int i = 0; i < arr.size(); i++) {cout << arr[i] << endl;}for(auto num : arr) {cout << num << endl;}return 0;
}
2.2.2 vector向量
#include <vector>
#include <iostream>
using namespace std;int main() {vector<int> vec(5); // 创建长度为5的int容器// 添加元素vec.push_back(222);vec.insert(vec.begin()+2, 333);// 修改元素vec[1] = 666;vec.at(3) = 777;// 删除元素vec.pop_back();          // 删除末尾vec.erase(vec.begin()+1);// 删除指定位置// 遍历方式for(int i = 0; i < vec.size(); i++) {cout << vec[i] << endl;}for(auto num : vec) {cout << num << " ";}return 0;
}
2.2.3 list列表
#include <list>
#include <string>
#include <iostream>
using namespace std;int main() {list<string> lis(5, "hello"); // 初始化5个"hello"// 添加元素lis.push_back("world");lis.push_front("haha");lis.insert(++lis.begin(), "222");// 删除元素lis.pop_front();auto it = lis.begin();advance(it, 1);lis.insert(it, "333");lis.erase(--lis.end());// 修改元素it = lis.end();advance(it, 2);*it = "200";// 遍历输出for(string s : lis) {cout<< s << endl;}return 0;
}
2.2.4 deque队列
#include <deque>
#include <iostream>
using namespace std;int main() {deque<int> deq(5);// 添加元素deq.push_back(222);deq.insert(deq.begin()+2, 333);// 修改元素deq[1] = 666;deq.at(3) = 777;// 删除元素deq.pop_back();deq.erase(deq.begin()+1);// 遍历输出for(int i = 0; i < deq.size(); i++) {cout << deq[i] << endl;}return 0;
}

2.3 关联容器

2.3.1 map键值对
#include <map>
#include <string>
#include <iostream>
using namespace std;int main() {map<string, int> ma;// 添加元素ma["身高"] = 180;ma.insert(make_pair("年龄", 24));// 修改元素ma["身高"] = 175;// 查找元素if(ma.find("体重") == ma.end()) {cout << "没有体重数据" << endl;}// 删除元素ma.erase("年龄");// 遍历输出for(auto& pair : ma) {cout << pair.first << ": " << pair.second << endl;}return 0;
}

2.4 迭代器遍历

#include <vector>
#include <array>
#include <list>
#include <deque>
#include <map>
#include <iostream>
using namespace std;int main() {// 遍历stringstring s = "abcdefg";for(auto it = s.cbegin(); it != s.cend(); ++it) {cout << *it << " ";}cout << endl;// 遍历arrayarray<int, 5> arr = {1, 2, 3, 4, 5};for(auto it = arr.cbegin(); it != arr.cend(); ++it) {cout << *it << " ";}cout << endl;// 遍历mapmap<string, int> ma = {{"A", 1}, {"B", 2}};for(auto it = ma.cbegin(); it != ma.cend(); ++it) {cout << it->first << ":" << it->second << endl;}return 0;
}

http://www.lryc.cn/news/2387091.html

相关文章:

  • 构建版本没mac上传APP方法
  • 如何解决大模型返回的JSON数据前后加上```的情况
  • 本地处理 + GPU 加速 模糊视频秒变 4K/8K 修复视频老旧素材
  • 服务器异常数据问题解决 工具(tcpdump+wireshark+iptables)
  • 综合实现案例 LVS keepalived mysql 等
  • 【QT】对话框dialog类封装
  • 2025/5/26 学习日记 基本/扩展正则表达式 linux三剑客之grep
  • 【后端高阶面经:消息队列篇】29、Kafka高性能探秘:零拷贝、顺序写与分区并发实战
  • Spring Boot企业级开发五大核心功能与高级扩展实战
  • 在SpringBoot项目中策略模式的使用
  • 在 Docker 中启动 Jupyter Notebook
  • IP 地址反向解析(IP反查域名)原理与应用
  • CodeTop之LRU缓存
  • uboot常用命令之eMMC/SD卡命令
  • 【Kafka】编写消费者开发模式时遇到‘未解析的引用‘SIGUSR1’’
  • DeepSeek 赋能教育游戏化:AI 重构学习体验的技术密码
  • Docker run命令-p参数详解
  • 知识宇宙-学习篇:学编程为什么从C语言开始学起?
  • Mybatis-入门程序、 数据库连接池、XML映射配置文件、MybatisX
  • 互联网大厂Java求职面试:Spring Cloud微服务架构设计中的挑战与解决方案
  • BUUCTF [ZJCTF 2019]EasyHeap
  • 机器学习AI精准预测复合材料性能、材料结构设计优化;数据驱动加速新材料研发,百年难遇的组合打破科研壁垒!
  • apache http client连接池实现原理
  • 如何做好一份网络安全技术文档?
  • Android Studio 介绍
  • MD5加密(Java)
  • [攻防世界] easyphp writeup
  • 力扣热题100之LRU缓存机制
  • 如何不规范的设置密码
  • 数据安全与纵深访问控制:构建数字时代的安全防线