STL:你能否让这个片段更有效率 [英] STL: Could you make this snippet more efficient

查看:84
本文介绍了STL:你能否让这个片段更有效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的comp.lang.c ++,


你能让这个片段效率更高吗?如你所见,我在代码中引入了太多

变量。


//在STL中逐行读取文件中的整数集设置

// fp是预先定义的

for(;!fp.eof();)

{

string linestr;

getline(fp,linestr);

istringstream strstreamline(linestr);

istream_iterator< unsigned intintstream(strstreamline);

set< unsigned intpckset;

copy(intstream,istream_iterator< unsigned int>(),

inserter(modpckset,modpckset.begin() ));

}


提前致谢。

解决方案

< a href =mailto:pe ****** @ gmail.com> pe ****** @ gmail.com 写道:


亲爱的comp.lang.c ++,


你能让这个片段效率更高吗?如你所见,我在代码中引入了太多

变量。


//在STL中逐行读取文件中的整数集设置

// fp是预先定义的

for(;!fp.eof();)

{

string linestr;

getline(fp,linestr);

istringstream strstreamline(linestr);

istream_iterator< unsigned intintstream(strstreamline);

set< unsigned intpckset;



这是为了什么?


copy(intstream,istream_iterator< unsigned int>() ,

inserter(modpckset,modpckset.begin()));

}



V
-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要不要问


12月3日上午11:37,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:


pedag ... @ gmail.com写道:


亲爱的comp.lang.c ++,


你能否让这个片段更有效率?如你所见,我在代码中引入了太多

变量。


//在STL集中逐行读取文件中的整数集

// fp是预先定义

for(;!fp.eof();)

{

string linestr;

getline(fp,linestr);

istringstream strstreamline(linestr);

istream_iterator< unsigned intintstream(strstreamline);

set< unsigned intpckset;



这是为了什么?


copy(intstream,istream_iterator< unsigned int>() ,

inserter(modpckset,modpckset.begin()));

}



V
-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要'不要问



*更正*'''pckset''应该是''modpckset''。


< blockquote> pe******@gmail.com 写道:


12月3日上午11:37,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:


> pedag ... @ gmail.com写道:


>>亲爱的comp.lang.c ++,


>>你能否让这个片段更有效率?如您所见,我在代码中引入了很多变量。


>> //在STL中逐行读取文件中的整数集// fp是预先定义
(;!fp.eof();)
{
字符串linestr;
getline(fp,linestr);
istringstream strstreamline(linestr) ;
istream_iterator< unsigned intintstream(strstreamline);
set< unsigned intpckset;


这是为了什么?


>> copy(intstream,istream_iterator< unsigned int>() ,
inserter(modpckset,modpckset.begin()));
}


V
-
请删除资金'' A'在回复电子邮件时
我没有回复最热门的回复,请不要问



*更正*' 'pckset''应该是''modpckset''。



好​​的。通过删除


getline(fp,linestr)之间的界限,可以提高整个代码段的效率;





}


;他们基本上没有任何会产生副作用的工作。

创建一个字符串流,从中读取内容,以及将这些未签名的内容放入本地集合中的
是浪费

CPU周期。


V

-

请删除大写''A'通过电子邮件回复

我没有回复最热门的回复,请不要问


Dear comp.lang.c++,

Could you make this snippet more efficient? As you see I have too many
variables introduced in the code.

//Read set of integers from a file on line by line basis in a STL set
//fp is pre-defined
for(;!fp.eof();)
{
string linestr;
getline(fp,linestr);
istringstream strstreamline(linestr);
istream_iterator<unsigned intintstream(strstreamline);
set<unsigned intpckset;
copy ( intstream , istream_iterator<unsigned int>() ,
inserter(modpckset,modpckset.begin ( ) ));
}

Thanks in advance.

解决方案

pe******@gmail.com wrote:

Dear comp.lang.c++,

Could you make this snippet more efficient? As you see I have too many
variables introduced in the code.

//Read set of integers from a file on line by line basis in a STL set
//fp is pre-defined
for(;!fp.eof();)
{
string linestr;
getline(fp,linestr);
istringstream strstreamline(linestr);
istream_iterator<unsigned intintstream(strstreamline);
set<unsigned intpckset;

What''s that for?

copy ( intstream , istream_iterator<unsigned int>() ,
inserter(modpckset,modpckset.begin ( ) ));
}

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


On Dec 3, 11:37 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

pedag...@gmail.com wrote:

Dear comp.lang.c++,

Could you make this snippet more efficient? As you see I have too many
variables introduced in the code.

//Read set of integers from a file on line by line basis in a STL set
//fp is pre-defined
for(;!fp.eof();)
{
string linestr;
getline(fp,linestr);
istringstream strstreamline(linestr);
istream_iterator<unsigned intintstream(strstreamline);
set<unsigned intpckset;


What''s that for?

copy ( intstream , istream_iterator<unsigned int>() ,
inserter(modpckset,modpckset.begin ( ) ));
}


V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask

*Correction* ''pckset'' should have been ''modpckset''.


pe******@gmail.com wrote:

On Dec 3, 11:37 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

>pedag...@gmail.com wrote:

>>Dear comp.lang.c++,

>>Could you make this snippet more efficient? As you see I have too
many variables introduced in the code.

>>//Read set of integers from a file on line by line basis in a STL
set //fp is pre-defined
for(;!fp.eof();)
{
string linestr;
getline(fp,linestr);
istringstream strstreamline(linestr);
istream_iterator<unsigned intintstream(strstreamline);
set<unsigned intpckset;


What''s that for?

>>copy ( intstream , istream_iterator<unsigned int>() ,
inserter(modpckset,modpckset.begin ( ) ));
}


V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


*Correction* ''pckset'' should have been ''modpckset''.

OK. The entire snippet can be made more efficient by removing
the lines between

getline(fp, linestr);

and

}

; they do essentially no work that would have side effects.
The creation of a stringstream, reading stuff from it, and
putting those unsigned ints into a local set are a waste of
CPU cycles.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于STL:你能否让这个片段更有效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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