矢量分配 [英] vector assign
问题描述
大家好,我个人希望能够做到这样的事情:
vector< intv;
v.assign(1 ,2,5,9,8,7等等
无需通过执行v [0] = 1,v [1] = 2来手动添加元素。
等
它会使更易读的代码更快写入
某些情况。我没有看到这个功能记录在任何地方
虽然我觉得很好奇。还有另外一种方法来实现这个目标吗?
谢谢,
斯蒂芬。
Hi all, personally I''d love to be able to do something like this:
vector<intv;
v.assign(1, 2, 5, 9, 8, 7) etc
without having to manually add elements by doing v[0] = 1, v[1] = 2 ..
etc.
it would make for much more readable code that is faster to write in
some situations. I''ve not seen this feature documented anywhere
though which I find curious. is there another way to achieve this?
thanks,
stephen.
推荐答案
stephen b写道:
stephen b wrote:
它会使得更易读的代码更快写入
某些情况。我没有看到这个功能记录在任何地方
虽然我觉得很好奇。有没有另一种方法来实现这一目标?
it would make for much more readable code that is faster to write in
some situations. I''ve not seen this feature documented anywhere
though which I find curious. is there another way to achieve this?
也许使用变量参数和继承
#include< iostream>
#include < vector>
#include< stdarg.h>
#include< iterator>
template< typename T class myVec:public std :: vector< T>
{
public:
void assign(int amount,...);
};
模板< typename T void myVec< T> :: assign(int amount,...)
{
T val;
va_list vl;
va_start(vl,amount);
for(int i = 0; i< amount; i ++)
{
val = va_arg(vl,T);
push_back(val); < br $>
}
va_end(vl);
}
int main()
{
myVec< intvec;
vec.assign(3,2,1,0);
std :: copy(vec.begin(),vec.end(),std :: ostream_itera tor< int>(std :: cout,
""));
返回0;
}
$ b $bDarío
Maybe using variable arguments and inheritance
#include <iostream>
#include <vector>
#include <stdarg.h>
#include <iterator>
template <typename T class myVec: public std::vector<T>
{
public:
void assign(int amount,...);
};
template <typename T void myVec<T>::assign(int amount,...)
{
T val;
va_list vl;
va_start(vl,amount);
for (int i=0;i<amount;i++)
{
val=va_arg(vl,T);
push_back(val);
}
va_end(vl);
}
int main()
{
myVec<intvec;
vec.assign(3,2,1,0);
std::copy(vec.begin(),vec.end(),std::ostream_itera tor<int>(std::cout,
" "));
return 0;
}
Darío
文章
< 25 *************** ******************* @ k37g2000hsf。 googlegroups.com>,
$ b $bDaríoGriffo< da ***************** @ gmail.comwrote:
In article
<25**********************************@k37g2000hsf. googlegroups.com>,
Darío Griffo <da*****************@gmail.comwrote:
stephen b写道:
stephen b wrote:
它会使更易读的代码更快写入
某些情况。我没有看到这个功能记录在任何地方
虽然我觉得很好奇。有没有另一种方法来实现这一目标?
it would make for much more readable code that is faster to write in
some situations. I''ve not seen this feature documented anywhere
though which I find curious. is there another way to achieve this?
也许使用变量参数和继承
#include< iostream>
#include < vector>
#include< stdarg.h>
#include< iterator>
template< typename T class myVec:public std :: vector< T>
{
public:
void assign(int amount,...);
};
模板< typename T void myVec< T> :: assign(int amount,...)
{
T val;
va_list vl;
va_start(vl,amount);
for(int i = 0; i< amount; i ++)
{
val = va_arg(vl,T);
push_back(val); < br $>
}
va_end(vl);
}
int main()
{
myVec< intvec;
vec.assign(3,2,1,0);
std :: copy(vec.begin(),vec.end(),std :: ostream_itera tor< int>(std :: cout,
""));
返回0;
}
$ b $bDarío
Maybe using variable arguments and inheritance
#include <iostream>
#include <vector>
#include <stdarg.h>
#include <iterator>
template <typename T class myVec: public std::vector<T>
{
public:
void assign(int amount,...);
};
template <typename T void myVec<T>::assign(int amount,...)
{
T val;
va_list vl;
va_start(vl,amount);
for (int i=0;i<amount;i++)
{
val=va_arg(vl,T);
push_back(val);
}
va_end(vl);
}
int main()
{
myVec<intvec;
vec.assign(3,2,1,0);
std::copy(vec.begin(),vec.end(),std::ostream_itera tor<int>(std::cout,
" "));
return 0;
}
Darío
这适用于非POD类型吗? BTW继承不是必需的。
模板< typename T>
void assign(std :: vector< T>& vec,int count,...)
{
va_list vl;
va_start(vl,count);
for(int i = 0; i!= count; ++ i)
{
vec.push_back(va_arg(vl,T));
}
va_end(vl);
}
使用命名空间std;
int main()
{
vector< intvec;
assign(vec,3,2,1,0);
copy(vec.begin(),vec.end(),ostream_iterator< int>(cout,""));
}
}
Would this work for non-POD types? BTW inheritance is not necessary.
template < typename T >
void assign( std::vector<T>& vec, int count, ... )
{
va_list vl;
va_start( vl, count );
for ( int i=0; i!= count; ++i)
{
vec.push_back( va_arg( vl, T ) );
}
va_end(vl);
}
using namespace std;
int main()
{
vector<intvec;
assign( vec, 3, 2, 1, 0 );
copy( vec.begin(), vec.end(), ostream_iterator<int>( cout, " " ) );
}
}
开5月23日下午3:40,stephen b< ker ... @ audiospillage.comwrote:
On May 23, 3:40 pm, stephen b <ker...@audiospillage.comwrote:
大家好,个人我很想能够做这样的事情:
vector< intv;
v.assign(1,2,5,9,8,7)等
Hi all, personally I''d love to be able to do something like this:
vector<intv;
v.assign(1, 2, 5, 9, 8, 7) etc
Checkout Boost'的分配库。你可以这样做:
vector< intv;
v + = 1,2,3,4,5,6,7,8,9; < br $>
或
vector< intv = list_of(1)(2)(3);
等
阿里
Checkout Boost''s Assign Library. You can do these:
vector<intv;
v += 1,2,3,4,5,6,7,8,9;
or
vector<intv = list_of(1)(2)(3);
etc.
Ali
这篇关于矢量分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!