Boost logo

Boost :

Subject: Re: [boost] [iterator] std::sort on zip_iterator (repost)
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-05-08 15:25:36


AMDG

Jeremiah Willcock wrote:
> (Resubmitting to Boost list because I did not receive any answers on
> boost-users, nor any helpful answers on the Boost list.)
>
> I would like to sort two separate vectors of numbers, treating
> corresponding elements of the two vectors as tuples and sorting the
> tuples. In particular, I am only comparing the first elements of the
> tuples; that may or may not be relevant to my problem. An example
> program is:
>
> <snip>
>
> On gcc 4.0.1, the sort operation results in the element (2, 1) (a pair
> of corresponding elements in a and b) being duplicated, while the
> element (1, 5) does not appear in the result at all:
>
> Before: (0, 1) (2, 1) (1, 5) (2, 3) (4, 7)
> After: (0, 1) (2, 1) (2, 1) (2, 3) (4, 7)
>
> I suspect the problem is related to the fact that zip_iterator does
> not return a "real" reference when dereferenced. Am I mistaken about
> this?

You are correct.

> Is the code above supposed to work?

No.

> If not, is there an alternative way to sort one sequence while
> swapping elements in another in a corresponding way? I am not able to
> join the two input sequences into a single sequence of pairs because
> of other factors in the use case for this code. Thank you for your help.

std::sort requires a Random Access Iterator. zip_iterator is not a
Random Access Iterator.

In Christ,
Steven Watanabe


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk