初始化矢量 [英] Initializing a vector

查看:60
本文介绍了初始化矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在创建时初始化这样的数组:


const double a [3] = {3.14,2.72,0.71};


可以使用向量< double>执行相同操作?


或者我可以创建一个用常量

数组的内容初始化的向量吗?

I can initialize an array like this when I create it:

const double a[3] = { 3.14, 2.72, 0.71 };

can I do the same with a vector<double> ?

or can I create a vector initialized with the contents of a constant
array ?

推荐答案

Mats Weber写道:
Mats Weber wrote:
我可以在创建时初始化这样的数组:

const double a [3] = {3.14,2.72 ,0.71};

我可以用矢量< double>做同样的事情?

或者我可以创建一个用常量
数组的内容初始化的向量吗?
I can initialize an array like this when I create it:

const double a[3] = { 3.14, 2.72, 0.71 };

can I do the same with a vector<double> ?

or can I create a vector initialized with the contents of a constant
array ?




const double a [3] = {3.14,2.72,0.71};

vector< double> v(3);

std :: copy(& a [0],& a [sizeof a / sizeof double - 1],v.begin();


- Pete



const double a[3] = { 3.14, 2.72, 0.71 };
vector<double> v(3);
std::copy(&a[0], &a[sizeof a/sizeof double - 1], v.begin();

- Pete


Pete C.写道:
Pete C. wrote:
Mats Weber写道:
Mats Weber wrote:
我可以当我创建它时初始化这样的数组:

const double a [3] = {3.14,2.72,0.71};

我可以用向量< double>?

或者我可以创建一个用常量
数组的内容初始化的向量吗?
I can initialize an array like this when I create it:

const double a[3] = { 3.14, 2.72, 0.71 };

can I do the same with a vector<double> ?

or can I create a vector initialized with the contents of a constant
array ?



const double a [3] = { 3.14,2.72,0.71};
向量< double> v(3);
std :: copy(& a [0],& a [sizeof a / sizeof double - 1],v .begin();

- 皮特



const double a[3] = { 3.14, 2.72, 0.71 };
vector<double> v(3);
std::copy(&a[0], &a[sizeof a/sizeof double - 1], v.begin();

- Pete




哎呀,忘了关闭paren:

std :: copy (& a [0],& a [sizeof a / sizeof double - 1],v.begin());


- Pete



Oops, forgot a closing paren:
std::copy(&a[0], &a[sizeof a/sizeof double - 1], v.begin());

- Pete


" Pete C."写道:
"Pete C." wrote:

Pete C.写道:

Pete C. wrote:
Mats Weber写道:
Mats Weber wrote:
我可以在创建时初始化这样的数组:

const double a [3] = {3.14,2.72,0.71};

我可以用矢量< double>做同样的事情吗? ?

或者我可以创建一个用常量
数组的内容初始化的向量吗?
I can initialize an array like this when I create it:

const double a[3] = { 3.14, 2.72, 0.71 };

can I do the same with a vector<double> ?

or can I create a vector initialized with the contents of a constant
array ?



const double a [3] = {3.14, 2.72,0.71};
向量< double> v(3);
std :: copy(& a [0],& a [sizeof a / sizeof double - 1],v.begin();

- 皮特



const double a[3] = { 3.14, 2.72, 0.71 };
vector<double> v(3);
std::copy(&a[0], &a[sizeof a/sizeof double - 1], v.begin();

- Pete



哎呀,忘了关闭paren:
std :: copy(& a [0],& a [sizeof a / sizeof double - 1],v。 begin());



Oops, forgot a closing paren:
std::copy(&a[0], &a[sizeof a/sizeof double - 1], v.begin());




这会删除数组的最后一个元素。它还会硬编码数组的类型

。这是一个改进:


std :: copy(a,a + sizeof(a)/ sizeof(* a),v.begin());


更好的是,使用vector'的默认构造函数,这样你就可以在初始化程序中更改

元素数而无需更改

构造函数调用:


vector< double> v;

std :: copy(a,a + sizeof(a)/ sizeof(* a),inserter (v,v.end());


更好的是,使用处理范围的向量构造函数:


向量v(a, a + sizeof(a)/ sizeof(* a));


-


Pete Becker

Dinkumware,Ltd。( http://www.dinkumware.com


这篇关于初始化矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆