65,211
社区成员
发帖
与我相关
我的任务
分享//#ifndef BAG_H
//#define BAG_H
//背包类,由一个map记录武器名字和个数,vector记录武器属性;
#include <iostream>
#include <string>
#include <vector>
#include <map>
//#include "F:\msdosd\gs\head\key.h"
using namespace std;
//typedef vector<Key> mups;
//typedef map<string,int> mup;
template <class T>
class Bag//背包类
{
public:
Bag(){ }
bool del_bag(T& res);
//void add_bag(T res);
//bool emptybag(){return _bao.empty();}
// void display_bag(bool shop=true);
vector<T> _baosu;//武器属性;
map<string, int> _bao ;//武器名字和个数
};
template <class T>
bool Bag<T> ::del_bag(T& res)
{
vector<T>::iterator its= _baosu.begin();
map<string, int>::iterator it;
it=_bao.find(res.first);
if( (it != _bao.end()) && (it->second >1) )
{
it->second--;
return true;
}
if( (it != _bao.end()) && (it->second =1) )
{
for(; its != _baosu.end(); its++)
{
if( its->first == res.first)
{
_baosu.erase(its);
_bao.erase(it);
return true;
}
}
}
return false;
}
/*void Bag::add_bag(T res)
{
map<string, int>::iterator it=_bao.begin();
it=_bao.find(res.first);
if(it != _bao.end())
{
it->second++;
}
else
{
_bao.insert(make_pair(res.first, 1));
_baosu.push_back(res);
}
}
void Bag::display_bag(bool shop)
{
map<string, int>::iterator it=_bao.begin();
vector<T>::iterator its=_baosu.begin();
if(shop)
{
for(; it != _bao.end(); ++it)
{
cout<<it->first<<"\t数量:"<<it->second;
for(its=_baosu.begin(); its != _baosu.end();its++)
{
if(its->first == it->first)
{
cout<<"\t攻:"<<its->second<<"\t";
}
}
}
cout<<endl;
}
else
{
for(; it != _bao.end(); ++it)
{
cout<<it->first<<"\t数量:"<<it->second;
for(its=_baosu.begin(); its != _baosu.end();its++)
{
if(its->first == it->first)
{
cout<<"\t攻:"<<its->second<<"\t钱:"<<(it->second)*(its->money)<<"\t";
}
}
}
cout<<endl;
}
}*/
//#endif
/*int main()
{
T st1("华山剑", 13, 50);
T st2("华山刀", 12, 45);
Bag s;
s.add_bag(st1);
s.add_bag(st1);
s.add_bag(st2);
s.del_bag(st1);
s.display_bag();
s.display_bag(true);//查看背包里的东西
s.display_bag(false);//卖背包里的东西
while(1);
return 0;
}*/
//背包类,由一个map记录武器名字和个数,vector记录武器属性;
#include <iostream>
#include <string>
#include <vector>
#include <map>
//#include "F:\msdosd\gs\head\key.h"
using namespace std;
//typedef vector<Key> mups;
//typedef map<string,int> mup;
template <class T>
class Bag//背包类
{
public:
Bag(){ }
bool del_bag(T& res);
void add_bag(T res);
bool emptybag(){return _bao.empty();}
void display_bag(bool shop=true);
vector<T> _baosu;//武器属性;
map<string, int> _bao ;//武器名字和个数
};
template <class T>
bool Bag<T> ::del_bag(T& res)
{
vector<T>::iterator its= _baosu.begin();
map<string, int>::iterator it;
it=_bao.find(res.first);
if( (it != _bao.end()) && (it->second >1) )
{
it->second--;
return true;
}
if( (it != _bao.end()) && (it->second =1) )
{
for(; its != _baosu.end(); its++)
{
if( its->first == res.first)
{
_baosu.erase(its);
_bao.erase(it);
return true;
}
}
}
return false;
}
template <class T>
void Bag<T>::add_bag(T res)
{
map<string, int>::iterator it=_bao.begin();
it=_bao.find(res.first);
if(it != _bao.end())
{
it->second++;
}
else
{
_bao.insert(make_pair(res.first, 1));
_baosu.push_back(res);
}
}
template <class T>
void Bag<T>::display_bag(bool shop)
{
map<string, int>::iterator it=_bao.begin();
vector<T>::iterator its=_baosu.begin();
if(shop)
{
for(; it != _bao.end(); ++it)
{
cout<<it->first<<"\t数量:"<<it->second;
for(its=_baosu.begin(); its != _baosu.end();its++)
{
if(its->first == it->first)
{
cout<<"\t攻:"<<its->second<<"\t";
}
}
}
cout<<endl;
}
else
{
for(; it != _bao.end(); ++it)
{
cout<<it->first<<"\t数量:"<<it->second;
for(its=_baosu.begin(); its != _baosu.end();its++)
{
if(its->first == it->first)
{
cout<<"\t攻:"<<its->second<<"\t钱:"<<(it->second)*(its->money)<<"\t";
}
}
}
cout<<endl;
}
}
int main()
{
/* T st1("华山剑", 13, 50); //是什么??
T st2("华山刀", 12, 45);
Bag s;
s.add_bag(st1);
s.add_bag(st1);
s.add_bag(st2);
s.del_bag(st1);
s.display_bag();
s.display_bag(true);//查看背包里的东西
s.display_bag(false);//卖背包里的东西
while(1);
*/
return 0;
}
//背包类,由一个map记录武器名字和个数,vector记录武器属性;
#include <iostream>
#include <string>
#include <vector>
#include <map>
//#include "F:\msdosd\gs\head\key.h"
using namespace std;
//typedef vector<Key> mups;
//typedef map<string,int> mup;
template <class T>
class Bag//背包类
{
public:
Bag(){ }
bool del_bag(T& res);
void add_bag(T res);
bool emptybag(){return _bao.empty();}
void display_bag(bool shop=true);
vector<T> _baosu;//武器属性;
map<string, int> _bao ;//武器名字和个数
};
template <class T>
bool Bag<T> ::del_bag(T& res)
{
typename vector<T>::iterator its= _baosu.begin();
map<string, int>::iterator it;
it=_bao.find(res.first);
if( (it != _bao.end()) && (it->second >1) )
{
it->second--;
return true;
}
if( (it != _bao.end()) && (it->second =1) )
{
for(; its != _baosu.end(); its++)
{
if( its->first == res.first)
{
_baosu.erase(its);
_bao.erase(it);
return true;
}
}
}
return false;
}
template <class T>
void Bag<T>::add_bag(T res)
{
typename map<string, int>::iterator it=_bao.begin();
it=_bao.find(res.first);
if(it != _bao.end())
{
it->second++;
}
else
{
_bao.insert(make_pair(res.first, 1));
_baosu.push_back(res);
}
}
template <class T>
void Bag<T>::display_bag(bool shop)
{
typename map<string, int>::iterator it=_bao.begin();
typename vector<T>::iterator its=_baosu.begin();
if(shop)
{
for(; it != _bao.end(); ++it)
{
cout<<it->first<<"\t数量:"<<it->second;
for(its=_baosu.begin(); its != _baosu.end();its++)
{
if(its->first == it->first)
{
cout<<"\t攻:"<<its->second<<"\t";
}
}
}
cout<<endl;
}
else
{
for(; it != _bao.end(); ++it)
{
cout<<it->first<<"\t数量:"<<it->second;
for(its=_baosu.begin(); its != _baosu.end();its++)
{
if(its->first == it->first)
{
cout<<"\t攻:"<<its->second<<"\t钱:"<<(it->second)*(its->money)<<"\t";
}
}
}
cout<<endl;
}
}
int main()
{
/* T st1("华山剑", 13, 50); //是什么??
T st2("华山刀", 12, 45);
Bag s;
s.add_bag(st1);
s.add_bag(st1);
s.add_bag(st2);
s.del_bag(st1);
s.display_bag();
s.display_bag(true);//查看背包里的东西
s.display_bag(false);//卖背包里的东西
while(1);
*/
return 0;
}