2017 10 1国庆培训七天总结(提高组),第一天(STL)。
国庆参加了学校组织的集训,第一天的内容还是比较简单,但是第一天出的题目和早上讲的内容没有关系,早上讲的内容现在进行总结,早上就讲了一个内容就是STL没有讲算法。下面对STL进行介绍:
1.vector:
#include <vector> //头文件
vector<变量类型(可以为自己定义的结构体)> v; //定义名叫v的vector
v.push_back(1); //在末尾添加1
v.erase(迭代器的位置); //删除当前迭代器的元素
v[0] = 0; //让第0个元素变为0;
v.size(); //获得容器大小
v.begin(); //获得开始的迭代器
v.end(); //获得末尾的迭代器
#include <cstdio> #include <vector> #include <iostream> using namespace std; vector <int > v; int main(){ printf("size:%d\n", v.size()); v.push_back(1); v.push_back(2); v.push_back(3); printf("size:%d\n", v.size()); vector<int>::iterator it; // 定义迭代器 for (it = v.begin();it != v.end();it ++) printf("%d ", *it);// vector的遍历 v[0] = 100;// 将第0个元素改为100 v.erase(v.end() - 1);// 删除末尾元素 printf("\nsize:%d\n", v.size()); for (it = v.begin();it != v.end();it ++) printf("%d ", *it); printf("\nsize:%d\n", v.size()); for (int i = 0;i < v.size();i ++) printf("%d ", v[i]); // vector的另一种方式遍历 }
2.string;
#include <string> //
string a, b; //定义名叫a , b 的两个字符串
a.size(); //获取大小
a.length(); //获取长度(和获取大小等价)
cin >> a; //输入只能用cin
cout << a; //输出只能用cout
b = "123456465"; //对b进行赋值
a = a + b; //进行加法直接将b接在a后面
if (a <= b) printf("YES") //比较大小
#include <cstdio> #include <string> #include <iostream> using namespace std; string a, b, c; int main(){ cout << "Tips:Please Input a and b" << endl; cin >> a >> b; //输入a和b int lena = a.length(); //获取a的长度 int lenb = b.length(); //获取b的长度 c = a + b; //将a+b赋值给c int lenc = c.length(); //获取c的长度 cout << "a length:" << lena << endl; cout << "a:" << a << endl; //输出a cout << "b length:" << lenb << endl; cout << "b:" << b << endl; //输出b cout << "c length:" << lenc << endl; cout << "c:" << c << endl; //输出c if (a == b) cout << "YES" << endl; //对a和b进行比较 else cout << "NO" << endl; for (int i = 0;i < lenc;i ++) cout << c[i] << endl; //对c进行单个字符遍历 return 0; }
3. queue;
#include <queue> //头文件
queue<类型名(可以是自己定义的结构体)> q;
q.push() //在队位插入元素
q.pop() //弹出队首元素
q.front() //获得队首元素的值
q.size() //获得队列大小
q.empty() //判断是否为空 1 为空 0 为不空
#include <queue> #include <iostream> using namespace std; queue <int > q; int main(){ q.push(1); //插入元素 q.push(2); q.push(3); q.push(4); cout << "Queue's size:" << q.size() << endl; cout << "Queue's number:"; while(!q.empty()){ //遍历每个元素 cout << q.front() << " "; q.pop(); } //遍历后队列为空 cout << endl << "Now the queue's size:" << q.size() << endl; return 0; }
4.set;
#include <set>
set <变量类型(可以是结构体)> s;
s.insert(); //插入元素
s.size(); //获得大小
s.begin(); //开始的迭代器
s.end(); //结束的迭代器
s.find(); //查找元素迭代器
s.count(); //判断是否存在元素x若存在返回1,否则返回0
s.erase(); //删除迭代器所处位置的元素
/* set利用了一种数据结构叫做红黑二叉树,在set中不会出现重复的元素,所以count函数返回1或0,这符合高中所讲集合的互异性。 */ #include <set> #include <iostream> using namespace std; set<int> s; int main(){ s.insert(4); s.insert(3); //插入元素 s.insert(3); cout << s.size() << endl; //输出大小 set<int> :: iterator it; for (it = s.begin();it != s.end();it ++) cout << *it << endl; //遍历集合 cout << *s.find(4) << endl; //输出4所在的迭代器对应的元素,实际就是4 cout << s.count(3) << endl << s.count(5) << endl; s.erase(s.begin()); //删除开始的元素 cout << s.count(3); return 0; }
5.priority_queue
#inclued<queue>//头文件
priority_queue<变量类型可以使结构体> q;
q.push();//插入元素
q.top();//取出队(按照从大到小)首元素
q.size();//获取大小
q.empty();//判断是否为空
q.pop();//弹出队首元素
/* priority_queue运用了一种树形结构,可以将数字插入后按照从大到小的顺序取出 */ #include <queue> #include <iostream> using namespace std; priority_queue<int> q; int main(){ q.push(1); q.push(4); q.push(5); // 插入元素 cout << q.size() << endl;//输出大小 while(!q.empty()){ cout << q.top() << " ";//遍历 q.pop(); } q.push(100); cout << endl << q.size() << endl; }
特别的:
priority_queue运用广泛如贪心题目合并果子,可以将priority_queue重载为取出最小的元素来解决,是复杂度降为(nlogn)又或者是dijkstra算法求最短路运用小顶堆可以实现,如果关于将大顶堆转换为小顶堆,给出两种方式的代码:
/* 1比较推荐 */ #include <queue> #include <iostream> using namespace std; struct node{ int val; }; bool operator < (const node a,const node b){return a.val > b.val;} priority_queue<node> q; int main(){ node a; a.val = 5; q.push(a); a.val = 1; q.push(a); cout << q.top().val << endl; } /* 2不支持结构体,但支持pair,别问我为什么这样写 */ #include <queue> #include <vector> #include <iostream> using namespace std; priority_queue<int, vector<int>, greater<int> > q; int main(){ q.push(5); q.push(1); cout << q.top() << endl; }
给出单元最短路的算法:哈哈哈
sort()将与归并排序一起介绍。