请教vector问题
c++吧
全部回复
仅看楼主
level 6
Lion1942 楼主
请教下各位大哥,vector的参数是不是都必须是确定类型的,如果使用模板类类型的参数可不可以?
例:首先定义了类模板
template<class iter,class inter>
class carton{};
然后在main函数中定义容器:
vector<carton<iter,inter>> cartons(10);
这样可不可以?
2011年04月28日 17点04分 1
level 12
当然OK
2011年04月28日 23点04分 2
level 12
那个>>。。。蛋疼
2011年04月29日 01点04分 3
level 15
不是最新的C++0x编译器需要分开来写成> >。
2011年04月29日 01点04分 4
level 12
学习了
g++ -Wall -O3 main.c的话会出现‘>>’ should be ‘> >’ within a nested template argument list错误,g++ -std=c++0x就不会
2011年04月29日 01点04分 5
level 6
Lion1942 楼主
昨晚练习时发现在vector<carton<iter,inter>> cartons(10);时无法编译,但换成
vector<carton<iter,inter>*> cartons(10);时可以正常编译,由于carton类为模板类,所以当时想是不是模板类的对象无法做vector的参数!
2011年04月29日 02点04分 6
level 6
Lion1942 楼主
代码如下,各位大神们给点意见,不胜感激!!
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <sstream>
#include <string>
using namespace std;
////////////////////////////////////////////////////////////////////////box
template<class inter>
class box{
private:
inter height;
inter length;
inter wideth;
public:
box(inter heightvalue=0,inter lengthvalue=0,inter widtehvalue=0);
   box(const box& abox);
box operator=(const box& abox);
inter volume();
bool operator<(const box& abox);
inter getheight()const{return height;};
inter getlength()const{return length;};
inter getwideth()const{return wideth;};
void setheight(const inter heightvalue)const{height=heightvalue;};
   void setlength(const inter lengthvalue)const{length=lengthvalue;};
void setwideth(const inter widethvalue)const{wideth=widethvalue;};
};
template<class inter>
box<inter>::box(inter heightvalue,inter lengthvalue,inter widethvalue):height(heightvalue),length(lengthvalue),wideth(widethvalue){
};
template<class inter>
box<inter>::box(const box<inter>& abox){
height=abox.height;
length=abox.length;
wideth=abox.wideth;
};
template<class inter>
box<inter> box<inter>::operator=(const box<inter>& abox){
height=abox.height;
length=abox.length;
wideth=abox.wideth;
return *this;
};
template<class inter>
inter box<inter>::volume(){
return height*length*wideth;
};
template<class inter>
bool box<inter>::operator<(const box& abox){
return volume()<abox.volume();
};
////////////////////////////////////////////////////////////////////////////carton
template<class iter,class inter>
class carton:public box<inter>{
private:
iter pmentral;
public:
carton(char* astring="paple");
carton(const carton<iter,inter>& acarton);
carton<iter,inter>& operator=(const carton<iter,inter>& acarton)const;
iter getpmentral()const{return pmentral;};
void setmentral(iter tstring)const{pmentral=&tstring;};
};
template<class iter,class inter>
carton<iter,inter>::carton(char* astring){
pmentral=new char[sizeof(astring)+1];
strcpy(pmentral,astring);
};
template<class iter,class inter>
carton<iter,inter>::carton(const carton<iter,inter>& acarton):box<inter>(carton){
   pmentral=new char[sizeof(astring)+1];
strcpy(pmentral,astring);
};
template<class iter,class inter>
carton<iter,inter>& carton<iter,inter>::operator=(const carton<iter,inter>& acarton)const{
   this->setheight(acarton.getheight());
this->setlength(acarton.getlength());
this->setwideth(acarton.getwideth());

2011年04月29日 03点04分 7
level 6
Lion1942 楼主
this->setmentral(acarton.getpmentral());
return *this;
};
//////////////////////////////////////////////////////////////////////////iterator
template<class xpoint>
class point:public iterator<random_access_iterator_tag,int*,int>{
private:
   xpoint apoint;
public:
point(xpoint tpoint);
~point();
point(const point<xpoint>& tpoint);
point<xpoint> operator=(const point<xpoint>& tpoint);
bool operator<(const point<xpoint>& tpoint);
bool operator==(const point<xpoint>& tpoint);
xpoint operator*();
xpoint operator[](size_t index);
point<xpoint>& operator++();
point<xpoint>& operator--();
xpoint getpoint(){return this->apoint;};
void setpoint(xpoint tpoint){apoint=tpoint;};
bool operator!=(const point<xpoint>& tpoint);
bool operator<=(const point<xpoint>& tpoint);
};
template<class xpoint>
point<xpoint>::point(xpoint tpoint):apoint(tpoint){};
template<class xpoint>
point<xpoint>::~point(){};
template<class xpoint>
point<xpoint>::point(const point<xpoint>& tpoint){
apoint=tpoint.apoint;
};
template<class xpoint>
point<xpoint> point<xpoint>::operator=(const point<xpoint>& tpoint){
   this->setpoint(tpoint.getpoint());
   return *this;
};
template<class xpoint>
bool point<xpoint>::operator<(const point<xpoint>& tpoint){
return this->apoint<tpoint.apoint;
};
template<class xpoint>
bool point<xpoint>::operator==(const point<xpoint>& tpoint){
return this->apoint==tpoint.apoint;
};
template<class xpoint>
xpoint point<xpoint>::operator*(){
return apoint;
};
template<class xpoint>
point<xpoint>& point<xpoint>::operator++(){
   apoint++;
return *this;
};
template<class xpoint>
point<xpoint>& point<xpoint>::operator--(){
apoint--;
return *this;
};
template<class xpoint>
xpoint point<xpoint>::operator[](size_t index){
int count=0;
while(count!=index)
{
*this++;
count++;
}
return this->apoint;
};
template<class xpoint>
bool point<xpoint>::operator!=(const point<xpoint>& tpoint){
return apoint!=tpoint.apoint;
};
template<class xpoint>
bool point<xpoint>::operator<=(const point<xpoint>& tpoint){
   return apoint<=tpoint.apoint;
};
int main(){
carton<char*,double> acarton[10];
   cout<<acarton[0].getheight()<<acarton[0].getlength()<<acarton[0].getwideth()<<acarton[0].getpmentral()<<endl;
   vector<box<double>*> boxes(10);
vector<carton<char*,double>*> cartons(10); //为何此处vector参数使用carton<char*,double>会报错?
   for(size_t i=0;i<10;i++)
{
cartons[i]=&acarton[i];
}
point<carton<char*,double>*> begin=*cartons.begin();
point<carton<char*,double>*> end=*(cartons.end()-1);
for(;begin<=end;begin++)
{
cout<<(*begin)->getheight()<<(*begin)->getlength()<<(*begin)->getwideth()<<(*begin)->getpmentral()<<endl;
}
   return 0;
}
2011年04月29日 03点04分 8
level 12
见4L
2011年04月29日 08点04分 9
1