c++中stl类模板的问题,我想把对象直接放入一个MAP中遍历,问下大神该怎么改 30
#include"stdafx.h"#include<iostream>usingnamespacestd;#include<map>#include<string>cl...
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <map>
#include <string>
class CarDriver
{
public:
CarDriver(string name):m_name(name){}
private:
string m_name;
};
class TruckDriver
{
public:
TruckDriver(string name):m_name(name){}
private:
string m_name;
};
class BusDriver
{
public:
BusDriver(string name):m_name(name){}
private:
string m_name;
};
template <typename T>
class vehicle
{
public:
vehicle<T>(string name,string type,int cout,int speed,int weight):m_driver(name)
{
m_type = type;
m_cout = cout;
m_speed = speed;
m_weight = weight;
}
void show()
{
cout<<m_driver.getName()<<" "<<m_type<<" "<<m_cout<<" "<<m_speed<<" "<<m_weight<<endl;
}
private:
T m_driver;
string m_type;
int m_cout;
int m_speed;
int m_weight;
};
int _tmain(int argc, _TCHAR* argv[])
{
vehicle<CarDriver> car1("zhangsan","car",4,77,500);
vehicle<BusDriver> bus1("chen","bus",30,50,1000);
map<int,vehicle<CarDriver>> m;
m.insert(map<int,vehicle<CarDriver>>::value_type(101,car1));
map<int,vehicle<CarDriver>>::iterator iter;
for (iter = m.begin();iter != m.end();iter++)
{
cout<<iter->first<<endl;
iter->second.show();
}
//vehicle<CarDriver> car4 = reinterpret_cast<vehicle<CarDriver>(bus1);
map<int,vehicle<BusDriver>> n;
n.insert(map<int,vehicle<BusDriver>>::value_type(104,bus1));
map<int,vehicle<BusDriver>>::iterator it;
for (it = n.begin();it != n.end();it++)
{
cout<<it->first<<endl;
it->second.show();
}
return 0;
} 展开
#include <iostream>
using namespace std;
#include <map>
#include <string>
class CarDriver
{
public:
CarDriver(string name):m_name(name){}
private:
string m_name;
};
class TruckDriver
{
public:
TruckDriver(string name):m_name(name){}
private:
string m_name;
};
class BusDriver
{
public:
BusDriver(string name):m_name(name){}
private:
string m_name;
};
template <typename T>
class vehicle
{
public:
vehicle<T>(string name,string type,int cout,int speed,int weight):m_driver(name)
{
m_type = type;
m_cout = cout;
m_speed = speed;
m_weight = weight;
}
void show()
{
cout<<m_driver.getName()<<" "<<m_type<<" "<<m_cout<<" "<<m_speed<<" "<<m_weight<<endl;
}
private:
T m_driver;
string m_type;
int m_cout;
int m_speed;
int m_weight;
};
int _tmain(int argc, _TCHAR* argv[])
{
vehicle<CarDriver> car1("zhangsan","car",4,77,500);
vehicle<BusDriver> bus1("chen","bus",30,50,1000);
map<int,vehicle<CarDriver>> m;
m.insert(map<int,vehicle<CarDriver>>::value_type(101,car1));
map<int,vehicle<CarDriver>>::iterator iter;
for (iter = m.begin();iter != m.end();iter++)
{
cout<<iter->first<<endl;
iter->second.show();
}
//vehicle<CarDriver> car4 = reinterpret_cast<vehicle<CarDriver>(bus1);
map<int,vehicle<BusDriver>> n;
n.insert(map<int,vehicle<BusDriver>>::value_type(104,bus1));
map<int,vehicle<BusDriver>>::iterator it;
for (it = n.begin();it != n.end();it++)
{
cout<<it->first<<endl;
it->second.show();
}
return 0;
} 展开
2个回答
展开全部
//代码如下,你自己对比看看
#include <iostream>
#include <map>
#include <string>
using namespace std;
class CarDriver
{
public:
CarDriver(string name):m_name(name){}
const char* getName() const { return m_name.c_str(); }
private:
string m_name;
};
class TruckDriver
{
public:
TruckDriver(string name):m_name(name){}
const char* getName() const { return m_name.c_str(); }
private:
string m_name;
};
class BusDriver
{
public:
BusDriver(string name):m_name(name){}
const char* getName() const { return m_name.c_str(); }
private:
string m_name;
};
template <typename T>
class vehicle
{
public:
vehicle<T>(string name,string type,int cout,int speed,int weight)
:m_driver(name), m_type(type)
{
m_cout = cout;
m_speed = speed;
m_weight = weight;
}
void show()
{
cout<< m_driver.getName() << " "
<< m_type << " " << m_cout << " "
<< m_speed<< " " << m_weight << endl;
}
private:
T m_driver;
string m_type;
int m_cout;
int m_speed;
int m_weight;
};
int main(int argc, char* argv[])
{
vehicle<CarDriver> car1("zhangsan","car",4,77,500);
typedef map< int,vehicle<CarDriver> > vc;
vc m;
m.insert(vc::value_type(101,car1));
vc::iterator iter;
for (iter = m.begin();iter != m.end();iter++)
{
cout<< iter->first << endl;
iter->second.show();
}
vehicle<BusDriver> bus1("chen","bus",30,50,1000);
typedef map< int,vehicle<BusDriver> > vb;
vb n;
n.insert(vb::value_type(104,bus1));
vb::iterator it;
for (it = n.begin();it != n.end();it++)
{
cout<< it->first << endl;
it->second.show();
}
return 0;
}
追问
我是想只要放到一个迭代器中的啊,就是只要一次遍历就够了!
追答
那就用foreach,具体例子我就先不给了,你去查下foreach的用法,不懂再问吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询