回顾:
泛化编程 day01
泛化编程 day02
1)STL的概念全称为Standard Template Library(标准模板库)
2)STL的作用
3)数组和链表优缺点
4)十大容器
5)容器的分类
6)容器的共同特点
==
进行相等性判断1)成员函数
2)向量的初始化
eg:01vectorinit.cpp
#include
#include
using namespace std;
void print(string const& str, vector<int>& v)
{
cout << str << endl;
cout << "向量的容量:" << v.capacity() << endl;
for(size_t i=0; i<v.capacity(); i++){
cout << v[i] << " ";
}
cout << endl;
cout << "向量的大小:" << v.size() << endl;
for(size_t i=0; i<v.size(); i++){
cout << v[i] << ' ';
}
cout << endl << "------------" <<endl;
}
int main(void)
{
vector<int> v1;//沒有維護任何的內存空間
print("v1:",v1);
vector<int> v2(10);//向量容量爲10,大小也爲10,存儲的數據都爲0
print("v2:", v2);
vector<int> v3(10, 5);//向量容量爲10,大小也爲10,存儲的數據都爲5
print("v3:", v3);
v3.push_back(100);//增加一個數據容量,容量增加不止一個
print("after v3 add a new data:", v3);
v3.pop_back();
print("after v3 delete a data:", v3);
v3.push_back(500);
print("v3 add data again:", v3);
return 0;
}
#include
#include
#include
using namespace std;
class Student
{
public:
Student(string const& name="", int const& age=0):
m_name(name), m_age(age){
cout << "缺省構造了:" << m_name << "(" << this << ")" << endl;
}
Student(Student const& that):m_name(that.m_name), m_age(that.m_age){
cout << "用" << that.m_name << "(" << &that << ")" << "拷貝構造了:"
<< m_name << "(" << this << ")" << endl;
}
~Student(){
cout << "析构了:" << m_name << "(" << this << ")" << endl;
}
private:
string m_name;
int m_age;
};
int main(void)
{
vector<Student> vs;
vs.reserve(10);
vs.push_back(Student("Adair"));
vs.push_back(Student("Rising"));
vs.push_back(Student("weeks"));
getchar();
return 0;
}
++
和--
运算;++
和--
,也支持对整数的加减运算,除了向量和双端队列以及优先队列随即迭代器以外,其余容器只支持顺序迭代器;最后一个元素的下一个位置
,增操作向容器的尾部移动,减操作向容器的首部移动;第一个元素的前一个位置
,增操作向容器的首部移动,减操作向容器的尾部移动。eg:01vector.cpp
#include
#include
#include
using namespace std;
template<class T>
void print(string const& str, vector<T>& v){
cout << str << endl;
typedef typename vector<T>::iterator IT;
for(IT it=v.begin(); it!=v.end(); ++it)
cout << *it << ' ';
cout << endl << "---------" << endl;
}
class CMP
{
public:
bool operator()(int const& a, int const& b)const{
return a < b;
}
};
int main(void)
{
vector<int> vi;
for(int i=0; i<10; i++){
vi.push_back(10-i);
}
print("添加节点后:", vi);
vi.insert(vi.begin(), 300);
print("在迭代器指向位置添加节点后:", vi);
vi.erase(vi.begin());
print("删除迭代器指向的节点后:",vi);
typedef vector<int>::iterator IT;
IT fit = find(vi.begin(), vi.end(), 5);
if(fit!=vi.end())
vi.erase(fit);
print("找到元素5并删除后:", vi);
//sort(vi.begin(), vi.end());//内部用“<”s实现排序
CMP cmp;
sort(vi.begin(), vi.end(), cmp);//内部用比较器(cmp)排序
print("排序后:", vi);
return 0;
}
eg:02vector.cpp
include <iostream>
#include
#include
using namespace std;
class Student
{
public:
Student(string const& name="",int const& age=0):m_name(name),m_age(age){
}
friend ostream& operator<<(ostream& os, Student const& s){
os << s.m_name << ","<< s.m_age;
return os;
}
bool operator==(Student const& that){
return m_name==that.m_name && m_age==that.m_age;
}
bool operator<(Student const& that){
return m_age < that.m_age;
}
private:
string m_name;
int m_age;
};
template<class T>
void print(string const& str, vector<T>& v){
cout << str << endl;
typedef typename vector<T>::iterator IT;
for(IT it=v.begin(); it!=v.end(); ++it)
cout << *it << ' ';
cout << endl << "---------" << endl;
}
emplate<class T>
class CMP
{
public:
bool operator()(T const& a, T const& b)const{
return a < b;
}
/*
public:
bool operator()(int const& a, int const& b)const{
return a < b;
}
bool operator()(Student const& a, Student const& b)const{
return a < b;
}
*/
};
int main(void)
{
/*
vector vi;
for(int i=0; i<10; i++){
vi.push_back(10-i);
}
print("添加节点后:", vi);
vi.insert(vi.begin(), 300);
print("在迭代器指向位置添加节点后:", vi);
vi.erase(vi.begin());
print("删除迭代器指向的节点后:",vi);
typedef vector::iterator IT;
IT fit = find(vi.begin(), vi.end(), 5);
if(fit!=vi.end())
vi.erase(fit);
print("找到元素5并删除后:", vi);
//sort(vi.begin(), vi.end());//内部用“<”s实现排序
CMP cmp;
sort(vi.begin(), vi.end(), cmp);//内部用比较器(cmp)排序
print("排序后:", vi);
*/
vector<Student> vs;
vs.push_back(Student("weeks", 28));
vs.push_back(Student("adair", 25));
vs.push_back(Student("rising", 26));
vs.push_back(Student("zhou", 23));
vs.push_back(Student("ten", 28));
vs.push_back(Student("cent", 29));
print("添加节点后:", vs);
vs.insert(vs.begin(), Student("da", 25));
print("在迭代器制定的位置添加节点后:", vs);
vs.erase(vs.begin());
print("删除迭代器指向的节点后:", vs);
typedef vector<Student>::iterator IT;
IT fit = find(vs.begin(), vs.end(), Student("zhou", 23));
if(fit!=vs.end())
vs.erase(fit);
print("找到zhou并删除后:", vs);
//sort(vs.begin(), vs.end());
CMP<Student> cmp;
sort(vs.begin(), vs.end(), cmp);
print("排序后:",vs);
return 0;
}
template<class IT, class value_type>
IT find(IT begin, IT end,value_type& key)//成功返回第一个匹配元素的迭代器,失败返回第二个参数
//使用内部的sort函数
template<class IT>void sort(IT begin, IT end);
//自定义比较器比较
template<class IT, class LESS>
void sort(IT begin, IT end, LESS LSEE cmp);
eg:03deque.cpp
#include
#include
#include
using namespace std;
template<class T>
void print(string const& str, deque<T>& d){
cout << str << endl;
typedef typename deque<T>::iterator IT;
for(IT it=d.begin(); it!=d.end(); ++it){
cout << *it << ' ';
}
cout << endl << "-----------" << endl;
}
template<class T>
class CMP
{
public:
bool operator()(T const& a, T const& b){
return a > b;
}
};
int main(void)
{
deque<int> di;
for(int i=0; i<5; i++){
di.push_front(10-i);
}
for(int i=0; i<5; i++){
di.push_back(11+i);
}
print("添加节点后:", di);
di.pop_front();
di.pop_back();
print("删除头尾节点后:", di);
di.insert(++di.begin(), 500);
print("在迭代器指向的位置添加节点后:",di);
di.erase(di.begin());
print("删除迭代器指向的节点后:", di);
typedef deque<int>::iterator IT;
IT fit = find(di.begin(), di.end(), 10);
if(fit!=di.end())
di.erase(fit);
print("找到元素10并删除后:",di);
sort(di.begin(), di.end());
print("排序后:", di);
CMP<int> cmp;
sort(di.begin(), di.end(), cmp);
print("比较器排序后:", di);
return 0;
}
void unique(void);//将连续重复出现的元素唯一化
列表的sort()是成员函数
,并且是全局排序不能提供排序的范围void sort(void);//通过<比较大小
template<class LESS>void sort(LESS less);//通过比较器比较大小
template<class IT>void splice(IT pos, list& lst);
template<class IT>void splice(IT pos, list& lst, IT del);
template<class IT>void splice(IT pos, list& lst, IT begin, IT end);
eg:04list.cpp
include <iostream>
#include
using namespace std;
template<class T>
void print(string const& str, list<T>& l){
cout << str << endl;
typedef typename list<T>::iterator IT;
for(IT it=l.begin(); it!=l.end(); ++it){
cout << *it << ' ';
}
cout << endl << "----------------" << endl;
}
template<class T>class CMP
{
public:
bool operator()(T const& a, T const& b)const{
return a > b;
}
};
int main(void)
{
list<int> ls;
for(int i=0; i<5; i++){
ls.push_front(10+i);
}
for(int i=0; i<5; i++){
ls.push_back(10-i);
}
print("添加节点后:", ls);
ls.unique();
print("唯一化后:", ls);
ls.sort();
print("排序后:", ls);
CMP<int> cmp;
ls.sort(cmp);
print("比较器排序后:", ls);
list<int> lst;//作为参数列表
ls.push_back(1000);
ls.push_back(2000);
ls.push_back(3000);
ls.push_back(4000);
//ls.splice(ls.begin(), lst);
//ls.splice(++ls.begin(), lst, ++lst.begin());
ls.splice(ls.begin(), lst, lst.begin(), --lst.end());
print("ls:", ls);
print("lst:", lst);
return 0;
}
stack<元素类型,[底层容器类型]>堆栈对象(构造实参表)
eg:05stack.cpp
#include
#include
#include
#include
#include
using namespace std;
int main(void)
{
//stack > s;
//stack > s;
stack<int, list<int> > s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
s.push(6);
while(!s.empty()){
cout << s.top() << endl;
s.pop();
}
return 0;
}
queue<元素类型,[底层容器类型]>队列队形(构造实参表)
eg:06queue.cpp
#include
#include
#include
#include
#include
using namespace std;
int main(void)
{
queue<int> q;
//queue > q;//vector不能作为队列的底层容器
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.push(5);
q.push(6);
while(!q.empty()){
cout << q.front() << endl;
q.pop();
}
return 0;
}
priority_queue<元素类型,[底层容器类型],[比较器类型]>优先队列对象(构造实参表)
<
运算符。而且优者先出并不是出队时挑选优者,而是进队列时就保证有序。eg:07priorityqueue.cpp’
#include
#include
#include
#include
using namespace std;
class CMP
{
public:
bool operator()(int const& a, int const& b)const{
return a > b;
}
};
int main(void)
{
priority_queue<int, vector<int>, CMP> pq;
pq.push(5);
pq.push(9);
pq.push(2);
pq.push(8);
pq.push(1);
pq.push(6);
pq.push(3);
pq.push(7);
pq.push(0);
while(!pq.empty()){
cout << pq.top() << endl;
pq.pop();
}
return 0;
}
map<键类型,值类型>映射对象;
O(logN)
),类似二分法template<class FIRST, class SECOND>class pair{
public:
pair(FIRST const& f, SECOND const& s):first(f), second(s){
}
FIRST first;//键
SECOND second;//值
};
映射的迭代器相当于指向pair对象的指针。
下标
运算,用键作为下标,得到队形的值得引用,如果所给出的键不存在,增加一个节点,返回其值的引用。insert(pair<FIRST,SECOND>(key, value))
insert(make_pair(key,value))
迭代器=find(key)
失败返回终止迭代(并非全局函数而是成员函数)
eg:08map.cpp
#include
#include
using namespace std;
class Candidate
{
public:
Candidate(string const& name=""):m_name(name), m_vote(0){
}
string GetName(void){
return m_name;
}
int GetVote(void){
return m_vote;
}
void SetVote(void){
++m_vote;
}
private:
string m_name;
int m_vote;
};
void print(map<char, Candidate>& m){
typedef map<char, Candidate>::iterator IT;
for(IT it=m.begin(); it!=m.end(); ++it){
cout << "(" << (*it).first << ")" << (*it).second.GetName() << ' ';
}
cout << endl;
}
int main(void)
{
map<char, Candidate> m;
m.insert(pair<char, Candidate>('A', Candidate("张飞")));
m.insert(make_pair('B', Candidate("赵云")));
m['C']=Candidate("关羽");
m['D']=Candidate("马超");
m['E']=Candidate("黄忠");
typedef map<char, Candidate>::iterator IT;
for(int i=0; i<10; i++){
print(m);
char ch;
cin >> ch;
IT fit = m.find(ch);
if(fit==m.end()){
cout << "废票" << endl;
continue;
}
(*fit).second.SetVote();
}
IT win = m.begin();
for(IT it=m.begin(); it!=m.end(); ++it){
cout << (*it).second.GetName() << ":" << (*it).second.GetVote() << endl;
if((*it).second.GetVote() > (*win).second.GetVote())
win = it;
}
cout << "恭喜:" << (*win).second.GetName() << "成功当选*****" << endl;
return 0;
}
允许键值重复的映射,表示一对多的逻辑关系,不支持下标运算符
eg:09multimap.cpp
#include
#include
using namespace std;
int main(void)
{
multimap<string, int> m;
m.insert(make_pair("张飞", 80));
m.insert(make_pair("赵云", 70));
m.insert(make_pair("关羽", 60));
m.insert(make_pair("张飞", 50));
m.insert(make_pair("赵云", 40));
m.insert(make_pair("关羽", 30));
cout << "节点个数:" << m.size() << endl;
typedef multimap<string, int>::iterator IT;
for(IT it=m.begin(); it!=m.end(); it++){
cout << (*it).first << ":" << (*it).second << ' ';
}
cout << endl;
return 0;
}
没有值只要键的映射,和向量等基本容器相比最大的优势就是排重
eg:10set.cpp
#include
#include
using namespace std;
int main(void)
{
set<char> s;
s.insert('a');
s.insert('b');
s.insert('c');
s.insert('a');
s.insert('b');
s.insert('c');
cout << "节点个数:" << s.size() << endl;
typedef set<char>::iterator IT;
for(IT it=s.begin(); it!=s.end(); ++it){
cout << *it << ' ';
}
cout << endl;
return 0;
}
没有值只有键的多重映射
eg:multiset.cpp
#include
#include
using namespace std;
int main(void)
{
multiset<char> s;
s.insert('a');
s.insert('b');
s.insert('c');
s.insert('a');
s.insert('b');
s.insert('c');
cout << "节点个数:" << s.size() << endl;
typedef multiset<char>::iterator IT;
for(IT it=s.begin(); it!=s.end(); ++it){
cout << *it << ' ';
}
cout << endl;
return 0;
}