
c++泛型编程
kpler
嵌入式软件
展开
-
内建函数对象和常用算法
1,stl函数对象的分类a,算数仿函数b,关系仿函数c,逻辑仿函数这些函数与一般函数相同,使用内建函数对象,需要引入头文件#include2,算术仿函数这里举两个例子plus和negate#include<functional>#include<iostream>using namespace std;int main(){ negate<int> p;//取反 cout << p(50) << endl; plus&原创 2022-05-31 19:15:03 · 255 阅读 · 0 评论 -
STL函数对象和谓词
函数对象1,重载函数调用操作符的类,其对象常称为函数对象2,仿函数,函数对象使用重载的()时,行为类似函数调用,也叫仿函数注意:函数对象不是一个函数而是一个类。函数对象的使用a,函数对象在使用时,可以像普通函数那样调用,可以有参数可以有返回值b,函数对象超出普通函数的概念,函数对象可以有自己的状态c,函数对象可以作为参数传递#include<iostream>using namespace std;class myadd{public: int operator()原创 2022-05-24 19:32:23 · 159 阅读 · 0 评论 -
STL模板set容器和map容器
set容器特征set/multiset属于关联式容器,底层是一棵二叉树。set不容许容器中重复的元素,multiset允许有重复的元素pair对组创建功能描述:成对出现的数据,利用对组可以返回两个数据。两种创建方式pair<type,type>p<val1,val2>;pair<type,type>p=make_pair(val1,val2);#include<set>#include<iostream>using name原创 2022-05-20 19:08:40 · 201 阅读 · 0 评论 -
STL模板栈、队列
栈模板stack()和队列模板queue()#include<stack>#include<iostream>#include<queue>#include<string>//先进后出的规律using namespace std;class person{public: string m_name; int m_age; person(string name,int age) { this->m_age = age;原创 2022-05-18 19:31:00 · 194 阅读 · 0 评论 -
deque容器
deque容器是一个双端数组,可以对头端进行插入和删除操作#include<vector>#include<deque>#include<iostream>using namespace std;void printDeque(deque<int> &Deque){ for (deque<int>::const_iterator it = Deque.begin(); it != Deque.end(); it++) {原创 2022-05-17 19:04:19 · 134 阅读 · 0 评论 -
stl容器string类
1,string类概述string本身就是一个类,类内部封装了char*,管理这个字符串是个char*的容器。string类内部封装了许多的成员方法。2,string类的方法#include<string>#include<iostream>#include<stdio.h>using namespace std;int main(){ string str = "NULL";//无参构造 const char* str2 = "have pare原创 2022-05-13 19:42:07 · 121 阅读 · 0 评论 -
stl容器vector和list容器
stl容器vector可以把vector理解为一个可以动态变化的数组。#include<iostream>#include<vector>#include<algorithm>using namespace std;void myprint(int val){ cout << val << endl;}void test(){ vector<int> v;//实例化一个vector容器 v.push_bac原创 2022-04-21 14:59:10 · 110 阅读 · 0 评论 -
C++泛型编程(模板)
c++主要提供两种模板函数模板和类模板1,函数模板的作用:建立一个通用函数,其函数返回值类型和参数类型可以不具体制定,用一个虚拟的类型来代表。template:关键字template表明是声明模板。typename可以用class代替,cal名称可以替换叫做通用数据类型。#include<iostream>using namespace std;template<typename cal>void swap(int &a,int &b){ int原创 2022-02-28 16:35:12 · 525 阅读 · 0 评论