一 multimap
multimap有key和value值,key用于排序,multimap存取key所对应的value值
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cstring>
using namespace std;
class Employee
{
private:
int id;
string name;
string tel;
public:
Employee(int i,string n,string s)
{
id = i;
name = n;
tel = s;
}
void show()
{
cout << "id =" << id << " " << "name = " << name << " " << "tel = " << tel << endl;
}
~Employee()
{
cout << "~Employee" << endl;
}
};
int main(int argc, const char *argv[])
{
Employee e1(1000,"aaa","123");
Employee e2(1001,"bbb","456");
Employee e3(1002,"ccc","789");
Employee e4(1003,"ddd","111");
multimap<string,Employee> m;
m.emplace(make_pair("sale",e1));
m.emplace(make_pair("sale",e2));
m.emplace(make_pair("development",e3));
m.emplace(make_pair("finance",e4));
for(auto &m : m)
{
cout << "department :" << m.first << endl;
m.second.show();
}
return 0;
}
ps:char s[32]和string s的区别:char []类型赋值必须使用strcpy函数,不能直接使用"=",string类型赋值可以直接使用"="
二.deque
双端数组,接口和vector类似,在头部和尾部非常迅速,但是在中部添加元素比较费时,需要添加头文件#include <deque>
#include <iostream>
#include <string>
#include <deque>
using namespace std;
int main(int argc, const char *argv[])
{
int data[5] = {1,2,3,4,5};
deque<int> d1;
deque<int> d2(data,data+5);
deque<int> d3(10,1);
for(auto &d : d2)
{
cout << d << " ";
}
cout << endl;
d2.emplace_back(6);
d2.emplace_front(0);
for(auto &d : d2)
{
cout << d << " ";
}
cout << endl;
d2.pop_back();
d2.pop_front();
for(auto &d : d2)
{
cout << d << " ";
}
cout << endl;
d2.front() = 1000;
d2.back() = 2000;
for(auto &d : d2)
{
cout << d << " ";
}
cout << endl;
return 0;
}
三.容器适配器
stack(LIFO)
queue(FIFO)
priority_queue()
1.stack
#include <iostream>
#include <stack>
#include <time.h>
using namespace std;
int main(int argc, const char *argv[])
{
stack<int> s;
srand(time(NULL));
int num;
for(int i = 0;i < 10;i++)
{
num = rand() % 10;
s.push(num);
cout << num << "push success!" << endl;
}
cout << "top of the stack" << s.top() << endl;
cout << "size of stack:" << s.size() << endl;
while(!s.empty())
{
cout << s.top() << "pop" << endl;
s.pop();
}
s.pop();
return 0;
}
2.queue
#include <queue>
#include <iostream>
#include <time.h>
using namespace std;
int main(int argc, const char *argv[])
{
queue<int> q;
for(int i = 0;i < 10;i++)
{
q.push(i);
cout << i << "push success" << endl;
}
cout << "top of the queue" << q.front() << endl;
cout << "back of the queue" << q.back() << endl;
cout << "size of the queue" << q.size() << endl;
while(!q.empty())
{
cout << q.front() << "out of the queue" << endl;
q.pop();
}
return 0;
}
3.priority_queue
//priority_queue<int> p;
//priority_queue<int, vector<int>, less<int>> p; //等价于priority_queue<int> p;
#include <iostream>
#include <queue>
#include <time.h>
using namespace std;
int main(int argc, const char *argv[])
{
priority_queue<int,deque<int>,greater<int>> p;
srand(time(NULL));
int num;
for(int i = 0;i < 10;i++)
{
num = rand() % 10;
p.emplace(num);
cout << num << "push success" << endl;
}
cout << "top :" << p.top() << endl;
cout << "size :" << p.size() << endl;
while(!p.empty())
{
cout << p.top() << "out of the queue" << endl;
p.pop();
}
return 0;
}
priority_queue是将进队元素排序后,按照从大到小的顺序或者从小到大的顺序依次出队
四.异常
4.1什么是异常
异常处理就是处理程序中的错误,所谓错误就是程序运行时发生的一些异常事件(栈溢出,数组下标越界等)
4.2异常的语法
分为三个部分:
抛出异常:throw
检测异常:try
{
可能发生异常的语句
}
捕获异常:
catch(异常类型 变量名)
{
异常处理语句
}
抛出异常:throw关键字,当执行throw之后,try语句会立即结束,运行catch中的异常处理的语句
throw表达式或常量或变量
throw的用法和return类似,throw是抛出一个值,return是上一级调用的函数
#include <iostream>
using namespace std;
int Div(int a,int b)
{
if(b == 0)
{
// throw 0;
throw 'a';
}
return a / b;
}
int main(int argc, const char *argv[])
{
int a, b;
cin >> a >> b;
try
{
cout << Div(a,b);
}
catch(int)
{
cout << "zero exception" << endl;
}
catch(char)
{
cout << "zero exception" << endl;
cout << "nihao" << endl;
}
cout << "helloworld" << endl;
return 0;
}
4.3异常的声明
#include <iostream>
using namespace std;
int Div(int x,int y)
{
if(y == 0)
{
// throw 0;
// throw 'a';
throw 3.14;
}
return x / y;
}
int main(int argc, const char *argv[])
{
int a,b;
cin >> a >> b;
try
{
Div(a,b);
}
catch(int)
{
cout << "int exceptionption" << endl;
}
catch(char)
{
cout << "char exception" << endl;
}
catch(double)
{
cout << "double exception" << endl;
}
return 0;
}
4.4异常对象
#include <iostream>
using namespace std;
class Test
{
public:
Test()
{
cout << "Test's create" << endl;
}
Test(const Test &obj)
{
cout << "Test's copy create" << endl;
}
void print()
{
cout << "Test exception" << endl;
}
~Test()
{
cout << "Test's destory" << endl;
}
};
int Div(int x,int y)
{
if(y == 0)
{
// throw Test();
throw new Test();
}
return x / y;
}
int main(int argc, const char *argv[])
{
int a,b;
cin >> a >> b;
try
{
Div(a,b);
}
/* catch(Test t)
{
t.print();
}*/
catch(Test &t)
{
t.print();
}
catch(Test *t)
{
t->print();
delete t;
}
return 0;
}
4.5标准异常库
#include <iostream>
#include <exception>
using namespace std;
class Exception:public exception
{
private:
string Errmsg;
public:
Exception(string e):Errmsg(e)
{
}
virtual const char* what() const throw()
{
return Errmsg.data();
}
};
int Div(int x,int y)
{
if(y == 0)
{
throw Exception("y is not allowed to be zero");
throw bad_alloc();
}
return x / y;
}
int main(int argc, const char *argv[])
{
int a,b;
cin >> a >> b;
try
{
Div(a,b);
}
catch(const std::exception &e)
{
cout << e.what() << endl;
}
return 0;
}
4.6自定义stack
优点:pop()------>返回下一个元素
如果stack为空,pop和top会抛出异常
#include <iostream>
#include <exception>
#include <deque>
using namespace std;
template<typename T>
class Stack
{
protected:
std::deque<T> c;
public:
class ReadEmptyStack:public exception
{
public:
virtual const char* what() const throw()
{
return "read empty stack";
}
};
auto size() const
{
return c.size();
}
bool empty() const
{
return c.empty();
}
void push(const T& elem)
{
c.push_back(elem);
}
T pop()
{
if(c.empty())
{
throw ReadEmptyStack();
}
T elem(c.back());
c.pop_back();
return elem;
}
T& top()
{
if(c.empty())
{
throw ReadEmptyStack();
}
return c.back();
}
};
int main(int argc, const char *argv[])
{
try
{
Stack<int> st;
st.push(1);
st.push(2);
st.push(3);
st.push(4);
cout << st.pop() << endl;
cout << st.pop() << endl;
st.top() = 77;
st.push(5);
st.push(6);
st.pop();
cout << "size = " << st.size() << endl;
while(!st.empty())
{
st.pop();
}
while(!st.empty())
{
st.pop();
}
st.pop();
}
catch(const std::exception &e)
{
cout << e.what() << endl;
}
return 0;
}
五string
和vector接口类似,是一种特殊的容器,除了具有vector的内存特性之外,专门用于对字符串的处理操作
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main(int argc, const char *argv[])
{
string s1;
string s2("helloworld");
string s3(10,'a');
string s4(s2);
cout << s2 << endl;
s1 += "helloworld";
if(s1 == s2)
{
}
s1 += s2;
cout << s1 << endl;
string s("helloworld");
cout << s[1] << endl;
s[1] = 'x';
for(auto it = s.begin();it != s.end();it++)
{
cout << *it << " ";
}
cout << endl;
for(auto &s : s)
{
cout << s << " ";
}
cout << endl;
char buf[32] = {0};
s.copy(buf,5);
cout << buf << endl;
cout << s.length() << endl;
cout << s.size() << endl;
s.reserve(100);
cout << s.capacity() << endl;
s.shrink_to_fit();
cout << s.capacity() << endl;
if(s.empty())
{
cout << "string is null" << endl;
}
s = "helloworld";
const char *ss = "this is test";
s1.assign(ss);
cout << s1 << endl;
s1.assign(ss,7);
cout << s1 << endl;
s1.assign(5,'a');
cout << s1 << endl;
s1.assign(s2);
cout << s1 << endl;
s1.assign(s4,4,3);
cout << s1 << endl;
s1 += "1234";
cout << s1 << endl;
s1 += s2;
cout << s1 << endl;
s1.append(ss);
s1.append(s2);
cout << s1 << endl;
s1.append(ss,2);
s1.append(s2,4,2);
cout << s1 << endl;
s1.append(10,'x');
cout << s1 << endl;
if(s1.compare(ss) < 0)
{
cout << "s1 < ss" << endl;
}
cout << s1.substr() << endl;
cout << s1.substr(5,6) << endl;
int p = s1.find('o');
cout << p << endl;
p = s1.find('o',8);
cout << p << endl;
p = s1.find("ll");
cout << p << endl;
p = s1.find(s2);
cout << p << endl;
s1.replace(5,3,"xxx");
cout << s1 << endl;
return 0;
}