在一行中初始化矢量 [英] Initializing vectors in one line

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

问题描述




我正在使用STL中的vector类。通常,我逐个元素初始化值

,这非常不舒服,有时候不可能

(例如,当将常量向量传递给继承的构造函数时,因为我

在调用之前无法创建临时向量。


是否有可能在一行中分配一个向量,就像可能的那样

for数组?


我想写这样的代码:


#include< vector>

typedef std ::矢量< INT> ivec;

无效测试(ivec w){};

int main(){

//是否有可能制作这些线有效

//(例如通过扩展矢量类)?

ivec v = {1,2};

v = {3,4 };

test({5,6});

返回0;

}


至少有一种解决方法可以调用


aconstructor():inheritedconstructor({5,6}){

...

}


其中inheritedconstructor需要一个向量?

请帮助,

Emanuel

解决方案



" Emanuel Ziegler" < EZ ****** @ web.de>在消息中写道

news:c1 ********** @ news.urz.uni-heidelberg.de ...


我正在使用STL中的vector类。通常,我逐个元素地初始化值,这是非常不舒服的,有时是不可能的(例如,当将常量向量传递给继承的构造函数时,因为我在调用之前无法创建临时向量) 。

有可能在一行中分配一个向量,就像数组一样吗?

我想写这样的代码:

#include< vector>
typedef std :: vector< int> ivec;
void test(ivec w){};
int main(){
//是否有可能使这些行有效
//(例如通过扩展矢量类)?
ivec v = {1,2};
v = {3,4};
test({5,6});
返回0; <是否至少有一种解决方法可以调用

aconstructor():inheritedconstructor({5,6}){
...
}

其中inheritedconstructor需要一个向量?

请帮助,
Emanuel




如何对这个?不需要新类型的向量。


#include< vector>

typedef std :: vector< int> ivec;


const int one_two [] = {1,2};

const int three_four [] = {3,4};

const int five_six [] = {5,6};


void test(ivec w){};

int main(){

ivec v(one_two,one_two + 2);

v = ivec(three_four,three_four + 2);

test(ivec(five_six, five_six + 2));

返回0;

}


john




" Emanuel Ziegler" < EZ ****** @ web.de>在消息中写道

news:c1 ********** @ news.urz.uni-heidelberg.de ...


我正在使用STL中的vector类。通常,我逐个元素地初始化值,这是非常不舒服的,有时是不可能的(例如,当将常量向量传递给继承的构造函数时,因为我在调用之前无法创建临时向量) 。

有可能在一行中分配一个向量,就像数组一样吗?

我想写这样的代码:

#include< vector>
typedef std :: vector< int> ivec;
void test(ivec w){};
int main(){
//是否有可能使这些行有效
//(例如通过扩展矢量类)?
ivec v = {1,2};
v = {3,4};
test({5,6});
返回0;
}




你应该看到所有的矢量构造函数。

这是一个演示代码 -

#include< vector>

#include< iostream>

使用命名空间std;


int main(){

typedef vector< int> intVec;

intVec vec(10,5);


intVec :: const_iterator it;

for(it = vec。 begin(); it!= vec.end(); it ++)

cout<< *它; //打印5次。


int arr [] = {1,2,3,4};

intVec vec2(arr,arr + 4 );

for(it = vec2.begin(); it!= vec2.end(); it ++)

cout<< *它; //打印1234

}


" John Harrison" <乔************* @ hotmail.com>在消息中写道

news:c1 ************* @ ID-196037.news.uni-berlin.de


这个怎么样?不需要新类型的矢量。

#include< vector>
typedef std :: vector< int> ivec;

const int one_two [] = {1,2};
const int three_four [] = {3,4};
const int five_six [] = {5 ,6};

void test(ivec w){};
int main(){
ivec v(one_two,one_two + 2);
v = ivec(three_four,three_four + 2);
测试(ivec(five_six,five_six + 2));
返回0;
}

john




替代


v = ivec(three_four,three_four + 2);


你也可以使用:


v.assign(three_four,three_four + 2);

-

John Carson

1.要回复电子邮件地址,请删除donald

2.不要回复电子邮件地址(在此处发布)

Hi,

I am using the vector class from the STL. Normally, I initialize values
element by element, which is very uncomfortable and sometimes impossible
(e.g. when passing a constant vector to an inherited constructor, since I
cannot create a temporary vector before calling).

Is there a possibility to assign a vector in one line, like it is possible
for arrays?

I want to write code like this:

#include <vector>
typedef std::vector<int> ivec;
void test (ivec w) {};
int main () {
// Is there a possibility to make these lines valid
// (e.g. by extending the vector class)?
ivec v = {1, 2};
v = {3, 4};
test({5, 6});
return 0;
}

Is there at least a workaround for calling

aconstructor () : inheritedconstructor({5, 6}) {
...
}

where inheritedconstructor needs a vector?
Please help,
Emanuel

解决方案


"Emanuel Ziegler" <ez******@web.de> wrote in message
news:c1**********@news.urz.uni-heidelberg.de...

Hi,

I am using the vector class from the STL. Normally, I initialize values
element by element, which is very uncomfortable and sometimes impossible
(e.g. when passing a constant vector to an inherited constructor, since I
cannot create a temporary vector before calling).

Is there a possibility to assign a vector in one line, like it is possible
for arrays?

I want to write code like this:

#include <vector>
typedef std::vector<int> ivec;
void test (ivec w) {};
int main () {
// Is there a possibility to make these lines valid
// (e.g. by extending the vector class)?
ivec v = {1, 2};
v = {3, 4};
test({5, 6});
return 0;
}

Is there at least a workaround for calling

aconstructor () : inheritedconstructor({5, 6}) {
...
}

where inheritedconstructor needs a vector?
Please help,
Emanuel



How about this? No need for a new type of vector.

#include <vector>
typedef std::vector<int> ivec;

const int one_two[] = { 1, 2 };
const int three_four[] = { 3, 4 };
const int five_six[] = { 5, 6 };

void test (ivec w) {};
int main () {
ivec v(one_two, one_two + 2);
v = ivec(three_four, three_four + 2);
test(ivec(five_six, five_six + 2));
return 0;
}

john



"Emanuel Ziegler" <ez******@web.de> wrote in message
news:c1**********@news.urz.uni-heidelberg.de...

Hi,

I am using the vector class from the STL. Normally, I initialize values
element by element, which is very uncomfortable and sometimes impossible
(e.g. when passing a constant vector to an inherited constructor, since I
cannot create a temporary vector before calling).

Is there a possibility to assign a vector in one line, like it is possible
for arrays?

I want to write code like this:

#include <vector>
typedef std::vector<int> ivec;
void test (ivec w) {};
int main () {
// Is there a possibility to make these lines valid
// (e.g. by extending the vector class)?
ivec v = {1, 2};
v = {3, 4};
test({5, 6});
return 0;
}



You should probably see all the vector constructors.
Here is a demo code -

#include <vector>
#include <iostream>
using namespace std;

int main(){
typedef vector<int> intVec;
intVec vec(10, 5);

intVec::const_iterator it;
for( it=vec.begin(); it!=vec.end(); it++)
cout << *it; // Print 5 ten times.

int arr[]={1, 2, 3, 4};
intVec vec2(arr, arr+4);
for( it=vec2.begin(); it!=vec2.end(); it++)
cout << *it; // Print 1234

}


"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de


How about this? No need for a new type of vector.

#include <vector>
typedef std::vector<int> ivec;

const int one_two[] = { 1, 2 };
const int three_four[] = { 3, 4 };
const int five_six[] = { 5, 6 };

void test (ivec w) {};
int main () {
ivec v(one_two, one_two + 2);
v = ivec(three_four, three_four + 2);
test(ivec(five_six, five_six + 2));
return 0;
}

john



As an alternative to

v = ivec(three_four, three_four + 2);

you can also use:

v.assign(three_four, three_four + 2);
--
John Carson
1. To reply to email address, remove donald
2. Don''t reply to email address (post here instead)


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

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