ACM中常用的STL函数总结
2016-08-21 19:48
309 查看
Vector部分
Stack部分
stack 模板类的定义在头文件中。
stack 模板类需要两个模板参数,一个是元素类型,一个容器类型,但只有元素类型是必要
的,在不指定容器类型时,默认的容器类型为deque。
定义stack 对象的示例代码如下:
stack s1;
stack s2;
stack 的基本操作有:
入栈,如例:s.push(x);
出栈,如例:s.pop();注意,出栈操作只是删除栈顶元素,并不返回该元素。
访问栈顶,如例:s.top()
判断栈空,如例:s.empty(),当栈空时,返回true。
访问栈中的元素个数,如例:s.size()。
Queue部分
queue 模板类的定义在头文件中。
与stack 模板类很相似,queue 模板类也需要两个模板参数,一个是元素类型,一个容器类
型,元素类型是必要的,容器类型是可选的,默认为deque 类型。
定义queue 对象的示例代码如下:
queue q1;
queue q2;
queue 的基本操作有:
入队,如例:q.push(x); 将x 接到队列的末端。
出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
访问队首元素,如例:q.front(),即最早被压入队列的元素。
访问队尾元素,如例:q.back(),即最后被压入队列的元素。
判断队列空,如例:q.empty(),当队列空时,返回true。
访问队列中的元素个数,如例:q.size()
Priority_queue部分
优先队列和队列中的操作指令基本上差不多,注意优先队列是默认大的数在前面,若是想定义小的数在前面只需要像下面这样定义即可,需要知道的是优先队列是通过堆来实现的
关于优先队列优先级的定义,可以参考
博客链接
Set用法
map用法
pair用法
pair
2)pair对象的操作,支持== 、< ,first、second成员的访问,如下
3)生成新的pair对象,如下:
同样地pair也可以创建数组,这里就不多演示了
#include <cstdio> #include <iostream> #include <queue> #include <algorithm> using namespace std; int main(){ vector<int> a; //向vector的末尾添加元素 for(int i=1;i<10;i++) a.push_back(i); //输出首元素 cout << a.front()<<endl; //输出尾元素 cout << a.back()<<endl; //利用指针和begin函数和end函数进行遍历 vector<int>::iterator it; for(it=a.begin();it!=a.end();it++) cout << *it << " "; cout <<endl; //使用size函数进行遍历 int len=a.size(); for(int i=0;i<len;i++) cout << a[i] << " "; cout <<endl; //删除队尾元素 a.pop_back(); cout << a.back()<<endl; //通过erase函数删除第二个元素 it=a.begin()+1; a.erase(it);//删除的是2 len=a.size(); for(int i=0;i<len;i++) cout << a[i] << " "; cout <<endl; //通过erase函数删除前两个元素 vector<int>::iterator it2; it=a.begin(); it2=a.begin()+2;//删除的是1,3(注意这里不删除it2指向的那个位置) a.erase(it,it2);//就是说删除[it,it2)的部分 len=a.size(); for(int i=0;i<len;i++) cout << a[i] << " "; cout <<endl; //清空整个vector a.clear(); //判断是否为空 if(a.empty()) cout << "这个vector现在是空的了" <<endl; //向指针指向的元素的前面插一个元素进去 for(int i=1;i<10;i++) a.push_back(i); it=a.begin(); a.insert(it,-1);//这样的插入是可以返回插入元素的指针的,而其他的插入方式就不行 len=a.size(); for(int i=0;i<len;i++) cout << a[i] << " "; cout <<endl; //向指针指向的元素的前面插4个相同的元素进去 it=a.begin()+1; a.insert(it,4,100); len=a.size(); for(int i=0;i<len;i++) cout << a[i] << " "; cout <<endl; //使用sort函数 sort(a.begin(),a.end()); //完整交换两个不同的vector vector<int> a2; for(int i=1;i<10;i++) a2.push_back(i); a.swap(a2); len=a.size(); for(int i=0;i<len;i++) cout << a[i] << " "; cout <<endl; len=a2.size(); for(int i=0;i<len;i++) cout << a2[i] << " "; cout <<endl; return 0; }
Stack部分
stack 模板类的定义在头文件中。
stack 模板类需要两个模板参数,一个是元素类型,一个容器类型,但只有元素类型是必要
的,在不指定容器类型时,默认的容器类型为deque。
定义stack 对象的示例代码如下:
stack s1;
stack s2;
stack 的基本操作有:
入栈,如例:s.push(x);
出栈,如例:s.pop();注意,出栈操作只是删除栈顶元素,并不返回该元素。
访问栈顶,如例:s.top()
判断栈空,如例:s.empty(),当栈空时,返回true。
访问栈中的元素个数,如例:s.size()。
#include <cstdio> #include <iostream> #include <stack> #include <algorithm> using namespace std; int main(){ stack<int> a; //入栈操作 a.push(1); a.push(2); a.push(3); //取栈顶元素输出 cout << a.top() << endl; //删除栈顶元素 a.pop(); //再次取栈顶元素 cout << a.top() << endl; //判断栈是否为空 if(!a.empty()) cout << "栈不为空" << endl; return 0; }
Queue部分
queue 模板类的定义在头文件中。
与stack 模板类很相似,queue 模板类也需要两个模板参数,一个是元素类型,一个容器类
型,元素类型是必要的,容器类型是可选的,默认为deque 类型。
定义queue 对象的示例代码如下:
queue q1;
queue q2;
queue 的基本操作有:
入队,如例:q.push(x); 将x 接到队列的末端。
出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
访问队首元素,如例:q.front(),即最早被压入队列的元素。
访问队尾元素,如例:q.back(),即最后被压入队列的元素。
判断队列空,如例:q.empty(),当队列空时,返回true。
访问队列中的元素个数,如例:q.size()
#include <cstdio> #include <iostream> #include <queue> #include <algorithm> using namespace std; int main(){ queue<int> a; //插入元素 a.push(2); a.push(1); a.push(3); a.push(4); //访问队列首元素,注意这里top是用不成的 cout << a.front()<<endl; //访问队列尾元素 cout << a.back()<<endl; //删除首元素,但是他不返回值 a.pop(); cout << a.front()<<endl; //判断队列是否为空 if(!a.empty()) cout << "队列不为空" <<endl; //输出队列中的元素个数 cout << a.size() << endl; return 0; }
Priority_queue部分
优先队列和队列中的操作指令基本上差不多,注意优先队列是默认大的数在前面,若是想定义小的数在前面只需要像下面这样定义即可,需要知道的是优先队列是通过堆来实现的
priority_queue<int,vector<int>,greater<int>> a;
#include <cstdio> #include <iostream> #include <queue> #include <algorithm> using namespace std; int main(){ priority_queue<int> a; //插入元素 a.push(2); a.push(1); a.push(3); a.push(4); //访问队列首元素,注意这里不是像队列一样使用front和back来访问首元素和尾元素 cout << a.top()<<endl; //删除首元素,但是他不返回值 a.pop(); cout << a.top ()<<endl; //判断队列是否为空 if(!a.empty()) cout << "队列不为空" <<endl; //输出队列中的元素个数 cout << a.size() << endl; return 0; }
关于优先队列优先级的定义,可以参考
博客链接
Set用法
#include <cstdio> #include <iostream> #include <set> #include <algorithm> using namespace std; int main(){ set<int> s; s.insert(1); s.insert(3); s.insert(5); //查找元素 set<int>::iterator it; it=s.find(1); if(it==s.end()) puts("not found"); else puts("found"); //输出found it=s.find(2); if(it==s.end()) puts("not found"); else puts("found"); //输出not found //删除元素 s.erase(3); //其他的查找元素的方法 if(s.count(3)!=0) puts("found"); else puts("not found");//输出 not found //遍历所有元素 for(it=s.begin();it!=s.end();it++){ printf("%d\n",*it); } return 0; }
map用法
#include <cstdio> #include <iostream> #include <map> #include <algorithm> using namespace std; int main(){ map<int,const char *> m; //插入元素 m.insert(make_pair(1,"ONE")); m.insert(make_pair(10,"TEN")); //标准的写法 m[100]="HUNDRED"; //另一种插入元素的写法 //查找元素 map<int,const char*>::iterator it; it=m.find(1); puts(it->second); //输出ONE it=m.find(2); if(it==m.end()) puts("not found"); else puts("found"); //输出not found puts(m[10]); //其他的输出方式 //删除元素 m.erase(10); //遍历一遍所有的元素 for(it = m.begin();it!=m.end();it++) printf("%d: %s",it->first,it->second); return 0; }
pair用法
pair
pair<string, string> anon;//调用默认构造函数来初始化 pair<string, int> word_count; pair<string, vector<int>> line; pair<string, string> author("jim", "weshon"); //定义时提供初始化 typedef pair<string, string> Author; Author product("marcel", "Product");
2)pair对象的操作,支持== 、< ,first、second成员的访问,如下
string firstbook; if (author.first == "jim" && author.second == "weshon") { firstbook = "stephon hero"; // }
3)生成新的pair对象,如下:
pair<string, string> next_author; string first, second; while (cin >> first >> second) { next_author = make_pair(first, second); //pair<string, string> next_author(first, second); //与上面创建类似 }
同样地pair也可以创建数组,这里就不多演示了
相关文章推荐
- C++:STL常用函数模块总结(set)
- C++:STL常用函数模块总结(算法algorithm)
- 总结一些在acm中常用的小技巧,小函数
- C++:STL常用函数模块总结(vector)
- STL---对STL中的各类常用函数的小总结
- STL常用函数总结
- STL常用容器及其常用函数总结(避免记混了SB)
- ACM等算法比赛中JAVA 常用"STL"总结:TreeMap,Queue,PriorityQueue等
- postgis常用函数总结(二)
- jQuery中常用的函数方法总结
- 水晶报表常用函数总结(不断更新中.......)
- SQL 常用函数总结
- 今天总结了几个常用函数比较相信对初学者有用?
- stl标准容器的常用函数
- [学习总结]SQL SERVER 2005常用系统函数
- postgis常用函数总结(一)
- Sql常用函数总结
- C语言中几个常用的函数总结
- 常用的几个STL::string的扩展函数
- STL知识点(常用算法函数介绍 、容器、类属算法、迭代器、函数对象、适配器、分配器