Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Could you explain std::multiset in the class definition to me?

26 views
Skip to first unread message

fl

unread,
Jun 8, 2015, 4:28:11 PM6/8/15
to
Hi,

When I read the for loop below, I feel that it is so different from the
normal integer as the index in a for loop.

It is easy to understand the first two value, i.e.

iter = items.begin();
iter != items.end();


My question is about the third, iter = items.upper_bound(*iter).

In a general for loop, the third is the step size for the variable.
Here, I do not see

items.upper_bound(*iter)

is a step from the std::multiset documentation.

My question can also be rephrased as what is the difference between

items.end()

and

items.upper_bound(*iter)



Can you help me?

Thanks



..................
class Basket {
std::multiset<Sales_item> items;



}

....

double Basket::total() const
{
double sum = 0.0; // holds the running total


for (const_iter iter = items.begin();
iter != items.end();
iter = items.upper_bound(*iter))
{
// we know there's at least one element with this key in the Basket
print_total(cout, *(iter->h), items.count(*iter));
// virtual call to net_price applies appropriate discounts, if any
sum += (*iter)->net_price(items.count(*iter));
}

Paavo Helde

unread,
Jun 8, 2015, 5:20:09 PM6/8/15
to
fl <rxj...@gmail.com> wrote in
news:13819f2e-c2cd-4afd...@googlegroups.com:

> Hi,
>
> When I read the for loop below, I feel that it is so different from
> the normal integer as the index in a for loop.

Because it is not an integer.

>
> It is easy to understand the first two value, i.e.
>
> iter = items.begin();
> iter != items.end();
>
>
> My question is about the third, iter = items.upper_bound(*iter).
>
> In a general for loop, the third is the step size for the variable.
> Here, I do not see
>
> items.upper_bound(*iter)
>
> is a step from the std::multiset documentation.

Well, it is. Reread the upper_bound documentation.

And note you left out the assignment part from that step, without that it
would not update the loop variable (iter) indeed. The full step is:

iter = items.upper_bound(*iter);

>
> My question can also be rephrased as what is the difference between
>
> items.end()
>
> and
>
> items.upper_bound(*iter)

If they were the same, then upper_bound would not need to take an
argument, for example.

hth
Paavo

Richard

unread,
Jun 8, 2015, 6:47:50 PM6/8/15
to
[Please do not mail me a copy of your followup]

fl <rxj...@gmail.com> spake the secret code
<13819f2e-c2cd-4afd...@googlegroups.com> thusly:

>In a general for loop, the third is the step size for the variable.

Maybe you have C++ confused with BASIC-PLUS's for loop:[1]

FOR I = 1 TO N STEP 3

In a general for C++ loop, the third chunk is the *iteration expression*.
<http://en.cppreference.com/w/cpp/language/for>

It is an expression that advances the iteration.

When using integral type loop variables or iterator loop variables, the
expression ++i or --i is most common and this iteration expression
updates the underlying loop variable because ++i is the same as
i = i + 1 for an integral type loop variable.

We could just as easily write something like:

for (int i = 0; i < 10; i = fn(i)) {
}

without any difficulty.

[1] See page 3-16
<http://bitsavers.trailing-edge.com/pdf/dec/pdp11/rsts/V06/DEC-11-ORBPB-A-D_BASIC-PLUS_LangMan_Jul75.pdf>
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Drew Lawson

unread,
Jun 9, 2015, 1:40:02 PM6/9/15
to
In article <13819f2e-c2cd-4afd...@googlegroups.com>
fl <rxj...@gmail.com> writes:
>Hi,
>
>When I read the for loop below, I feel that it is so different from the
>normal integer as the index in a for loop.
>
>It is easy to understand the first two value, i.e.
>
> iter = items.begin();
> iter != items.end();
>
>
>My question is about the third, iter = items.upper_bound(*iter).
>
>In a general for loop, the third is the step size for the variable.

The third expression in the 'for' is most commonly an increment or
decrement, but that is not the only thing that it can be. It can
be anything that is useful.

>Here, I do not see
>
>items.upper_bound(*iter)
>
>is a step from the std::multiset documentation.

It has the effect of a step, but not a fixed size step.
Multiset can have multiple objects that compare as equal.
The effect of
iter = items.upper_bound(*iter)

is to move to the next item which is not equal to the current one.
It is skipping duplicates.


http://www.cplusplus.com/reference/set/multiset/upper_bound/

Returns an iterator pointing to the first element in the container
which is considered to go after val.



> for (const_iter iter = items.begin();
> iter != items.end();
> iter = items.upper_bound(*iter))
> {
> // we know there's at least one element with this key in the Basket
> print_total(cout, *(iter->h), items.count(*iter));
> // virtual call to net_price applies appropriate discounts, if any
> sum += (*iter)->net_price(items.count(*iter));
> }


--
Drew Lawson For it's not the fall, but landing,
That will alter your social standing
0 new messages