STL list/map - rest size

T

Thomas Kowalski

Hi everyone,
I would like if there is an easy way to get the number of elements left
after an given iterator?
I have an interator called it somethere between beginning and end of an
list / map and want to know how often I have to call ++it before I
arrive at the end of the list. Means how many elements are left after
the given iterator it. My first solution (see below) is quite
inefficent.

int restsize()
{
int i=0;
T::iterator it2 = it;
for ( ;it2 != somelistormap.end(); ++it2)
{
++i;
}
return i;
}
 
V

Victor Bazarov

Thomas said:
I would like if there is an easy way to get the number of elements
left after an given iterator?

Why? What are you trying to accomplish by that?
I have an interator called it somethere between beginning and end of
an list / map and want to know how often I have to call ++it before I
arrive at the end of the list. Means how many elements are left after
the given iterator it. My first solution (see below) is quite
inefficent.

int restsize()
{
int i=0;
T::iterator it2 = it;
for ( ;it2 != somelistormap.end(); ++it2)
{
++i;
}
return i;
}

You can take the last element and then see 'std::distance', but it will
most likely do the same increment, since std::map iterators are not of
the random-access kind.

V
 
T

Thomas Kowalski

Hi Victor,
Why? What are you trying to accomplish by that?

Need it to set the size of the return datastructure X. X is specified
stupidly.
You can take the last element and then see 'std::distance', but it will
most likely do the same increment, since std::map iterators are not of
the random-access kind.

At least its a cleaner :) Thanks.

Regards,
Thomas Kowalski
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top