矢量和for循环 [英] vector and for-loop

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

问题描述

给出以下代码:


vector< Obj> v;

.....

for(i = 0; i< v.size(); i ++)

v.some_data = 0;


是否可以进一步简化和提高上述代码的速度?

谢谢。

解决方案

它应该是:


vector< Obj> v;

.....

for(i = 0; i< v.size(); i ++)

v [i] .some_data = 0;


" Kitty" <没有垃圾邮件> ????????????? D:41 ********** @ rain.i-cable.com ...

给出以下代码:

vector< Obj> v;
....
for(i = 0; i< v.size(); i ++)
v.some_data = 0;

是吗有可能进一步简化和提高上述代码的速度吗?谢谢。





" Kitty" <没有垃圾邮件>在消息新闻中写道:41 ********** @ rain.i-cable.com ...

它应该是:

vector< Obj> ; v;
....
for(i = 0; i< v.size(); i ++)
v [i] .some_data = 0;
Kitty <没有垃圾邮件> ????????????? D:41 ********** @ rain.i-cable.com ...

给出以下代码:

vector< Obj> v;
....
for(i = 0; i< v.size(); i ++)
v.some_data = 0;

是吗有可能进一步简化和提高上述代码的速度吗?谢谢。




以下是imo''simpler'',但可能会或可能不会''更快'',

知道的唯一方法是计时。


#include< vector>


class Obj

{

int some_data;

public:

Obj():some_data(0)

{< br $>
}

};


int main()

{

const std :: vector< Obj> :: size_type sz(100); / *任意大小* /

std :: vector< Obj> V(SZ); / *所有元素都默认初始化* /

返回0

}


-Mike


"小猫" <没有垃圾邮件>在留言新闻中写道:41 ********** @ rain.i-cable.com ......

给出以下代码:

vector< Obj> v;
....
for(i = 0; i< v.size(); i ++)
v.some_data = 0;

是吗有可能进一步简化和提高上述代码的速度吗?




第一个问题是为什么要初始化一堆矢量元素

首先。


为什么你不等到你真正需要这些元素并创建

然后呢?这样,你就不必构建你不需要的元素。


第二个问题是这段代码需要多长时间目前的形式?

小时?分钟?秒?毫秒?如果没有至少一个估计,你就不知道是否值得任何努力使它更快地获得


Given the following code:

vector<Obj> v;
.....
for(i=0;i<v.size();i++)
v.some_data=0;

Is it possible to further simplify and improve the speed of the above code?
Thanks.

解决方案

It should be:

vector<Obj> v;
.....
for(i=0;i<v.size();i++)
v[i].some_data=0;

"Kitty" <No spam> ???g?ó?l¥ó·s?D:41**********@rain.i-cable.com...

Given the following code:

vector<Obj> v;
....
for(i=0;i<v.size();i++)
v.some_data=0;

Is it possible to further simplify and improve the speed of the above
code? Thanks.




"Kitty" <No spam> wrote in message news:41**********@rain.i-cable.com...

It should be:

vector<Obj> v;
....
for(i=0;i<v.size();i++)
v[i].some_data=0;

"Kitty" <No spam> ???g?ó?l¥ó·s?D:41**********@rain.i-cable.com...

Given the following code:

vector<Obj> v;
....
for(i=0;i<v.size();i++)
v.some_data=0;

Is it possible to further simplify and improve the speed of the above
code? Thanks.



The below is imo ''simpler'', but may or may not be ''faster'',
the only way to know is to time it.

#include <vector>

class Obj
{
int some_data;
public:
Obj() : some_data(0)
{
}
};

int main()
{
const std::vector<Obj>::size_type sz(100); /* arbitrary size */
std::vector<Obj> v(sz); /* all elements are default-initialized */
return 0
}

-Mike


"Kitty" <No spam> wrote in message news:41**********@rain.i-cable.com...

Given the following code:

vector<Obj> v;
....
for(i=0;i<v.size();i++)
v.some_data=0;

Is it possible to further simplify and improve the speed of the above
code?



The first question is why you want to initialize a bunch of vector elements
in the first place.

Why aren''t you waiting until you actually need the elements and creating
them then? That way, you don''t have to construct elements that you don''t
need.

The second question is how much time this code takes in its current form?
Hours? Minutes? Seconds? Milliseconds? Without at least an estimate, you
(and we) have no clue as to whether it''s worth any effort at all to make it
faster.


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

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