473,734 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL: Could you make this snippet more efficient

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,line str);
istringstream strstreamline(l inestr);
istream_iterato r<unsigned intintstream(st rstreamline);
set<unsigned intpckset;
copy ( intstream , istream_iterato r<unsigned int>() ,
inserter(modpck set,modpckset.b egin ( ) ));
}

Thanks in advance.
Dec 3 '07 #1
12 1725
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,line str);
istringstream strstreamline(l inestr);
istream_iterato r<unsigned intintstream(st rstreamline);
set<unsigned intpckset;
What's that for?
copy ( intstream , istream_iterato r<unsigned int>() ,
inserter(modpck set,modpckset.b egin ( ) ));
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 3 '07 #2
On Dec 3, 11:37 am, "Victor Bazarov" <v.Abaza...@com Acast.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,line str);
istringstream strstreamline(l inestr);
istream_iterato r<unsigned intintstream(st rstreamline);
set<unsigned intpckset;

What's that for?
copy ( intstream , istream_iterato r<unsigned int>() ,
inserter(modpck set,modpckset.b egin ( ) ));
}

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'.
Dec 3 '07 #3
pe******@gmail. com wrote:
On Dec 3, 11:37 am, "Victor Bazarov" <v.Abaza...@com Acast.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,li nestr);
istringstre am strstreamline(l inestr);
istream_iterato r<unsigned intintstream(st rstreamline);
set<unsigne d intpckset;

What's that for?
>>copy ( intstream , istream_iterato r<unsigned int>() ,
inserter(modp ckset,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
Dec 3 '07 #4
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(); )
Hint. The for statement does not do what you think it does.
Also, why are you using a for() instead of a while() in this situation?
{
string linestr;
getline(fp,line str);
istringstream strstreamline(l inestr);
istream_iterato r<unsigned intintstream(st rstreamline);
set<unsigned intpckset;
copy ( intstream , istream_iterato r<unsigned int>() ,
inserter(modpck set,modpckset.b egin ( ) ));
}

Thanks in advance.
Dec 3 '07 #5
On Dec 3, 11:50 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
pedag...@gmail. com wrote:
On Dec 3, 11:37 am, "Victor Bazarov" <v.Abaza...@com Acast.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,lin estr);
istringstrea m strstreamline(l inestr);
istream_iterato r<unsigned intintstream(st rstreamline);
set<unsigned intpckset;
What's that for?
>copy ( intstream , istream_iterato r<unsigned int>() ,
inserter(modpc kset,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
Insightful perspective. It just me that I dont have to necessarily
deal them as integers and could consider them as strings instead.
Appreciate your help.

However, I need to store the parsed result in a container on which I
could apply set_difference algorithm. Looks like I must resort to
stringstream in this case. Please correct me if I am wrong.

Best.
Dec 3 '07 #6
On Dec 3, 11:51 am, red floyd <no.s...@here.d udewrote:
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(); )

Hint. The for statement does not do what you think it does.
Also, why are you using a for() instead of a while() in this situation?
{
string linestr;
getline(fp,line str);
istringstream strstreamline(l inestr);
istream_iterato r<unsigned intintstream(st rstreamline);
set<unsigned intpckset;
copy ( intstream , istream_iterato r<unsigned int>() ,
inserter(modpck set,modpckset.b egin ( ) ));
}
Thanks in advance.
Thank you, got it.
Dec 3 '07 #7
pe******@gmail. com wrote:
On Dec 3, 11:50 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>pedag...@gmail .com wrote:
>>On Dec 3, 11:37 am, "Victor Bazarov" <v.Abaza...@com Acast.net>
wrote:
pedag...@gma il.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.eo f();)
{
string linestr;
getline(fp, linestr);
istringstre am strstreamline(l inestr);
istream_iterato r<unsigned intintstream(st rstreamline);
set<unsigne d intpckset;
>>>What's that for?
>>>>copy ( intstream , istream_iterato r<unsigned int>() ,
inserter(mo dpckset,modpcks et.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

Insightful perspective. It just me that I dont have to necessarily
deal them as integers and could consider them as strings instead.
Appreciate your help.

However, I need to store the parsed result in a container on which I
could apply set_difference algorithm. Looks like I must resort to
stringstream in this case. Please correct me if I am wrong.
If you have to convert them, you should use some kind of conversion
function, be it strtod, sscanf, or istream::operat or>>. Actually,
it is quite possible that operator>intern ally uses strtod.

If storing them in a set is a requirements, you probably need to
do your 'copy' with the second argument being 'inserter' for some
other, non-local set.

It is possible that once the string is read, you might have better
luck using 'strtod' yourself (essentially replacing what operator>>
does, skipping WS, etc.) However, without measuring it's hard to
say what area of your algorithm you need to address.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 3 '07 #8
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,line str);
istringstream strstreamline(l inestr);
istream_iterato r<unsigned intintstream(st rstreamline);
set<unsigned intpckset;
copy ( intstream , istream_iterato r<unsigned int>() ,
inserter(modpck set,modpckset.b egin ( ) ));
}
Efficiency doesn't have much to do with number of variables, however, it
probably *is* more efficient to stream your results into a vector, sort
it, and then apply set_difference (which, of course, doesn't actually
require a std::set). One sort at the end is likely to be faster than
maintaining a sorted structure throughout the construction.

Mark
Dec 3 '07 #9
pe******@gmail. com wrote:
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,line str);
istringstream strstreamline(l inestr);
istream_iterato r<unsigned intintstream(st rstreamline);
set<unsigned intpckset;
copy ( intstream , istream_iterato r<unsigned int>() ,
inserter(modpck set,modpckset.b egin ( ) ));
}

Thanks in advance.
First let's clean up the code you have.

set< unsigned int modpckset; // has to be defined outside the loop
string line;
while ( getline( fp, line ) ) // proper way to read until end of file
{
istringstream ss( line );
copy( istream_iterato r< unsigned int >( ss ),
istream_iterato r< unsigned int >(),
inserter( modpckset, modpckset.begin () ) );
}

With the above code, getline will read in a line of the file at a time,
then parse it out to a bunch of unsigned ints. We don't need to do the
extra step:

set< unsigned int modpckset;
copy( istream_iterato r< unsigned int >( fp ),
istream_iterato r< unsigned int >(),
inserter( modpckset, modpckset.begin () ) );

The above will do the same thing.

Now, if your goal was to read each line into a separate set, say you
want to end up with a vector of sets, then you would need to read each
line separately.

vector< set< unsigned int all_sets;
string line;
while ( getline( fp, line ) )
{
istringstream ss( line );
all_sets.push_b ack( set< unsigned int >() );
copy( istream_iterato r< unsigned int >( ss ),
istream_iterato r< unsigned int >(),
inserter( all_sets.back() , all_sets.back() .begin() ) );
}
Dec 3 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
2275
by: rokia | last post by:
in a project, I use many,many stl such as stack,list,vctor etc. somewhere the vector's size is more than 2K. is this a efficient way?
9
1888
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. Here's the question: I want to be able to change the references (including deleting them). Is there any way to do that besides using pointers rather than references for the STL library? I'd also prefer to avoid using const_cast, if it is indeed...
13
1759
by: Alvin | last post by:
Correct me if I am wrong, but the STL standard only defines the interface only and not the implementation, right? For example, say I use a std::map. The speed and efficiency of the operator(key) function can vary from library-to-library? For example, one implementation of the STL C++ library, say for Linux, could be different (slower, faster, larger code, etc.) then say the STL C++ Library for Windows? If this is the case, then to use...
35
2643
by: Jon Slaughter | last post by:
I'm having a problem allocating some elements of a vector then deleting them. Basicaly I have something like this: class base { private: std::vector<object> V;
11
4747
by: ma740988 | last post by:
I'm perusing a slide with roughly 12 bullets spread across 3 pages. Each bullet reflects 'advice'. I'm ok with all but 1 bullet, more specifically the bullet that states: " Avoid the STL unless absolutely necessary " Now, I'm not acclimated with much C++ history, but something tells me this is akin to trepidation that surrounded C++ during it's inception? IOW, is this 'old school' rhetoric or ..... How do you refute this argument?
8
6169
by: olanglois | last post by:
Hi, I was asking myself to following question. What is better to erase an element from a STL map: calling (option #1) size_type erase(const key_type& k) or calling (option #2)
48
2635
by: Tony | last post by:
How much bloat does the STL produce? Is it a good design wrt code bloat? Do implementations vary much? Tony
1
3072
by: krunalbauskar | last post by:
Hi, Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. For example - <snippet> #include <iostream> #include <vector>
5
4928
by: feverzsj | last post by:
STL sort() as an implementation of introsort, below is the code snippet from the gnu stl. template<typename _RandomAccessIterator, typename _Size> void __introsort_loop(_RandomAccessIterator __first, _RandomAccessIterator __last, _Size __depth_limit) { typedef typename iterator_traits<_RandomAccessIterator>::value_type
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.