STLNormalFunc
- #include <iostream>
- #include <vector>
- using namespace std;
- void main_1()
- {
- vector<int> vecIntA;//默认构造函数
- vector<int> vecIntB(vecIntA);//调用拷贝构造函数
- int iSize=vecIntA.size();//放回容器中元素的个数
- bool isEmpty=vecIntA.empty();//判断容器是否为空
- //4.比较操作
- bool isEqual= (vecIntB == vecIntA);
- bool isNotEqual= (vecIntB != vecIntA);
- cout<< iSize <<endl;
- cout<< isEmpty <<endl;
- cout<< isEqual <<endl;
- cout<< isNotEqual <<endl;
- vecIntA.push_back();
- cout<<"===================="<<endl;
- {
- int iSize=vecIntA.size();//放回容器中元素的个数
- bool isEmpty=vecIntA.empty();//判断容器是否为空
- //4.比较操作
- bool isEqual= (vecIntB == vecIntA);
- bool isNotEqual= (vecIntB != vecIntA);
- cout<< iSize <<endl;
- cout<< isEmpty <<endl;
- cout<< isEqual <<endl;
- cout<< isNotEqual <<endl;
- }
- /*
- 0
- 1
- 1
- 0
- ====================
- 1
- 0
- 0
- 1
- Press any key to continue
- */
- }
- #include<algorithm>
- #include<numeric>
- #include<functional>
- bool greaterThan3(int iNum)
- {
- if(iNum >)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- void main_2()
- {
- int data[] = {,,,,,,,};
- vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
- int iCount = count_if(vecInt.begin(), vecInt.end(), greaterThan3);
- cout<<iCount<<endl;
- /*
- 6
- Press any key to continue
- */
- }
- void main_3()
- {
- //bianry_search();//在有序序列中查找value,找到则返回true。注意:在无序序列中,不可使用。
- //例:
- int data[] = {,,,,,,,};
- vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
- bool bFind = binary_search(vecInt.begin(), vecInt.end(), );
- cout<<bFind<<endl;
- bFind = binary_search(vecInt.begin(), vecInt.end(), );
- cout<<bFind<<endl;
- /*
- 1
- 0
- Press any key to continue
- */
- {
- int data[] = {,,,,,,,};
- vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
- bool bFind = binary_search(vecInt.begin(), vecInt.end(), );
- cout<<bFind<<endl;
- bFind = binary_search(vecInt.begin(), vecInt.end(), );
- cout<<bFind<<endl;
- /*
- 0
- 0
- Press any key to continue
- */
- }
- }
- void main_4()
- {
- //查找指定元素个数:
- //count();//利用等于操作符,把标志范围内的元素与输入值比较,返回相等的个数。
- //例:
- int data[] = {,,,,,,,};
- vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
- int iCount = count(vecInt.begin(), vecInt.end(), );
- cout<<iCount<<endl;
- /*
- 2
- Press any key to continue
- */
- }
- void main_5()
- {
- //count_if:();//利用输入的函数,对标志范围内的元素进行比较操作,返回结果为true的个数。
- //例:
- int data[] = {,,,,,,,};
- vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
- int iCount = count_if(vecInt.begin(), vecInt.end(), greaterThan3);
- cout<<iCount<<endl;
- /*
- 6
- Press any key to continue
- */
- }
- void main_6()
- {
- //条件查找
- //find_if();//查找满足条件的元素位置
- //例:
- int data[] = {,,,,,,,};
- vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
- vector<int>::iterator it = find_if(vecInt.begin(), vecInt.end(), greaterThan3);
- while(it != vecInt.end())
- {
- cout<<*it<<endl;
- it = find_if(it+, vecInt.end(), greaterThan3);
- }
- /*
- 4
- 5
- 6
- 6
- 11
- Press any key to continue
- */
- }
- void show(const int &item)
- {
- cout << item << " ";
- }
- int increase(int item)
- {
- return item + ;
- }
- void main_7()
- {
- //2、常用合并算法
- //加集:
- //merge()//合并:合并容器A和B到容器C。
- //例:
- int arry[]={,,,,};
- vector<int> vecIntA(arry , arry+sizeof(arry)/sizeof(int) );//默认构造函数
- vector<int> vecIntB(vecIntA);//调用拷贝构造函数
- vector<int> vecIntBB;
- vecIntBB.resize(vecIntB.size());
- transform(vecIntB.begin(), vecIntB.end(),vecIntBB.begin(), increase);
- vector<int> vecIntC;
- vecIntC.resize( vecIntA.size() + vecIntB.size() );
- //merge(vecIntA.begin(),vecIntA.end(),vecIntB.begin(),vecIntB.end(), vecIntC.begin());
- merge(vecIntA.begin(),vecIntA.end(),vecIntBB.begin(),vecIntBB.end(), vecIntC.begin());
- for_each(vecIntC.begin(), vecIntC.end(), show);
- cout<<endl;
- /*
- 1 2 3 4 5 6 7 8 9 10
- Press any key to continue
- */
- }
- class Student
- {
- public:
- Student():m_id(),m_name(""){}
- Student(int id, string name):m_id(id),m_name(name){}
- Student(const Student & stu):m_id(stu.m_id),m_name(stu.m_name){}
- public:
- int m_id;
- string m_name;
- };
- bool compare(const Student &stuA, const Student &stuB)
- {
- return stuA.m_id<stuB.m_id ? true : false;
- }
- void main_8()
- {
- //3、常用其他算法
- //排序:
- //sort()//默认升序排序
- //例:
- //sort(vec.begin(), vec.end())
- //自定义排序:
- //sort(beg,end,compare)//按照自定义的规则排序
- //例:
- //srand(time(0));//随机数发生器的初始化
- vector<Student> vecStudent;
- vecStudent.push_back(Student(, "小明3"));
- vecStudent.push_back(Student(, "小明5"));
- vecStudent.push_back(Student(, "小明2"));
- vecStudent.push_back(Student(, "小明1"));
- vecStudent.push_back(Student(, "小明4"));
- sort(vecStudent.begin(), vecStudent.end(), compare);
- vector<Student>::iterator it;
- for(it=vecStudent.begin(); it!=vecStudent.end(); it++)
- {
- cout << (*it).m_name.c_str() << " ";
- }
- cout<<endl;
- /*
- 小明1 小明2 小明3 小明4 小明5
- Press any key to continue
- */
- }
- void main_9()
- {
- //颠倒顺序:
- //reverse();//反转原有排序
- //reverse(vec.begin(), vec.end());
- int arry[]={,,,,};
- vector<int> vecIntA(arry , arry+sizeof(arry)/sizeof(int) );//默认构造函数
- reverse(vecIntA.begin(), vecIntA.end());
- for_each(vecIntA.begin(), vecIntA.end(), show);
- cout<<endl;
- /*
- 9 7 5 3 1
- Press any key to continue
- */
- }
- void main_10()
- {
- //拷贝:
- // copy();//拷贝容器A的指定区间到容器B
- //例:
- vector<Student> vecStudent;
- vector<Student> vecStu;
- vecStudent.push_back(Student(, "小明3"));
- vecStudent.push_back(Student(, "小明5"));
- vecStudent.push_back(Student(, "小明2"));
- vecStudent.push_back(Student(, "小明1"));
- vecStudent.push_back(Student(, "小明4"));
- vecStu.resize(vecStudent.size());//需要有默认的构造函数Student()
- copy(vecStudent.begin(), vecStudent.end(), vecStu.begin());
- vector<Student>::iterator it;
- for(it=vecStu.begin(); it!=vecStu.end(); it++)
- {
- cout<<(*it).m_name.c_str()<<endl;
- }
- /*
- 小明3
- 小明5
- 小明2
- 小明1
- 小明4
- Press any key to continue
- */
- }
- void main_11()
- {
- //替换:
- //replace();//将指定元素替换成给定的元素
- //replace(vec.begin(), vec.end(), 3, 10);//将容器中所用等于3的元素替换成10
- //条件替换:
- //replace_if();//替换满足条件的元素
- //例:
- int data[] = {,,,,,,,};
- vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
- vector<int>::iterator it;
- for(it=vecInt.begin(); it!=vecInt.end(); it++)
- {
- cout<<*it<<"\t";
- }
- cout<<endl;
- replace_if(vecInt.begin(), vecInt.end(), greaterThan3, );
- for(it=vecInt.begin(); it!=vecInt.end(); it++)
- {
- cout<<*it<<"\t";
- }
- cout<<endl;
- replace(vecInt.begin(), vecInt.end(), ,);
- for(it=vecInt.begin(); it!=vecInt.end(); it++)
- {
- cout<<*it<<"\t";
- }
- cout<<endl;
- /*
- 1 2 4 5 6 3 6 11
- 1 2 10 10 10 3 10 10
- 1 2 3 3 3 3 3 3
- Press any key to continue
- */
- }
- void main_12()
- {
- //交换:
- //swap(vec1,vec2);//交换容器元素
- int arry[]={,,,,};
- vector<int> vecIntA(arry , arry+sizeof(arry)/sizeof(int) );//默认构造函数
- vector<int> vecIntB(vecIntA);
- transform(vecIntA.begin(), vecIntA.end(),vecIntB.begin(),increase);
- swap(vecIntA,vecIntB);
- for_each(vecIntA.begin(), vecIntA.end(),show);
- cout<<endl;
- for_each(vecIntB.begin(), vecIntB.end(),show);
- cout<<endl;
- /*
- 2 4 6 8 10
- 1 3 5 7 9
- Press any key to continue
- */
- }
- void main_13()
- {
- //计算和:
- //accumulate(vec.begin(), vec.end(), 100);//计算从vec.begin()到vec.end()的和再加上最后的100
- int arry[]={,,,,};
- vector<int> vecIntA(arry , arry+sizeof(arry)/sizeof(int) );
- int sum=accumulate(vecIntA.begin(), vecIntA.end(), );
- cout<<sum<<endl;
- /*
- 125
- Press any key to continue
- */
- }
- void main()
- {
- // 填充:
- // fill(vec.begin(), vec.end(), 100);//将vec里的值全部填充成100
- vector<int> vecInt;
- vecInt.resize();
- fill(vecInt.begin(), vecInt.end(), );
- for_each(vecInt.begin(), vecInt.end(),show);
- cout<<endl;
- /*
- 100 100 100 100 100 100 100 100 100 100 100 100
- Press any key to continue
- */
- }
STLNormalFunc的更多相关文章
随机推荐
- ThreadLocal源代码1
public class ThreadLocalTrxt { static ThreadLocal<Object> x1 = new ThreadLocal<Object>() ...
- 嵌入式02 STM32 实验07 串口通信
STM32串口通信(F1系列包含3个USART和2个UART) 一.单片机与PC机串行通信研究目的和意义: 单片机自诞生以来以其性能稳定,价格低廉.功能强大.在智能仪器.工业装备以及日用电子消费产品中 ...
- 列表,元组以及range
列表,元组以及range 一.列表(list) 列表是数据类型之一,它有序,可变,支持索引 作用:存储数据,支持的数据类型很多:字符串,数字,布尔值,列表等 # 定义一个列表 lst = ['alex ...
- go语言浅析二叉树
Hello,各位小伙伴大家好,我是小栈君,今天给大家带来的分享是关于关于二叉树相关的知识点,并用go语言实现一个二叉树和对二叉树进行遍历. 我们主要针对二叉树的概念,go实战实现二叉树的前序遍历.中序 ...
- java之spring之spring整合hibernate
这篇讲下spring和hibernate的整合 目录结构如下: 1.新建java项目 2.导入jar包 antlr-2.7.7.jar aopalliance.jar aspectjweaver.ja ...
- IE浏览器 location.href 不跳转
var url = "https://www.cnblogs.com/zing"; location.href = url; window.event.returnValue = ...
- linux搭建GitLab
GitLab CentOS6 1. 安装VMware和CentOS 2. 安装必备Linux插件 3. 准备安装GitLab 4. 开始安装GitLab 5. 配置GitLab 6. 启动GitLab ...
- Linux系统怎么分区
linux分区方法,不同的人有不同的方法,反正没有统一的方法.在分区方面,我觉得根据自己的实际情况来分是最好的.玩linux也有好几年了,下面说一下,我在分区方面的一些经验. 一,个人用 如果是个人用 ...
- Java知识回顾 (14)网络编程
本资料来自于runoob,略有修改. 网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来. java.net 包中 J2SE 的 API 包含有类和接口,它们提供低层次的通信细 ...
- sublime 快速编写代码技巧
在sublime上装了Emmet插件后,我们就可以利用以下技巧快速编写代码 1.自动生成html头文件 html:5 或!:用于HTML5文档类型 html:xt:用于XHTML过渡文档类型 html ...