开学就要上OOP了.....感觉十分萌萌哒- -!

整理自《ACM程序设计》,本文为转载(原文地址)

  迭代器(iterator)

  个人理解就是把所有和迭代有关的东西给抽象出来的,不管是数组的下标,指针,for里面的、list里面的、vector里面的,抽象一下变成了iterator

 #include <iostream>
#include <vector> using namespace std; int main()
{
vector<int> v;
for(int i = ; i < ; ++i )
{
v.push_back(i);
}
for(vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
return ;
}

  

  求和(<numeric> accumulate)

  accumulate(v.begin(),v.end(),0),把从 v.begin() 开始到 v.end()结束所有的元素加到 0上面去

 #include <iostream>
#include <vector>
#include <numeric> using namespace std; int main()
{
vector<int> v;
for(int i = ; i < ; ++i )
{
v.push_back(i);
}
for(vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
cout << accumulate(v.begin(),v.end(),) << endl;
return ;
}

  vector(动态数组)

  vector有内存管理的机制,也就是说对于插入和删除,vector可以动态调整所占用的内存空间。  

  vector相关函数

 #include <iostream>
#include <vector> using namespace std; int main()
{
vector<int> v;
v.push_back(); //数组尾部插入3
v.push_back();
v.push_back();
v.push_back();
cout << " 下标 " << v[] << endl;
cout << " 迭代器 " << endl;
for(vector<int>::iterator i = v.begin();i!= v.end();++i)
{
cout << *i << " ";
}
cout << endl;
//在第一个元素之前插入111 insert begin+n是在第n个元素之前插入
v.insert(v.begin(),);
//在最后一个元素之后插入222 insert end + n 是在n个元素之后插入
v.insert(v.end(),); for(vector<int>::iterator i = v.begin();i!= v.end();++i)
{
cout << *i << " ";
}
cout << endl; vector<int> arr();
for(int i = ; i < ; i++)
{
arr[i] = i;
}
for(vector<int>::iterator i = arr.begin();i!= arr.end();++i)
{
cout << *i << " ";
}
cout << endl; //删除 同insert
arr.erase(arr.begin()); for(vector<int>::iterator i = arr.begin();i!= arr.end();++i)
{
cout << *i << " " ;
}
cout << endl ; arr.erase(arr.begin(),arr.begin()+); for(vector<int>::iterator i = arr.begin();i!= arr.end();++i)
{
cout << *i << " " ;
}
cout << endl ;
return ;
}

  数组转置 (<algorithm> reverse)

  reverse(v.begin(),v.end())

 #include<iostream>
#include<vector>
#include<algorithm> using namespace std; int main()
{
vector<int> v;
for(int i = ; i < ; ++i)
{
v.push_back(i);
}
for(vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << *it << " ";
}
cout << endl; reverse(v.begin(),v.end()); for(vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
return ;
}

  排序(<algorithm> sort)

  sort(v.begin(),v.end())

 #include<iostream>
#include<vector>
#include<algorithm> using namespace std; bool Comp(const int &a,const int &b)
{
return a>b;
} int main()
{
vector<int> v;
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back(-);
v.push_back();
v.push_back();
v.push_back();
v.push_back(); for(vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << *it << " ";
}
cout << endl; //默认升序
sort(v.begin(),v.end()); for(vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << *it << " ";
}
cout << endl; //用降序 需要自定义一个降序函数
sort(v.begin(),v.end(),Comp); for(vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << *it << " ";
}
cout << endl; return ;
}

  字符串(<string>)

  输入

 #include<iostream>
#include<string>
#include<cstdio> using namespace std; int main()
{
string s1;
s1 = "hello"; string s2;
char s[];
//scanf 输入速度比cin快的多
//scanf 是C函数,不可以输入string
scanf("%s",s);
s2 = s; cout << s1 << endl;
cout << s2 << endl; return ;
}

 

  尾部添加字符字符串直接用+号 例如: s += 'a'; s += "abc",或者使用append方法,s.append(“123”)

  删除 (erase clear)

  s.erase(it + 1,it + 4); clear()

 #include<iostream>
#include<string> using namespace std; int main()
{
string s;
s = "";
cout << s << endl; string::iterator it = s.begin(); //删除s[3]
s.erase(it+);
cout << s << endl; //删除s[1]~s[3]
s = "";
s.erase(it + ,it + );
cout << s << endl; //全部删除
s.clear();
cout << "clear : " << s << endl; return ;
}

  查找(find)

  用find找到string里面第一个要找到元素(char或者串),找到返回数组下标,找不到返回end()迭代器

  string和vector有很多相同的东西,比如length(),size(),empty(),reverse(),相对也容易,就不一一说了。

  数字化处理(string)

  经常会遇到这样一种情况,有一个数字,需要把每一位给提取出来,如果用取余数的方法,花费的时间就会很长,所以可以当成字符串来处理,方便、省时。

  例子:求一个整数各位数的和

 #include<iostream>
#include<string> using namespace std; int main()
{
string s;
s = "";
int sum = ;
for(int i = ; i < s.size(); ++i)
{
switch(s[i])
{
case '': sum += ;break;
case '': sum += ;break;
case '': sum += ;break;
case '': sum += ;break;
case '': sum += ;break;
case '': sum += ;break;
case '': sum += ;break;
case '': sum += ;break;
case '': sum += ;break;
}
} cout << sum << endl; return ;
}

  string与char *

  

 #include<iostream>
#include<string>
#include<cstdio> using namespace std; int main()
{
string s_string;
char s_char[];
scanf("%s",s_char); s_string = s_char; //printf输出char* 时用c_str处理
printf(s_string.c_str());
cout << endl; printf("%s",s_char);
cout << endl; cout << s_char << endl;
cout << s_string << endl;
return ;
}

  sscanf

  

 #include<iostream>
#include<string>
#include<cstdio> using namespace std; int main()
{
string s1,s2,s3;
char sa[],sb[],sc[];
sscanf("abc 123 wcd","%s%s%s",sa,sb,sc);
s1 = sa;
s2 = sb;
s3 = sc;
cout << s1 << " " << s2 << " " << s3 << endl; //将字符串分离成数字,分隔符为',''$'
int a,b,c;
sscanf("4,5$6","%d,%d$%d",&a,&b,&c);
cout << a << " " << b << " " << c << endl;
return ;
}

  string与数值相互转换( sprintf <sstream> )

 #include<iostream>
#include<string>
#include<sstream>
#include<cstdio> using namespace std; //c++ 方法 把数转换为string
string converToString(double x)
{
ostringstream o;
if( o << x)
{
// str()没有'\0' c_str有
return o.str();
}
return "error";
} double converFromString(const string &s)
{
istringstream i(s);
double x;
if( i >> x)
{
return x;
}
//if error
return 0.0;
}
int main()
{
char b[];
string s1; //c语言方法
sprintf(b,"%d",);
s1 = b;
cout << s1 << endl; string s2 = converToString();
cout << s2 << endl; string s3 = "";
int c = converFromString(s3);
cout << c << endl; string s4 = "casacsa6";
int d = converFromString(s4);
cout << d << endl; string s5 = "21abf4";
int f = converFromString(s5);
cout << f << endl; return ;
}

  set容器

  set是用红黑树的平衡二叉索引树的数据结构来实现的,插入时,它会自动调节二叉树排列,把元素放到适合的位置,确保每个子树根节点的键值大于左子树所有的值、小于右子树所有的值,插入重复数据时会忽略。set迭代器采用中序遍历,检索效率高于vector、deque、list,并且会将元素按照升序的序列遍历。set容器中的数值,一经更改,set会根据新值旋转二叉树,以保证平衡,构建set就是为了快速检索(python中的set一旦建立就是一个常量,不能改的)。

  multiset,与set不同之处就是它允许有重复的键值。

  正反遍历,迭代器iterator、reverse_iterator

 #include<iostream>
#include<set> using namespace std; int main()
{
set<int> v;
v.insert();
v.insert();
v.insert();
v.insert();
v.insert();
v.insert(); //中序遍历 升序遍历
for(set<int>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << *it << " ";
}
cout << endl; for(set<int>::reverse_iterator rit = v.rbegin(); rit != v.rend(); ++rit)
{
cout << *rit << " ";
}
cout << endl; return ;
}

  自定义比较函数,insert的时候,set会使用默认的比较函数(升序),很多情况下需要自己编写比较函数。

  1、如果元素不是结构体,可以编写比较函数,下面这个例子是用降序排列的(和上例插入数据相同):

 #include<iostream>
#include<set> using namespace std; struct Comp
{
//重载()
bool operator()(const int &a, const int &b)
{
return a > b;
}
};
int main()
{
set<int,Comp> v;
v.insert();
v.insert();
v.insert();
v.insert();
v.insert();
v.insert(); for(set<int,Comp>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << *it << " ";
}
cout << endl; for(set<int,Comp>::reverse_iterator rit = v.rbegin(); rit != v.rend(); ++rit)
{
cout << *rit << " ";
}
cout << endl; return ;
}

  2、元素本身就是结构体,直接把比较函数写在结构体内部,下面的例子依然降序:

 #include<iostream>
#include<set>
#include<string> using namespace std; struct Info
{
string name;
double score; //重载 <
bool operator < (const Info &a) const
{
return a.score < score;
}
};
int main()
{
set<Info> s;
Info info; info.name = "abc";
info.score = 123.3;
s.insert(info); info.name = "EDF";
info.score = -23.53;
s.insert(info); info.name = "xyz";
info.score = 73.3;
s.insert(info); for(set<Info>::iterator it = s.begin(); it != s.end(); ++it)
{
cout << (*it).name << ":" << (*it).score << endl;
}
cout << endl; for(set<Info>::reverse_iterator rit = s.rbegin(); rit != s.rend(); ++rit)
{
cout << (*rit).name << ":" << (*rit).score << endl;
}
cout << endl; return ;
}

  multiset与set的不同之处就是key可以重复,以及erase(key)的时候会删除multiset里面所有的key并且返回删除的个数。

  map

  map也是使用红黑树,他是一个键值对(key:value映射),便利时依然默认按照key程序的方式遍历,同set。

 #include<iostream>
#include<map>
#include<string> using namespace std; int main()
{
map<string,double> m; //声明即插入
m["li"] = 123.4;
m["wang"] = 23.1;
m["zhang"] = -21.9;
m["abc"] = 12.1;
for(map<string,double>::iterator it = m.begin(); it != m.end(); ++it)
{
//first --> key second --> value
cout << (*it).first << ":" << (*it).second << endl;
}
cout << endl;
return ;
}

  用map实现数字分离

  string --> number

  之前用string进行过数字分离,现在使用map

 #include<iostream>
#include<map>
#include<string> using namespace std; int main()
{
map<char,int> m; m[''] = ;
m[''] = ;
m[''] = ;
m[''] = ;
m[''] = ;
m[''] = ;
m[''] = ;
m[''] = ;
m[''] = ;
m[''] = ;
/*
等价于
for(int i = 0; i < 10; ++i)
{
m['0' + i] = i;
}
*/ string sa;
sa = "";
int sum = ;
for( int i = ; i < sa.length(); ++i)
{
sum += m[sa[i]];
}
cout << sum << endl;
return ;
}

  number --> string

 #include <iostream>
#include <map>
#include <string> using namespace std; int main()
{
map<int,char> m; for(int i = ; i < ; ++i)
{
m[i] = '' + i;
} int n = ; string out = "the number is :";
cout << out + m[n] << endl; return ;
}

  multimap

  multimap由于允许有重复的元素,所以元素插入、删除、查找都与map不同。

  插入insert(pair<a,b>(value1,value2))

 #include <iostream>
#include <map>
#include <string> using namespace std; int main()
{
multimap<string,double> m; m.insert(pair<string,double>("Abc",123.2));
m.insert(pair<string,double>("Abc",123.2));
m.insert(pair<string,double>("xyz",-43.2));
m.insert(pair<string,double>("dew",43.2)); for(multimap<string,double>::iterator it = m.begin(); it != m.end(); ++it )
{
cout << (*it).first << ":" << (*it).second << endl;
}
cout << endl; return ;
}

  至于删除和查找,erase(key)会删除掉所有key的map,查找find(key)返回第一个key的迭代器

  deque

  deque和vector一样,采用线性表,与vector唯一不同的是,deque采用的分块的线性存储结构,每块大小一般为512字节,称为一个deque块,所有的deque块使用一个Map块进行管理,每个map数据项记录各个deque块的首地址,这样以来,deque块在头部和尾部都可已插入和删除元素,而不需要移动其它元素。使用push_back()方法在尾部插入元素,使用push_front()方法在首部插入元素,使用insert()方法在中间插入元素。一般来说,当考虑容器元素的内存分配策略和操作的性能时,deque相对vectore更有优势。(下面这个图,我感觉Map块就是一个list< map<deque名字,deque地址> >)

  插入删除

  遍历当然可以使用下标遍历,在这里使用迭代器。

 #include <iostream>
#include <deque> using namespace std; int main()
{
deque<int> d; //尾部插入
d.push_back();
d.push_back();
d.push_back();
for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
{
cout << (*it) << " ";
}
cout << endl << endl; //头部插入
d.push_front();
d.push_front(-);
for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
{
cout << (*it) << " ";
}
cout << endl << endl; d.insert(d.begin() + ,);
for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
{
cout << (*it) << " ";
}
cout << endl << endl; //反方向遍历
for(deque<int>::reverse_iterator rit = d.rbegin(); rit != d.rend(); ++rit )
{
cout << (*rit) << " ";
}
cout << endl << endl; //删除元素pop pop_front从头部删除元素 pop_back从尾部删除元素 erase中间删除 clear全删
d.clear();
d.push_back();
d.push_back();
d.push_back();
d.push_back();
d.push_back();
d.push_back();
d.push_back();
d.push_back();
for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
{
cout << (*it) << " ";
}
cout << endl; d.pop_front();
d.pop_front();
for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
{
cout << (*it) << " ";
}
cout << endl; d.pop_back();
d.pop_back();
for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
{
cout << (*it) << " ";
}
cout << endl; d.erase(d.begin() + );
for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
{
cout << (*it) << " ";
}
cout << endl;
return ;
}

  list

  list<int> l

  插入:push_back尾部,push_front头部,insert方法前往迭代器位置处插入元素,链表自动扩张,迭代器只能使用++--操作,不能用+n -n,因为元素不是物理相连的。

  遍历:iterator和reverse_iterator正反遍历

  删除:pop_front删除链表首元素;pop_back()删除链表尾部元素;erase(迭代器)删除迭代器位置的元素,注意只能使用++--到达想删除的位置;remove(key) 删除链表中所有key的元素,clear()清空链表。

  查找:it = find(l.begin(),l.end(),key)

  排序:l.sort()

  删除连续重复元素:l.unique() 【2 8 1 1 1 5 1】 --> 【 2 8 1 5 1】

  bitset

  从来没用过,上两幅图吧就:

  stack(后进先出)

  这个印象深刻,学数据结构的时候做表达式求值的就是用的栈。

 #include <iostream>
#include <stack>
using namespace std; int main()
{ stack<int> s;
s.push();
s.push();
s.push();
s.push(); cout << s.size() << endl; while(s.empty() != true)
{
cout << s.top() << endl;
s.pop();
}
return ;
}

  stack然我唯一费解之处在于,貌似它没有iterator,可以试试s.begin()编译器报错的。

  queue(先进先出)

  queue有入队push(插入)、出队pop(删除)、读取队首元素front、读取队尾元素back、empty,size这几种方法

  priority_queue(最大元素先出)

 #include <iostream>
#include <queue>
using namespace std; int main()
{ priority_queue<int> pq; pq.push();
pq.push();
pq.push();
pq.push();
pq.push();
pq.push(); cout << "size: " << pq.size() << endl; while(pq.empty() != true)
{
cout << pq.top() << endl;
pq.pop();
}
return ;
}

  重载操作符同set重载操作符。

原文地址:http://www.cnblogs.com/duoduo369/archive/2012/04/12/2439118.html

C++STL库常用函数用法的更多相关文章

  1. 使用STL库sort函数对vector进行排序

    使用STL库sort函数对vector进行排序,vector的内容为对象的指针,而不是对象. 代码如下 #include <stdio.h> #include <vector> ...

  2. Python math库常用函数

    math库常用函数及举例: 注意:使用math库前,用import导入该库>>> import math 取大于等于x的最小的整数值,如果x是一个整数,则返回x>>> ...

  3. Matplotlib库常用函数大全

    Python之Matplotlib库常用函数大全(含注释) plt.savefig(‘test’, dpi = 600) :将绘制的图画保存成png格式,命名为 test plt.ylabel(‘Gr ...

  4. C++STL 常用 函数 用法

    学完c++快一年了,感觉很有遗憾,因为一直没有感觉到c++的强大之处,当时最大的感觉就是这个东西的输入输出比C语言要简单好写. 后来我发现了qt,opencv,opengl,原来,c++好玩的狠. 在 ...

  5. C++中string常用函数用法总结

    string(s小写)是C++标准库中的类,纯C中没有,使用时需要包含头文件#include<string>,注意不是<string.h>,下面记录一下string中比较常用的 ...

  6. Python之Numpy库常用函数大全(含注释)

    前言:最近学习Python,才发现原来python里的各种库才是大头! 于是乎找了学习资料对Numpy库常用的函数进行总结,并带了注释.在这里分享给大家,对于库的学习,还是用到时候再查,没必要死记硬背 ...

  7. Python之Numpy库常用函数大全(含注释)(转)

    为收藏学习,特转载:https://blog.csdn.net/u011995719/article/details/71080987 前言:最近学习Python,才发现原来python里的各种库才是 ...

  8. 彻底弄清c标准库中string.h里的常用函数用法

    在我们平常写的c/c++程序,一些算法题中,我们常常会用到c标准库中string.h文件中的函数,这些函数主要用于处理内存,字符串相关操作,是很有用的工具函数.而且有些时候,在笔试或面试中也会出现让你 ...

  9. C++STL 常用 函数 用法(转)

    http://www.cnblogs.com/duoduo369/archive/2012/04/12/2439118.html 迭代器(iterator) 个人理解就是把所有和迭代有关的东西给抽象出 ...

随机推荐

  1. C\C++中的 struct 关键字详解

    struct关键字是用来定义一个新的类型,这个新类型里面可以包含各种其他类型,称为结构体. 1. 什么是结构体 结构体(struct)是一种自定义的数据类型,就是把一组需要在一起使用的数据元素组合成一 ...

  2. CSRF利用

    使用burpsuite的csrf poc选项,可以生成HTML代码 json CSRF flash + 307跳转 https://github.com/sp1d3r/swf_json_csrf

  3. shell脚本执行出现“期待整数表达式”

    在执行shell时一直出现“integer expression expected”,找了很久也没发现那个地方出错.翻了笔记发现-le并不错啊,甚至还怀疑零是不是整数还特意上网搜了下 -_- . 最后 ...

  4. Reactor系列(十)collectMap集合

    #java#reactor#collect#hashMap# 转换成Map 视频讲解: https://www.bilibili.com/video/av80048104/ FluxMonoTestC ...

  5. JavaSE基础(七)--Java流程控制语句之switch case 语句

    Java switch case 语句 switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支. 语法 switch case 语句语法格式如下: switch(exp ...

  6. go 二进制数据处理

    以下是利用标准库binary来进行编解码 编码 ①使用bytes.Buffer来存储编码生成的串②使用binary.Write来编码存储在①的buf中 package main import ( &q ...

  7. java动态更新枚举类

    工作中遇到需要对枚举类的值进行动态更新 手动改不现实也不方便 现记录下来方便以后学习使用 1.在工程utils包中添加动态更新枚举类得工具类(根据自己得项目,放到指定位置调用就可以) 2.一开始陷入了 ...

  8. 1.3.3 并发容器类MAP/LIST/SET/QUEUE

    HashMap 下标计算方法:hashCode & (length-1) ,&按位与操作,二进制相同位都是1,该位才为1 JDK1.7与JDK1.8中HashMap区别: JDK1.8 ...

  9. mybatis缓存机制与装饰者模式

    mybatis 缓存 MyBatis的二级缓存的设计原理 装饰者模式

  10. poi 3061 尺取例题1

    题目传送门/res tp poj 白书题 尺取法例题 #include<iostream> #include<algorithm> using namespace std; c ...