
stl
闭上左眼看世界
这个作者很懒,什么都没留下…
展开
-
STL:string常用函数方法使用介绍
0.append 用来在字符串的结尾增加字符串.也可以使用’+’运算符直接增加字符串.string str = "abcde";str.append("ab");cout << str << endl;//abcdeabstring str = "abcde";str += "ab";cout << str <原创 2018-03-27 21:54:00 · 1028 阅读 · 0 评论 -
STL常用算法: fill,rotate,rotate_copy.
fill算法用来填充容器,当然创建容器的时候,也可以达到与fill算法一样的效果.#include <iostream>#include <vector>#include <algorithm>using namespace std;//fill算法的手动实现.template <typename T, typename V>void myFill(T first, T last, co原创 2017-11-25 14:05:49 · 311 阅读 · 0 评论 -
STL通用算法: iter_swap,reverse,reverse_copy,advance算法
在学习C语言时,当我们需要互换两个数的时候,我们会这么写,或者通过指针去写.int temp = a;a = b;b = temp;所以在STL中针对互换容器内的元素时,就有了iter_swap基础算法,就是用来互换两个指针所指向内容的方法.#include <iostream>#include <algorithm>#include <vector>#include <functiona原创 2017-11-22 16:51:09 · 459 阅读 · 0 评论 -
STL通用算法: for_each,c++11标准范围for,transform.
之前的算法都用到了for_each算法把容器内的元素输出到屏幕上,所以今天讲讲for_each这个算法,还有和它功能类似的范围for的用法.#include <iostream>#include <algorithm>#include <vector>using namespace std;template <typename T>class Print{public: void原创 2017-11-17 20:14:20 · 254 阅读 · 0 评论 -
STL常用算法: remove,remove_if,remove_copy,remove_copy_if,remove系列算法和partition的区别...
下面我要介绍的是STL算法中,用来进行按条件除去容器内元素的通用算法,也就是所有容器都适用的方法.#include <iostream>#include <algorithm>#include <functional>#include <list>using namespace std;template <typename T>class Print{public: void原创 2017-11-15 13:55:52 · 832 阅读 · 0 评论 -
STL常用算法: replace.replace_copy.replace_if.replace_copy_if
在STL中,我们可以用算法把容器内的一些元素进行替换,这里就用到了replace系列的算法.首先是最简单的replace算法.#include <iostream>#include <vector>#include <algorithm>#include <functional>using namespace std;//模板输出类.template <typename T>class原创 2017-11-13 13:06:49 · 398 阅读 · 0 评论 -
STL常用算法: distance实现,count_if算法实现,bind2nd的使用..
前面讲到用distance算法来返回两个迭代器之间的距离.下面给出实现:template <typename T>int myDistance(T a, T b){ int distance = 0; if (a <= b) { while (a != b) { distance++; a++原创 2017-11-09 20:35:29 · 777 阅读 · 0 评论 -
STL常用算法: max_element,min_element, distance以及手动实现.
今天主要讲解如何在容器中找到最大元素或者最小元素的方法,就不用我们手动去写函数来找到最值的大小和位置啦~.//头文件!不要忘啦,否则编译不能通过的.#include <iostream>#include <algorithm>#include <functional>#include <vector>using namespace std;int main(){ vector<i原创 2017-11-08 20:51:42 · 2339 阅读 · 0 评论 -
STL简介
学C++,必须也要学会使用STL,STL全称standard template library,也就是所谓的标准模板库,它包括了容器,也就是我们学习数据结构时自己手写的链表,栈,队列等等,当然栈和队列只是在原有链表或者deque的基础上砍掉了某些东西而形成的一种结构,在STL中,它不属于容器的范畴,而是属于配接(adapters). 我们可以很明显的从STL中看到什么叫做泛型,泛型编程是如何的,而原创 2017-11-08 20:14:22 · 476 阅读 · 0 评论 -
STL: vector容器成员函数resize的作用
#include <iostream>#include <vector>#include <time.h>#include <algorithm>#define SIZE 5000000using namespace std;class A{public: int operator()() { return x++; } static i原创 2017-10-26 23:20:27 · 669 阅读 · 0 评论 -
STL: string和vector的容量变化
我是在vs2013下所做的测试,实验结果可能和在vc6.0或者其他编译器上有所不同.当string的元素个数<=15时,容量恒为15,当新元素增加等导致容量增加时,取原容量的1.5倍和原容量+16的一个倍数作比较,取大值.那么这个倍数怎么取呢?只要原容量一次次的增加16,当数值大于所需新容量时即可.string容量第一个例子:#include <iostream>#include <string>原创 2017-10-26 22:55:28 · 577 阅读 · 0 评论