473,761 Members | 9,480 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
12 1727
On Dec 3, 3:23 pm, "Daniel T." <danie...@earth link.netwrote:
pedag...@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() ) );
}
Very nice. If its not too much clutter one could do the same as

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
{
copy( istream_iterato r< unsigned int >( istringstream (line) ),
istream_iterato r< unsigned int >(),
inserter( modpckset, modpckset.begin () ) );
//...
}
Dec 3 '07 #11
Daniel T. wrote:
[..]
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.
Really? Will it? What if it can't copy (there is 'O' instead of '0'
somewhere in the file)? Will it attempt to read again from the next
line?
[..]
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 #12
"Victor Bazarov" <v.********@com Acast.netwrote:
Daniel T. wrote:
[..]
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.

Really? Will it? What if it can't copy (there is 'O' instead of '0'
somewhere in the file)? Will it attempt to read again from the next
line?
Sorry, at some point while writing the post, I had written that I was
assuming that the file contained all unsigned ints. I must have
accidentally cut that line out.
Dec 4 '07 #13

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

Similar topics

25
2279
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
1889
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
2647
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
4751
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
6170
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
2636
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
3074
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
4933
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
9531
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
9345
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
9957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9775
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...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7332
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
5229
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...
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2752
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.