1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. void main_1()
  7. {
  8. vector<int> vecIntA;//默认构造函数
  9. vector<int> vecIntB(vecIntA);//调用拷贝构造函数
  10.  
  11. int iSize=vecIntA.size();//放回容器中元素的个数
  12. bool isEmpty=vecIntA.empty();//判断容器是否为空
  13.  
  14. //4.比较操作
  15. bool isEqual= (vecIntB == vecIntA);
  16. bool isNotEqual= (vecIntB != vecIntA);
  17.  
  18. cout<< iSize <<endl;
  19. cout<< isEmpty <<endl;
  20. cout<< isEqual <<endl;
  21. cout<< isNotEqual <<endl;
  22.  
  23. vecIntA.push_back();
  24. cout<<"===================="<<endl;
  25. {
  26. int iSize=vecIntA.size();//放回容器中元素的个数
  27. bool isEmpty=vecIntA.empty();//判断容器是否为空
  28.  
  29. //4.比较操作
  30. bool isEqual= (vecIntB == vecIntA);
  31. bool isNotEqual= (vecIntB != vecIntA);
  32.  
  33. cout<< iSize <<endl;
  34. cout<< isEmpty <<endl;
  35. cout<< isEqual <<endl;
  36. cout<< isNotEqual <<endl;
  37. }
  38. /*
  39. 0
  40. 1
  41. 1
  42. 0
  43. ====================
  44. 1
  45. 0
  46. 0
  47. 1
  48. Press any key to continue
  49. */
  50. }
  51.  
  52. #include<algorithm>
  53. #include<numeric>
  54. #include<functional>
  55.  
  56. bool greaterThan3(int iNum)
  57. {
  58. if(iNum >)
  59. {
  60. return true;
  61. }
  62. else
  63. {
  64. return false;
  65. }
  66. }
  67.  
  68. void main_2()
  69. {
  70. int data[] = {,,,,,,,};
  71. vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
  72.  
  73. int iCount = count_if(vecInt.begin(), vecInt.end(), greaterThan3);
  74. cout<<iCount<<endl;
  75. /*
  76. 6
  77. Press any key to continue
  78. */
  79. }
  80.  
  81. void main_3()
  82. {
  83. //bianry_search();//在有序序列中查找value,找到则返回true。注意:在无序序列中,不可使用。
  84. //例:
  85. int data[] = {,,,,,,,};
  86. vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
  87. bool bFind = binary_search(vecInt.begin(), vecInt.end(), );
  88. cout<<bFind<<endl;
  89. bFind = binary_search(vecInt.begin(), vecInt.end(), );
  90. cout<<bFind<<endl;
  91. /*
  92. 1
  93. 0
  94. Press any key to continue
  95. */
  96. {
  97. int data[] = {,,,,,,,};
  98. vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
  99. bool bFind = binary_search(vecInt.begin(), vecInt.end(), );
  100. cout<<bFind<<endl;
  101. bFind = binary_search(vecInt.begin(), vecInt.end(), );
  102. cout<<bFind<<endl;
  103. /*
  104. 0
  105. 0
  106. Press any key to continue
  107. */
  108. }
  109. }
  110.  
  111. void main_4()
  112. {
  113. //查找指定元素个数:
  114. //count();//利用等于操作符,把标志范围内的元素与输入值比较,返回相等的个数。
  115. //例:
  116. int data[] = {,,,,,,,};
  117. vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
  118. int iCount = count(vecInt.begin(), vecInt.end(), );
  119. cout<<iCount<<endl;
  120. /*
  121. 2
  122. Press any key to continue
  123. */
  124. }
  125.  
  126. void main_5()
  127. {
  128. //count_if:();//利用输入的函数,对标志范围内的元素进行比较操作,返回结果为true的个数。
  129. //例:
  130. int data[] = {,,,,,,,};
  131. vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
  132.  
  133. int iCount = count_if(vecInt.begin(), vecInt.end(), greaterThan3);
  134. cout<<iCount<<endl;
  135. /*
  136. 6
  137. Press any key to continue
  138. */
  139. }
  140.  
  141. void main_6()
  142. {
  143. //条件查找
  144. //find_if();//查找满足条件的元素位置
  145. //例:
  146. int data[] = {,,,,,,,};
  147. vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
  148. vector<int>::iterator it = find_if(vecInt.begin(), vecInt.end(), greaterThan3);
  149. while(it != vecInt.end())
  150. {
  151. cout<<*it<<endl;
  152. it = find_if(it+, vecInt.end(), greaterThan3);
  153. }
  154. /*
  155. 4
  156. 5
  157. 6
  158. 6
  159. 11
  160. Press any key to continue
  161. */
  162. }
  163.  
  164. void show(const int &item)
  165. {
  166. cout << item << " ";
  167. }
  168.  
  169. int increase(int item)
  170. {
  171. return item + ;
  172. }
  173.  
  174. void main_7()
  175. {
  176. //2、常用合并算法
  177. //加集:
  178. //merge()//合并:合并容器A和B到容器C。
  179. //例:
  180. int arry[]={,,,,};
  181. vector<int> vecIntA(arry , arry+sizeof(arry)/sizeof(int) );//默认构造函数
  182. vector<int> vecIntB(vecIntA);//调用拷贝构造函数
  183. vector<int> vecIntBB;
  184. vecIntBB.resize(vecIntB.size());
  185. transform(vecIntB.begin(), vecIntB.end(),vecIntBB.begin(), increase);
  186.  
  187. vector<int> vecIntC;
  188. vecIntC.resize( vecIntA.size() + vecIntB.size() );
  189.  
  190. //merge(vecIntA.begin(),vecIntA.end(),vecIntB.begin(),vecIntB.end(), vecIntC.begin());
  191. merge(vecIntA.begin(),vecIntA.end(),vecIntBB.begin(),vecIntBB.end(), vecIntC.begin());
  192.  
  193. for_each(vecIntC.begin(), vecIntC.end(), show);
  194. cout<<endl;
  195. /*
  196. 1 2 3 4 5 6 7 8 9 10
  197. Press any key to continue
  198. */
  199. }
  200.  
  201. class Student
  202. {
  203. public:
  204. Student():m_id(),m_name(""){}
  205. Student(int id, string name):m_id(id),m_name(name){}
  206. Student(const Student & stu):m_id(stu.m_id),m_name(stu.m_name){}
  207. public:
  208. int m_id;
  209. string m_name;
  210. };
  211.  
  212. bool compare(const Student &stuA, const Student &stuB)
  213. {
  214. return stuA.m_id<stuB.m_id ? true : false;
  215. }
  216.  
  217. void main_8()
  218. {
  219. //3、常用其他算法
  220. //排序:
  221. //sort()//默认升序排序
  222. //例:
  223. //sort(vec.begin(), vec.end())
  224. //自定义排序:
  225. //sort(beg,end,compare)//按照自定义的规则排序
  226. //例:
  227. //srand(time(0));//随机数发生器的初始化
  228. vector<Student> vecStudent;
  229.  
  230. vecStudent.push_back(Student(, "小明3"));
  231. vecStudent.push_back(Student(, "小明5"));
  232. vecStudent.push_back(Student(, "小明2"));
  233. vecStudent.push_back(Student(, "小明1"));
  234. vecStudent.push_back(Student(, "小明4"));
  235.  
  236. sort(vecStudent.begin(), vecStudent.end(), compare);
  237.  
  238. vector<Student>::iterator it;
  239. for(it=vecStudent.begin(); it!=vecStudent.end(); it++)
  240. {
  241. cout << (*it).m_name.c_str() << " ";
  242. }
  243. cout<<endl;
  244. /*
  245. 小明1 小明2 小明3 小明4 小明5 
  246. Press any key to continue
  247. */
  248. }
  249.  
  250. void main_9()
  251. {
  252. //颠倒顺序:
  253. //reverse();//反转原有排序
  254. //reverse(vec.begin(), vec.end());
  255. int arry[]={,,,,};
  256. vector<int> vecIntA(arry , arry+sizeof(arry)/sizeof(int) );//默认构造函数
  257. reverse(vecIntA.begin(), vecIntA.end());
  258. for_each(vecIntA.begin(), vecIntA.end(), show);
  259. cout<<endl;
  260. /*
  261. 9 7 5 3 1
  262. Press any key to continue
  263. */
  264. }
  265.  
  266. void main_10()
  267. {
  268. //拷贝:
  269. // copy();//拷贝容器A的指定区间到容器B
  270. //例:
  271. vector<Student> vecStudent;
  272. vector<Student> vecStu;
  273.  
  274. vecStudent.push_back(Student(, "小明3"));
  275. vecStudent.push_back(Student(, "小明5"));
  276. vecStudent.push_back(Student(, "小明2"));
  277. vecStudent.push_back(Student(, "小明1"));
  278. vecStudent.push_back(Student(, "小明4"));
  279.  
  280. vecStu.resize(vecStudent.size());//需要有默认的构造函数Student()
  281. copy(vecStudent.begin(), vecStudent.end(), vecStu.begin());
  282.  
  283. vector<Student>::iterator it;
  284. for(it=vecStu.begin(); it!=vecStu.end(); it++)
  285. {
  286. cout<<(*it).m_name.c_str()<<endl;
  287. }
  288. /*
  289. 小明3
  290. 小明5
  291. 小明2
  292. 小明1
  293. 小明4
  294. Press any key to continue
  295. */
  296. }
  297.  
  298. void main_11()
  299. {
  300. //替换:
  301. //replace();//将指定元素替换成给定的元素
  302. //replace(vec.begin(), vec.end(), 3, 10);//将容器中所用等于3的元素替换成10
  303. //条件替换:
  304. //replace_if();//替换满足条件的元素
  305. //例:
  306. int data[] = {,,,,,,,};
  307. vector<int> vecInt(data, data+sizeof(data)/sizeof(int));
  308.  
  309. vector<int>::iterator it;
  310. for(it=vecInt.begin(); it!=vecInt.end(); it++)
  311. {
  312. cout<<*it<<"\t";
  313. }
  314. cout<<endl;
  315.  
  316. replace_if(vecInt.begin(), vecInt.end(), greaterThan3, );
  317. for(it=vecInt.begin(); it!=vecInt.end(); it++)
  318. {
  319. cout<<*it<<"\t";
  320. }
  321. cout<<endl;
  322.  
  323. replace(vecInt.begin(), vecInt.end(), ,);
  324. for(it=vecInt.begin(); it!=vecInt.end(); it++)
  325. {
  326. cout<<*it<<"\t";
  327. }
  328. cout<<endl;
  329. /*
  330. 1 2 4 5 6 3 6 11
  331. 1 2 10 10 10 3 10 10
  332. 1 2 3 3 3 3 3 3
  333. Press any key to continue
  334. */
  335. }
  336.  
  337. void main_12()
  338. {
  339. //交换:
  340. //swap(vec1,vec2);//交换容器元素
  341. int arry[]={,,,,};
  342. vector<int> vecIntA(arry , arry+sizeof(arry)/sizeof(int) );//默认构造函数
  343. vector<int> vecIntB(vecIntA);
  344. transform(vecIntA.begin(), vecIntA.end(),vecIntB.begin(),increase);
  345. swap(vecIntA,vecIntB);
  346. for_each(vecIntA.begin(), vecIntA.end(),show);
  347. cout<<endl;
  348. for_each(vecIntB.begin(), vecIntB.end(),show);
  349. cout<<endl;
  350. /*
  351. 2 4 6 8 10
  352. 1 3 5 7 9
  353. Press any key to continue
  354. */
  355. }
  356.  
  357. void main_13()
  358. {
  359. //计算和:
  360. //accumulate(vec.begin(), vec.end(), 100);//计算从vec.begin()到vec.end()的和再加上最后的100
  361. int arry[]={,,,,};
  362. vector<int> vecIntA(arry , arry+sizeof(arry)/sizeof(int) );
  363. int sum=accumulate(vecIntA.begin(), vecIntA.end(), );
  364. cout<<sum<<endl;
  365. /*
  366. 125
  367. Press any key to continue
  368. */
  369. }
  370.  
  371. void main()
  372. {
  373. // 填充:
  374. // fill(vec.begin(), vec.end(), 100);//将vec里的值全部填充成100
  375. vector<int> vecInt;
  376. vecInt.resize();
  377. fill(vecInt.begin(), vecInt.end(), );
  378. for_each(vecInt.begin(), vecInt.end(),show);
  379. cout<<endl;
  380. /*
  381. 100 100 100 100 100 100 100 100 100 100 100 100
  382. Press any key to continue
  383. */
  384. }

STLNormalFunc的更多相关文章

随机推荐

  1. ThreadLocal源代码1

    public class ThreadLocalTrxt { static ThreadLocal<Object> x1 = new ThreadLocal<Object>() ...

  2. 嵌入式02 STM32 实验07 串口通信

    STM32串口通信(F1系列包含3个USART和2个UART) 一.单片机与PC机串行通信研究目的和意义: 单片机自诞生以来以其性能稳定,价格低廉.功能强大.在智能仪器.工业装备以及日用电子消费产品中 ...

  3. 列表,元组以及range

    列表,元组以及range 一.列表(list) 列表是数据类型之一,它有序,可变,支持索引 作用:存储数据,支持的数据类型很多:字符串,数字,布尔值,列表等 # 定义一个列表 lst = ['alex ...

  4. go语言浅析二叉树

    Hello,各位小伙伴大家好,我是小栈君,今天给大家带来的分享是关于关于二叉树相关的知识点,并用go语言实现一个二叉树和对二叉树进行遍历. 我们主要针对二叉树的概念,go实战实现二叉树的前序遍历.中序 ...

  5. java之spring之spring整合hibernate

    这篇讲下spring和hibernate的整合 目录结构如下: 1.新建java项目 2.导入jar包 antlr-2.7.7.jar aopalliance.jar aspectjweaver.ja ...

  6. IE浏览器 location.href 不跳转

    var url = "https://www.cnblogs.com/zing"; location.href = url; window.event.returnValue = ...

  7. linux搭建GitLab

    GitLab CentOS6 1. 安装VMware和CentOS 2. 安装必备Linux插件 3. 准备安装GitLab 4. 开始安装GitLab 5. 配置GitLab 6. 启动GitLab ...

  8. Linux系统怎么分区

    linux分区方法,不同的人有不同的方法,反正没有统一的方法.在分区方面,我觉得根据自己的实际情况来分是最好的.玩linux也有好几年了,下面说一下,我在分区方面的一些经验. 一,个人用 如果是个人用 ...

  9. Java知识回顾 (14)网络编程

    本资料来自于runoob,略有修改. 网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来. java.net 包中 J2SE 的 API 包含有类和接口,它们提供低层次的通信细 ...

  10. sublime 快速编写代码技巧

    在sublime上装了Emmet插件后,我们就可以利用以下技巧快速编写代码 1.自动生成html头文件 html:5 或!:用于HTML5文档类型 html:xt:用于XHTML过渡文档类型 html ...