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

关于vector的赋值有关问题,等

2012-02-08 
关于vector的赋值问题,急等!小弟初用vector,写了一段程序做测试用typedefdoubleseed[3]seeds[5]{{0,0,0}

关于vector的赋值问题,急等!
小弟初用vector,写了一段程序做测试用
typedef   double   seed[3];
seed   s[5]   =   {   {0,0,0},{1,1,1},{2,2,2},{3,3,3},{4,4,4}   };
std::vector <seed>   vec;
for(int   i=0;i <5;i++)
{
vec.push_back(s[i]);//提示这儿有错
}
...   ...
错误提示很奇怪,小弟从没见过:e:\program   files\microsoft   visual   studio\vc98\include\xutility(39)   :   error   C2440:   '= '   :   cannot   convert   from   'const   double   [3] '   to   'double   [3] '
                There   is   no   context   in   which   this   conversion   is   possible
                e:\program   files\microsoft   visual   studio\vc98\include\vector(170)   :   see   reference   to   function   template   instantiation   'void   __cdecl   std::fill(double   (*)[3],double   (*)[3],const   double   (&)[3]) '   being   compiled
e:\program   files\microsoft   visual   studio\vc98\include\xutility(25)   :   error   C2106:   '= '   :   left   operand   must   be   l-value
                e:\program   files\microsoft   visual   studio\vc98\include\vector(174)   :   see   reference   to   function   template   instantiation   'double   (*__cdecl   std::copy_backward(double   (*)[3],double   (*)[3],double   (*)[3]))[3] '   being   compiled
e:\program   files\microsoft   visual   studio\vc98\include\xmemory(34)   :   error   C2538:   new   :   cannot   specify   initializer   for   arrays
                e:\program   files\microsoft   visual   studio\vc98\include\xmemory(66)   :   see   reference   to   function   template   instantiation   'void   __cdecl   std::_Construct(double   (*)[3],const   double   (&)[3]) '   being   compiled
Error   executing   cl.exe.
希望能得到高手的指导,上述向量应该怎么赋值?多谢!!!

[解决办法]
typedef double seed[3];
seed s[5] = { {0,0,0},{1,1,1},{2,2,2},{3,3,3},{4,4,4} };
std::vector <seed> vec;
for(int i=0;i <5;i++)
{
vec.push_back(s[i]);//提示这儿有错
}
--------------
个人觉得用结构体为vector中的元素比较好点.

struct seed
{
double dd1;
double dd2;
double dd3;
};
std::vector <seed> vec;
for(int i=0;i <5;i++)
{
seed s;
s.dd1 = i;
s.dd2 = i;
s.dd3 = i;
vec.push_back(s);//
}



[解决办法]
用两个vector也可以实现吧

typedef std::vector <int> seed;

int main()
{
std::vector <seed> vec;
for(int i=0;i <5;i++)
{
seed tmp;
tmp.push_back(i);
tmp.push_back(i);
tmp.push_back(i);
vec.push_back(tmp);
}
}

push_back的原型是void vector::push_back(const _TYPE& _X),参数是引用类型,所以数组是不可以的

热点排行