Hi,
If I have a std::multimap<int, int> and two iterator it1 and it2
(obtained from multimap's equal_range()), how to loop the multimap from
it1 to it2? I have the following loop but don't know if there is a
better way? Please help. Thanks.
for(it=it1; it!=m.end();)
{
.....
it ++;
if (it == it2) // is it legal?
break;
}
Tony 4 1430
There are two changes that can be made to clear up readability. First,
the traditional place for the increment is in the third part of the for
statement. So get rid of the it++ where it is and move it to there:
for(it=it1 ; it!=m.end() ; it++)
The second thing is that you are looping from it1 to it2, there's no
reason you need to compare against m.end() in the loop then later test
it against it2; it's sufficient to just put that test in the for loop:
for(it=it1 ; it!=it2 ; it++)
This also might fix a bug in you program now. I think that if what you
are searching for with equal_range isn't present in the multimap, your
code might break, depending on what you do in the omitted code. The
reason is that when the loop starts, it1 and it2 are equal, but neither
points to what you are looking for. So you do stuff as if it were
there. When you get to the end, you increment it, which is now past
it2, and it will never return. You'll run off the end of the container.
(I'm not positive about this all though.) Making the above change fixes
this if, in fact, it is broken.
Finally, the other comment I would add is that if you have something
like 'it++' as a statement (and thus don't really care whether you are
using 'it++' or '++it'), go with the prefix notation unless you find it
less readable. There will be a small improvement in the runtime speed*
because the prefix is slightly more efficient for non-primitive
datatypes. Assuming the functions are following the usual ++ semantics,
the postfix ++ must create a temporary object to return, while the
prefix ++ avoids this.
*A small improvement appears to be from about 2.5% with GCC -O2 and
about 5% with MSVC over the code I measured:
for( it=container.begin() ; it!=container.end() ; [++it or it++] )
*it = 0;
Evan wrote: *A small improvement appears to be from about 2.5% with GCC -O2 and about 5% with MSVC over the code I measured: for( it=container.begin() ; it!=container.end() ; [++it or it++] ) *it = 0;
It surprises me that an optimizing compiler can't easily recognize these
two as functionally identical. Clearly the result is dicarded at each
incrememnt. Is there more to this than meets the eye?
> It surprises me that an optimizing compiler can't easily recognize
these two as functionally identical. Clearly the result is dicarded at
each incrememnt. Is there more to this than meets the eye?
To figure out that it could, it'd have to make sure that neither the
constructor nor the destructor of the temporary have any side effects.
I don't know how difficult this is, but it'd probably take quite a bit
of work...
Evan wrote: It surprises me that an optimizing compiler can't easily recognize
these
two as functionally identical. Clearly the result is dicarded at
each
incrememnt. Is there more to this than meets the eye?
To figure out that it could, it'd have to make sure that neither the constructor nor the destructor of the temporary have any side effects. I don't know how difficult this is, but it'd probably take quite a bit of work...
Ah, good point. So I guess this can only be easily optimized for
built-in types? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kitty |
last post by:
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.
|
by: Michael Hopkins |
last post by:
Hi all
I want to create a std::vector that goes from 1 to n instead of 0 to n-1.
The only change this will have is in loops and when the vector returns
positions of elements etc. I am calling this uovec at the moment (for
Unit-Offset VECtor). I want the class to respond correctly to all usage of
STL containers and algorithms so that it is...
|
by: codefixer |
last post by:
Hello,
I am trying to understand if ITERATORS are tied to CONTAINERS.
I know the difference between 5 different or 6(Trivial, on SGI).
But what I fail to understand is how can I declare all 5 kinds of
iterators on say a vector.
OR is it that any iterator declared on Vector is Random Iterator which
has the functionality of all the others.
|
by: Allerdyce.John |
last post by:
Hi,
I am trying to compare the amount of work between using STL algorithm
VS a plain Java loop.
Let's say the class Rect has 2 attributes: area, and areaPerCent.
In Java, I just write a plain for loop with a list:
public static void calculateAreaPerCent(List rectList, float
containerArea) {
|
by: Amadeus W. M. |
last post by:
I have a vector from which I want to erase elements between iterators i,j.
If i<j, everything works as expected, but if j>i, an insertion is actually
performed. Example:
vector<double> x(10);
vector<double>::iterator i=x.begin()+2, j=x.begin()+6;
x.erase(i,j); // i<j, ok, erases 4 elements.
x.erase(j,i); // j>i, no error, just...
| |
by: Jim Langston |
last post by:
There's the thing about iterating though a map or vector when you may delete
one of the elements, where you simply assign the iterator to the map.erase()
statement or increment it if you don't. Well, I have this issue where I may
delete the map element in 3 different places:
for ( map_key_pcmissile::iterator mit = World.Missiles.begin();...
|
by: toton |
last post by:
Hi,
I want to have a vector like class with some additional functionality
(cosmetic one). So can I inherit a vector class to add the addition
function like,
CorresVector : public vector<Corres>{
public:
void addCorres(Corres& c); //it do little more than push_back
function.
}
|
by: nabh4u |
last post by:
hi,
i have a program where every thing is working properly. i have a vector with some values. i use iterators and delete a specific value in the vector. here the loop runs infinitely only for some values. i dont know what is the problem.
sample code:
vector<int>::iterator Iterator;
for( Iterator = vect.cmatch.begin();
...
|
by: xyz |
last post by:
I have to run the simulation of a trace file around (7gb contains
116million entries)...
presently i am using vector iterators to check the conditions in my
program.....
it is taking 2 days to finish whole simulation.....
my question are the map iterators are faster than vector
iterators.....
does it improve the performance....
thanks to...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| | |