stl容器
vector
略。
set
/**
*set简单示例。
*
*所用函数主要为:
*set::insert();插入
*set::begin();返回头指针(或是迭代器);
*set::end();返回尾指针(或是迭代器)(下一个空地址的开始位置);
*iterator 迭代器,因为不清楚,所以这样不予详细解释。
**/
#include<bits/stdc++.h>
using namespace std;
set<int> dict;//string 集合;
int main(){
int s,buf;
while(cin>>s){
dict.insert(s);
}
set<int>::iterator it;
for(it= dict.begin(); it!= dict.end(); it++){
cout<<*it<<endl;
}
return 0;
}
/**
*set的结构体示例
*
*所用函数和上个基本相同,
*同之处为需要自己家指定自己定义的结构体的大小,
*不同之处需将比较函数指针放入set的类模板之中。
*思考原理应该是c++的重载,或是其他,这里不进行讨论。
**/
#include<bits/stdc++.h>
using namespace std;
struct Student
{
int id;
string name;
};
struct SetCmp
{
bool operator()(Student a,Student b)
{
return a.id<b.id;
}
};
int main()
{
set<Student,SetCmp>mys;
set<Student>::iterator it;
Student a1,a2,a3;
a1.id=100;
a1.name="lihua";
a2.id=200;
a2.name="wangming";
a3.id=100;
a3.name="lihua";
mys.insert(a1);
mys.insert(a2);
mys.insert(a3);
//set<>::iterator it;
for(it= mys.begin(); it!= mys.end(); it++)
cout<<it->id<<" "<<it->name<<endl;
}
map
/**
*map部分用法的简单示例
*其中map保存键值和映射的内容,就像x和y的关系。
*一个x只能对应一个y值。
*可通过“map名【键值】”来访问映射的值。
*map::find(),返回迭代器,用法类似指针。
*map::count(),返回下标值,用法与数组相同。
*map::size(),返回map里面有多少数据元素。
**/
#include<bits/stdc++.h>
using namespace std;
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};
int main()
{
map<const char*, int, ltstr> months;
months["january"] = 31;
months["february"] = 28;
months["march"] = 31;
months["april"] = 30;
months["may"] = 31;
months["june"] = 30;
months["july"] = 31;
months["august"] = 31;
months["september"] = 30;
months["october"] = 31;
months["november"] = 30;
months["december"] = 31;
cout << "june -> " << months["june"] << endl;
map<const char*, int, ltstr>::iterator cur = months.find("june");
map<const char*, int, ltstr>::iterator prev = cur;
map<const char*, int, ltstr>::iterator next = cur;
++next;
--prev;
cout << "Previous (in alphabetical order) is " << (*prev).first << endl;
cout << "Next (in alphabetical order) is " << (*next).first << endl;
}
stck and queue
使用标准库的栈和队列时,先包含相关的头文件
#include<stack>
#include<queue>
定义栈如下:
stack<int> stk;
定义队列如下:
queue<int> q;
栈提供了如下的操作
[cpp] view plain copy
s.empty() 如果栈为空返回true,否则返回false
s.size() 返回栈中元素的个数
s.pop() 删除栈顶元素但不返回其值
s.top() 返回栈顶的元素,但不删除该元素
s.push() 在栈顶压入新元素
队列提供了下面的操作
[cpp] view plain copy
q.empty() 如果队列为空返回true,否则返回false
q.size() 返回队列中元素的个数
q.pop() 删除队列首元素但不返回其值
q.front() 返回队首元素的值,但不删除该元素
q.push() 在队尾压入新元素
q.back() 返回队列尾元素的值,但不删除该元素
/**
*C++ Priority Queues(优先队列)
C++优先队列类似队列,但是在这个数据结构中的元素按照一定的断言排列有序。
1.empty() 如果优先队列为空,则返回真
2.pop() 删除第一个元素
3.push() 加入一个元素
4.size() 返回优先队列中拥有的元素的个数
5.top() 返回优先队列中有最高优先级的元素
*优先级队列可以用向量(vector)或双向队列(deque)来实现(注意list container 不能用来实现queue,因为list 的迭代器不是任意存取iterator,而pop 中用到堆排序时是要求randomaccess iterator 的!):
*priority_queue<vector
*priority_queue<deque
*其成员函数有“判空(empty)” 、“尺寸(Size)” 、“栈顶元素(top)” 、“压栈(push)” 、“弹栈(pop)”等。
**/
//例:
#include <iostream>
#include <queue>
using namespace std;
class T {
public:
int x, y, z;
T(int a, int b, int c):x(a), y(b), z(c)
{
}
};
bool operator < (const T &t1, const T &t2)
{
return t1.z < t2.z; // 按照z的顺序来决定t1和t2的顺序
}
main()
{
priority_queue<T> q;
q.push(T(4,4,3));
q.push(T(2,2,5));
q.push(T(1,5,4));
q.push(T(3,3,6));
while (!q.empty())
{
T t = q.top();
q.pop();
cout << t.x << " " << t.y << " " << t.z << endl;
}
return 1;
}

浙公网安备 33010602011771号