首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

这样定义的vector怎么取值和赋值呢

2012-02-29 
这样定义的vector如何取值和赋值呢classABC{public:ABC():row(0),column(0){}ABC(ABCconst&rhs){(*this)r

这样定义的vector如何取值和赋值呢
class   ABC
{
public:
ABC():row(0),column(0){}
ABC(   ABC   const&   rhs){   (*this)   =   rhs;}
ABC(   int   r,   int   c):row(r),column(c){}

ABC&   operator=(   ABC   const&   rhs){   row=rhs.row;   column=rhs.column;   return   *this;}
bool   operator==(   ABC   const&   rhs)   const   {   return   (row   ==   rhs.row)   &&   (column   ==   rhs.column);}
bool   operator!=(   ABC   const&   rhs)   const   {   return   (row   !=   rhs.row)   ||   (column   !=   rhs.column);}
bool   operator <(   ABC   const&   rhs)   const   {   return   (row   <   rhs.row)   ||   ((row   ==   rhs.row)   &&   (column   <   rhs.column));}

int   row;
int   column;
};
typedef   vector <ABC>   ABCVector;
--------------------
1.现在我定义一个ABCVector   abcv;
如何给abcv赋值和取值呢.
2.这种方式是否和用结构体一样,如下.
struct   ABC
{
        int   row;
        int   column;
}
vector   <ABC>   ABCVector;


[解决办法]
vector <ABC> ABCVector(100);
ABCVector[2]=ABC(1,2);
ABC a=ABCVector[2];
[解决办法]
yutaooo(用标准C++,需详加解释,LZ看不懂是不会给钱的!55~~) ( ) 信誉:100 Blog 加为好友 2007-04-20 08:25:33 得分: 0


std::vector <ABC> abcv;
abcv.push_back(ABC(2,3));

不用特意写临时变量的吧,这样难道不行吗?


--------------------------------------
呵呵,中间的过程是一样的
[解决办法]
//完整测试程序如下

#include <iostream>
#include <vector>

using namespace std;

class ABC
{
public:
ABC():row(0),column(0){}
ABC( ABC const& rhs){ (*this) = rhs;}
ABC( int r, int c):row(r),column(c){}

ABC& operator=( ABC const& rhs){ row=rhs.row; column=rhs.column; return *this;}
bool operator==( ABC const& rhs) const { return (row == rhs.row) && (column == rhs.column);}
bool operator!=( ABC const& rhs) const { return (row != rhs.row) || (column != rhs.column);}
bool operator <( ABC const& rhs) const { return (row < rhs.row) || ((row == rhs.row) && (column < rhs.column));}

int row;
int column;
};
typedef vector <ABC> ABCVector;

int main()
{
ABC ass1(4,6), ass2(5,7);
ABCVector abcv;
//(1)
abcv.assign(10,ass1);// put 10 copies of ass into abcv
//(2)
abcv.push_back(ass2);// put ass into abcv


///ABC out;
//ABCvector abcv;
//(1)
for( int i = 0; i < 10; i++ )
{
cout < < abcv.at(i).row < < " " < < abcv.at(i).column < < endl;
}
//(2)
cout < < abcv.back().row < < " " < < abcv.back().column < < endl;

return 0;
}

热点排行