c++高级编程12 中
容器适配器:
适配器没有提供迭代器
queue: size(),empty(),swap(),push(),emplace()
pop不会返回弹出的元素,通过front()和back()分别获得第一个和最后一个元素的引用
#include <iostream>
#include <queue>
using namespace std;
template <typename T>
class PackBuffer
{
public:
PackBuffer(size_t maxsize = 0);
bool bufferPacket(const T& packet);
T getNextPacket() throw(std::out_of_range);
protected:
//缓存队列
std::queue<T> mPackets;
int mMaxsize;
};
template <typename T>
PackBuffer<T>::PackBuffer(size_t maxsize)
:mMaxsize(maxsize)
{}
template <typename T>
bool PackBuffer<T>::bufferPacket(const T& packet)
{
if(mMaxsize > 0&&mPackets.size() == mMaxsize)
{
return false;
}
mPackets.push(packet);
return true;
}
template<typename T>
T PackBuffer<T>::getNextPacket() throw(std::out_of_range)
{
if(mPackets.empty())
{
throw std::out_of_range("Buffer is empty");
}
T temp= mPackets.front();
mPackets.pop();
return temp;
}
class IPPacket
{
public:
IPPacket(int id):mID(id){}
int getID()const {return mID;}
private:
int mID;
};
int main()
{
PackBuffer<IPPacket> ipPacket(3);
for(int i=0;i<=4;++i)
{
if(!ipPacket.bufferPacket(IPPacket(i)))
cout<<"Packet "<<i <<"dropped (queue is full)"<<endl;
}
while(true)
{
try
{
IPPacket packet = ipPacket.getNextPacket();
cout<<"Process packet"<<packet.getID()<<endl;
}catch(const out_of_range)
{
cout<<"Queue is empty"<<endl;
break;
}
}
return 0;
}
priority_queue 优先队列:push(),pop(), top() 返回头元素的const引用 size() empty() swap()
#include <iostream>
#include <queue>
using namespace std;
class Error
{
public:
Error(int priority,const std::string& errMsg)
:mPriority(priority),mError(errMsg){}
int getPriorty() const {return mPriority;}
std::string getErrorString()const {return mError;}
friend bool operator <(const Error& lsh,const Error& rsh);
friend std::ostream& operator <<(std::ostream &os,const Error& err);
protected:
//错误号 错误信息
int mPriority;
std::string mError;
};
//return highest priority error
class ErrorCorrelator
{
public:
ErrorCorrelator(){}
void addError(const Error& error);
Error getError() throw(out_of_range);
protected:
std::priority_queue<Error> mErrors;
};
bool operator<(const Error& lhs,const Error& rsh)
{
return (lhs.mPriority<rsh.mPriority);
}
ostream& operator<<(ostream &os,const Error &err)
{
os<<err.mError<<" "<<err.mPriority<<" "<<err.getErrorString()<<endl;
return os;
}
void ErrorCorrelator::addError(const Error &error)
{
mErrors.push(error);
}
Error ErrorCorrelator::getError() throw(out_of_range)
{
if(mErrors.empty())
throw out_of_range("No element!");
Error top = mErrors.top();
mErrors.pop();
return top;
}
int main()
{
ErrorCorrelator ec;
ec.addError(Error(3,"unable to read file"));
ec.addError(Error(1,"Incorrect entry from user"));
ec.addError(Error(10,"unable to allocate memory"));
while(true)
{
try{
Error e= ec.getError();
cout<<e<<endl;
}catch(const out_of_range&)
{
cout<<"Finished processing nerror"<<endl;
break;
}
}
return 0;
}
#include <iostream>
#include <stack>
using namespace std;
class Error
{
public:
Error(int priority,const std::string& errMsg)
:mPriority(priority),mError(errMsg){}
int getPriorty() const {return mPriority;}
std::string getErrorString()const {return mError;}
friend bool operator <(const Error& lsh,const Error& rsh);
friend std::ostream& operator <<(std::ostream &os,const Error& err);
protected:
//错误号 错误信息
int mPriority;
std::string mError;
};
//return highest priority error
class ErrorCorrelator
{
public:
ErrorCorrelator(){}
void addError(const Error& error);
Error getError() throw(out_of_range);
protected:
std::stack<Error> mErrors;
};
bool operator<(const Error& lhs,const Error& rsh)
{
return (lhs.mPriority<rsh.mPriority);
}
ostream& operator<<(ostream &os,const Error &err)
{
os<<err.mError<<" "<<err.mPriority<<" "<<err.getErrorString()<<endl;
return os;
}
void ErrorCorrelator::addError(const Error &error)
{
mErrors.push(error);
}
Error ErrorCorrelator::getError() throw(out_of_range)
{
if(mErrors.empty())
throw out_of_range("No element!");
Error top = mErrors.top();
mErrors.pop();
return top;
}
int main()
{
ErrorCorrelator ec;
ec.addError(Error(3,"unable to read file"));
ec.addError(Error(1,"Incorrect entry from user"));
ec.addError(Error(10,"unable to allocate memory"));
while(true)
{
try{
Error e= ec.getError();
cout<<e<<endl;
}catch(const out_of_range&)
{
cout<<"Finished processing nerror"<<endl;
break;
}
}
return 0;
}
*/
pair工具类
将两个属性不同的值组合起来 通过frist second 访问这对象
void func(const map<int,int>& m)
{
cout<<m[1]<<endl; //error operator没有标记为const
}
void func(const map<int,int>& m)
{
auto iter = m.find(1);
if(iter!=m.end())
cout<<iter->second<<endl;
}
map迭代器
map<int,Data> datamap;
datamap[1] = Data(4);
datamap[1] = Data(5);
for(auto iter = datamap.begin();iter!=datamap.end();++iter)
{
cout<<iter->second.getVal()<<endl;
}
for(auto& p:datamap)
{
cout<<p.second.getVal()<<endl;
}
不看了,看了一天,标记一下回头再看 410页