• C++ SLT中的容器学习与函数谓词



    java的集合框架 相当于 C++ STL(标准模板库) 在iostream库中
    在这里插入图片描述

    1、vector向量学习

    // C ++ vector 向量 容器 内部封装大小数组作为容器,能够存放任意动态数组(数据结构)

    #include 
    #include  // 引入 vector 容器的支持
    using namespace std;
    
    int main(){
    	//内定了模板函数(泛型)
    	vector<int> vector1;
    	//指定10的空间大小
    	vector<int> vector2(10;
    	//指定10的空间大小,每一个都是0
    	vector<int> vector3(100;
    	//插入数据
    	vector<int> vector4;
    	// vector.begin() 迭代器插入到前面
    	// vector.end() 迭代器插入到后面
    	vector4.insert(vector.begin()40);
    	vector4.insert(vector.begin()50);
    	vector4.insert(vector.begin()60);
    	cout << "vector4.front()" << vector4.front()<< endl; // 60
    	vector4.front() = 99;//修改第一个
    	//最后一个
    	vector4.back() = 777
    	//变成 99 50 777
    	//移除 第一个元素
    	vector4.erase(vector4.begin());
    	// 50 777	
    	//循环打印 默认 "从大到小" 输出
    	for(int i = 0; i < vector4.size(); i++) {
    		//上面第一个60被移除了,最后一个40被修改成777.输出50、777
    		//vector4[i]中的[]是运算符重载,跟数组的[]不一样
    		cout << "item: " << vector4[i] << endl;
    	}
    	// 迭代器 循环遍历
    	for(vector<int>::iterator iteratorVar = vector4.begin(); 
    	iteratorVar != vector4.end(); iteratorVar++){
    		// 输出50、777
    		cout << "item: " << vector4[i] << endl;
    	}
    	// auto kotlin 自带类型推导
    	for(auto iteratorVar = vector4.begin(); 
    	iteratorVar != vector4.end(); iteratorVar++){
    		// 输出50、777
    		cout << "item: " << vector4[i] << endl;
    	}
    	// NDK开发中一定要用容器,应该用queue 队列,这个几乎用不到。
    return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    2、stack栈学习

    int main(){
    	stack<int> s;
    	s.push(1);
    	s.push(2);
    	s.push(3);
    	for(int i = 0; i < s.size(); ++i) {
    		//报错 stack内部没有重载[],也没有支持迭代器stack :: iterator
    		cout << s[i] << endl;
    	}
    	//循环把元素清空
    	while(!s.empty) {
    	int top = s.top();//拿第一个元素
    	s.pop;//把元素弹出去
    	}
    	// NDK 开发中,几乎用不到!
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    3、queue队列学习

    先进先出,用管道来理解是最容易的

    #include 
    #include  // 队列支持(内部:基本上 链表、数组)
    using namespace std;
    
    int main(){
    	queue<int> queueVar;
    	queueVar.push(20);
    	queueVar.push(40);
    	queueVar.push(60);
    	//第一个元素是20
    	cout << "queueVar.front()" << queueVar.front() << endl;
    	queueVar.front()  = 88;
    	//最后一个元素是60
    	cout << "queueVar.back()" << queueVar.back() << endl;
    	queueVar.front()  = 88;
    	// 跟栈stack一样,只能把前面的元素弹出去
    	while(!queueVar.empty) {
    	int top = queueVar.front();//拿第一个元素
    	cout << top << endl; //88 40 88
    	queueVar.pop;//把元素弹出去
    	}
    
    return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    4、优先队列学习

    #include 
    #include  // queue的子集
    using namespace std;
    // queue 单端队列  Duque 双端
    //priority_queue 内部对vector 有一定的封装
    int main(){
    	priority_queue<int> priorityQueue;
    	//隐式代码 vector, less 你看不到
    	//less将当前和上一个元素比大小,从大到小 ,greater 反过来,会输出 10 20 30 ...
    	//vector,内部需要,你就给它。
    	//priority_queue, less> priorityQueue;
    	priorityQueue.push(10);
    	priorityQueue.push(20);
    	priorityQueue.push(30);
    	priorityQueue.push(40);
    	priorityQueue.push(50);
    	priorityQueue.push(60);
    	//第一个元素是 60
    	cout << "priorityQueue.top()" << priorityQueue.top() << endl;
    	
    	// 循环遍历
    	while(!priorityQueue.empty) {
    	int top = priorityQueue.top();//拿第一个元素
    	cout << top << endl; // 60 50 40 30 20 10
    	queueVar.pop;//把最前面元素消费掉
    	}
    
    return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    5、list学习

    javja:ArrayList采用Object[]数组, C++的list 内部:采用链表

    #include 
    #include 
    using namespace std;
    
    int main(){
    	list<int> listVar;
    	//插入操作 到前面
    	listVar.push_front(50);
    	//后面
    	listVar.push_back(60);
    	//插入到什么位置,也是签名,跟push_front一样,相对灵活
    	listVar.insert(listVar.begin(), 70);
    	listVar.insert(listVar.end(), 80);
    	//遍历,不能通过角标访问
    	for(list<int> :: iterator it = listVar.begin(); it != listVar.end();it ++{
    	cout << *it << ednl; // 70 50 60 80
    	}
    	//修改
    	listVar.back() = 88;
    	//删除最前面
    	listVar.erase(listVar.begin())// NDK开发中使用较少
    
    return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    6、set来引出函数谓词

    #include 
    #include  // 内部实现:红黑树结构,会对你存入的数据进行排序,但是绝对不允许元素相同
    using namespace std;
    
    class Person{
    public:
    	string name;
    	int id;
    	Person(string name, int id): name(name),id(id){}
    };
    // C++ 默认的这个源码没有对象比较的功能,其实这点跟java也差不读
    // bool operator()(const _Tp& __x, const _Tp& __y) const{return __x < __y;}
    // 系统源码谓词做不到比较对象,自定义这个功能【自定义谓词】
    bool doCompareAction(const Person & person1, const Person & person2){
    	return person1.id < person2.id;
    }
    
    // 真正的谓词,仿照系统源码写
    struct doCompareAction2{
    public:
    	bool operator() (const Person & person1, const Person & person2){
    		return person1.id < person2.id;
    	}
    };
    int main(){
    	set<int, less<int>> setVr; // __x < __y 从小到大,默认是less
    	//添加参数,不需要用迭代器,也不需要指定位置
    	setVar.insert(1);
    	setVar.insert(2);
    	setVar.insert(3);
    	setVar.insert(4);
    	//重复插入不会报错 std::pair
    	pair<set<int,less<int>>::iterator,bool> = setVar.insert(1);
    	//res.first 获取第一个元素  迭代器
    	//res.second 获取第二个元素  bool
    	bool insert_success = res.second;
    	if(insert_success) {
    	cout << "成功" << endl;
    	} else {
    	cout << "失败" << endl; // 输出失败
    	}
    	//全部遍历
    	for(auto it = setVar.begin(); it != setVar.end(); it++) {}
    
    	// 对象的排序
    	//set sp; 没有对象比对功能会报错
    	//set sp; 报错,山寨版,达不到谓词的标准
    	set<Person, doCompareAction2> sp;
    	// 构建对象
    	Person p1("sanke",1);
    	Person p1("kevin",2);
    	Person p1("marry",3);
    	// 放到set容器
    	setVar.insert(p1);
    	setVar.insert(p2);
    	setVar.insert(p3);
    	//name string 000 c_str() --- char *    
    	for(set<Person>::iterator it = setVar.begin(); it !+ setVar.end();it++){
    		cout << it->name.c_str() << "," << it->id << endl; 
    	}
    return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    7、map容器

    #include 
    #include  
    using namespace std;
    
    int main(){
    	// map 会对key进行排序 key不能重复
    	map<int ,string> mapVar;
    	//添加数据方式1
    	mapVar.insert(pair<int ,string>(1, "一"));
    	//第二种
    	mapVar.insert(make_pair(2, "二"));
    	//第三种
    	mapVar.insert(map<int ,string>::value_type(3, "三"));
    	// 上面三种方式key不能重复,跟java一样,会插入失败 ,无法覆盖。
    	//第四种   mapVar[key] = value 会覆盖
    	mapVar[4] = "四";
    	mapVar[4] = "四四"; 
    	//循环打印 迭代器
    	for(map<int, string>:: iterator it = mapVar.begin(); it != mapVar.end(); it++){
    		cout << it->first << "," << it->second.c_str() << endl;
    	}
    	pair<map<int, string>::iterator, bool> result 
    	= mapVar.insert(map<int ,string>::value_type(3, "三san"));
    	//判断是否成功和成功之后的数据
    	if(result.second) {
    		cout << "成功" << endl;
    		for(result.first == mapVar.begin(); result.first != mapVar.end;
    		result.first++){
    			cout << result.first->first << "," << result.first->second << endl;
    		}
    	}
    
    	// 查找
    	map<int, string> :: iterator findResult = mapVar.find(3);
    	if (findResult != mapVar.end()) {
    		cout << "找到了" << endl;
    	}
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    8、 multimap容器

    #include 
    #include  
    using namespace std;
    
    int main(){
    	// 1、key 可以重复 2key重复的数据可以分组
    	multimap<int, string> multimapVar;
    	multimapVar.insert(make_pair(10, "十个1"))
    	multimapVar.insert(make_pair(10, "十个2"))
    	multimapVar.insert(make_pair(10, "十个3"))
    	
    	multimapVar.insert(make_pair(30, "三十个1"))
    	multimapVar.insert(make_pair(30, "三十个2"))
    	multimapVar.insert(make_pair(130, "三十个3"))
    	
    	multimapVar.insert(make_pair(10, "二十个1"))
    	multimapVar.insert(make_pair(10, "二十个2"))
    	multimapVar.insert(make_pair(10, "二十个3"))
    
    	for(multimap<int, string> :: iterator iteratorVar = multimap.begin();
    	iteratorVar != multimap.end(); iteratorVar ++) {
    		cout << iteratorVar->first << "," << iteratorVar->second << endl;
    		//输出顺序按照key排序 10 10 10 20 20 20 30 30 30 value不会排序
    	}
    
    	//核心功能是分组
    	int result;
    	cout << "请输入要查询的key 为int类型" << endl;
    	cin  >> result;
    	multimap<int, string>::iterator it = multimapVar.find(result);
    	//如果输入10 将输出三个 key是10的元素
    	while(it != multimapVar.end()) {
    		cout << it->first << "," << it->second << endl;
    		//需要自己做逻辑,不然又问题。
    		it ++ ;
    		if(it->first != result) {
    			break;
    		}
    		// 严谨些
    		if(it == multimapVar.end()){
    			break;
    		}
    	}
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
  • 相关阅读:
    Go基础12-理解Go语言表达式的求值顺序
    剑指 Offer 07. 重建二叉树
    IIFE立即执行函数表达式使用
    1099:第n小的质数(信奥)
    从智能锁谈STM32安全技术
    论文阅读之Learning and Generalization of Motor Skills by Learning from Demonstration
    ssm+vue+elementUI 电子资源管理系统-#毕业设计
    2023-06-10 Untiy进阶 C#知识补充1——.Net介绍
    我的居家生活--爱摸鱼的美工
    JMH – Java基准测试
  • 原文地址:https://blog.csdn.net/qq_34606099/article/details/126288374