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的更多相关文章
随机推荐
- Graph machine learning 工具
OGB: Open Graph Benchmark https://ogb.stanford.edu/ https://github.com/snap-stanford/ogb OGB is a co ...
- asp.net中的html標籤runat=server時的映射
asp.net中的html控制項runat=server時的映射 1.標準xhtml標籤:http://blog.csdn.net/TangZhongxin/archive/2009/07/31/43 ...
- Word 插入脚注、尾注与题注 -- 视频教程(5)
>> 视频教程链接:B站,速度快,清晰 未完 ...... 点击访问原文(进入后根据右侧标签,快速定位到本文)
- BJFU-217-基于链式存储结构的图书信息表的逆序存储
这道题可以用头插法创建列表,然后正常输出: #include<stdio.h> #include<stdlib.h> #define MAX 100 typedef struc ...
- Django框架深入了解_05 (Django中的缓存、Django解决跨域流程(非简单请求,简单请求)、自动生成接口文档)
一.Django中的缓存: 前戏: 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增,删,查,改,渲染模板,执行业务逻辑,最后生成用户看到的页面. 当一个网站的用户访问量很大的时候,每一 ...
- 第一个python小脚本
第一个python小实验 前言 作为一个工作1年的linux运维搬砖师来说,发现没点开发能力真的是不好混啊.于是下定决心学习python! 直接上刚写的语句(大神莫鄙视) 通过控制台输入一个账号密码, ...
- zbar android sdk源码编译
zbar,解析条码和二维码的又一利器,zbar代码是用c语言编写的,如果想在Android下使用zbar类库,就需要使用NDK将zbar编译成.so加载使用,zbar编译好的Android SDK可以 ...
- 三伏天里小试牛刀andriod 开发 #华为云·寻找黑马程序员#【华为云技术分享】
2019年07月,北京,三伏天,好热啊.越热自己还越懒得动换(肉身给的信号),但是做为产品经理/交互设计师的,总想着思考些什么(灵魂上给的信号),或者是学习些什么,更有利于将来的职业发展吧,哈哈哈.工 ...
- 2019 央视网java面试笔试题 (含面试题解析)
本人3年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.央视网等公司offer,岗位是Java后端开发,最终选择去了央视网. 面试了很多家公司,感觉大部分公司考察的点都差 ...
- Python进阶(八)----模块,import , from import 和 `__name__`的使用
Python进阶(八)----模块,import , from import 和 __name__的使用 一丶模块的初识 #### 什么是模块: # 模块就是一个py文件(这个模块存放很多相似的功能, ...