开学就要上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

最新文章

  1. Android 更新UI的几种方式
  2. bzoj4404: [Neerc2015]Binary vs Decimal
  3. day4----装饰器
  4. [置顶] 自己写代码生成器之生成Dal层代码(获取数据库所有表名称)
  5. 【&#127568;】 彻底理解webservice SOAP WSDL
  6. WIN2003 设置 OPENVPN 服务端
  7. Vue和Bootstrap的整合之路
  8. Oracle删除重复行
  9. Go开发之路 -- 函数详解
  10. 解决app安装成功后,直接点击“打开”再按home返回,再次打开app会重新启动的问题
  11. 使用redis限制ip访问次数
  12. AVL树的JAVA实现及AVL树的旋转算法
  13. Docker服务端和客户端
  14. android 开发 写一个RecyclerView布局的聊天室,并且添加RecyclerView的点击事件
  15. How develop BigData Project in Visual Studio
  16. mvc4 初体验(一)
  17. vmware Esxi 回收Thin模式磁盘空间
  18. 第10章-Vue.js 项目实战
  19. VC++ 知识点
  20. mysql前缀索引优化示例

热门文章

  1. Java基础——接口和抽象类
  2. Linux 下清空或删除大文件内容的 5 种方法
  3. 【Qt开发】关于QWSServer
  4. Java学习笔记-包,classpath,import,jar
  5. eNSP——交换机基础配置
  6. 【AtCoder】CODE FESTIVAL 2016 qual A
  7. pycharm配置git版本管理
  8. vue的基本语法
  9. python3.5+installer 将.py 打包成.exe
  10. selenium登录百度