Re: Copy contents of map into vector
From: Larry Brasfield (donotspam_larry_brasfield_at_hotmail.com)
Date: 12/10/04
- Next message: Carl Daniel [VC++ MVP]: "Re: Version of NMAKE"
- Previous message: mr.sir bossman: "ATL + WTL"
- In reply to: Daniel Lidström: "Re: Copy contents of map into vector"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 10 Dec 2004 07:19:00 -0800
"Daniel Lidström" <someone@microsoft.com> wrote in message news:1w7pg77xwo8u0$.14m7njs042ipz$.dlg@40tude.net...
> On Fri, 10 Dec 2004 01:58:52 -0800, Larry Brasfield wrote:
>
>> "Daniel Lidström" <someone@microsoft.com> wrote in message news:2oxb2p807n8r$.16oykcdeaxfrv.dlg@40tude.net...
>>> On Fri, 10 Dec 2004 09:53:24 +0100, Daniel Lidström wrote:
>>>
>>>> how can I, in the most elegant way, copy the contents of a map<int, coord>
>>>> into a vector<coord>? I'm trying to use transform but I'm not really sure
>>>> how to put it together. Something like:
>>>
>>> Hmmm... I'm not sure I'm using the right containers for the job. Let me
>>> rephrase the problem from the start. I have a list of coordinates, each
>>> with an id. I have no idea what range or order the id's come. So it may
>>> look like this:
>>>
>>> id
>>> 49 coord
>>> 61 coord
>>> 52 coord
>>> 17 coord
>>>
>>> Now as a final result, I want the coordinates in a vector, sorted in
>>> increasing id order:
>>> 17 coord
>>> 52 coord
>>> 49 coord
>>> 61 coord
>>>
>>> What would be the most elegant way to do this, using stl?
>>
>>
>> That would depend on what your performance requirements are.
>> If you put them into a vector, then sort them, you will pay a lot
>> of cycles at one time, but insert them quickly. If you put them
>> into a map, inserts will take a little longer, there will be no lumped
>> sort operation, and access would be faster if you are finding the
>> item by id.
>
> The only requirements I have is to do this as elegant as C++ is capable of.
Then std::map is your best bet. One good measure of
elegance is how minimal the code is, provided it meets
the requirements.
>> Why do you want to put them into a vector? The map is
>> already sorted on id without any extra effort. Since you
>> do not mention performance, I would guess it is not a
>> driving consideration.
>
> I don't need the map because the position in the vector determines the id
> for later use. So do you have a nice solution? I'll give you the code
> segment:
>
> bool P = FirstP(id, n, e, z);
> std::map<int, TXYZCoord> sorted_points;
> while( P ) {
> TXYZCoord point;
> point.x = n;
> point.y = e;
> point.z = z;
> sorted_points[id] = point;
> P = NextP(id, n, e, z);
> }
> std::vector<TXYZCoord> points;
>
> /*transform(sorted_points.begin(), sorted_points.end(), points.begin(),
> copy_second());*/
>
> std::map<int, TXYZCoord>::const_iterator it;
> for( it=sorted_points.begin(); it!=sorted_points.end(); ++it )
> points.push_back((*it).second);
>
>
> Since I don't know the largest id from the start I can't put points
> directly into a vector as I wouldn't know its initial size.
That is not true, especially under the constraints you've mentioned.
The vector will grow, as needed, with each push_back(). And the
code you have just shown demonstrates that.
> I don't want to
> std::sort either (feels clumsy). Do you have an alternative? I'm just
> trying to use C++ the best (for me `best' this time means elegant) way
> possible.
Your comments remind me of the aphorism, "The perfect is
the enemy of the good."
You have at least two good choices. Leave your data in the map,
keeping the code minimal, or put it into another container that can
minimize the storage ultimately required and possibly reduce the
time needed access each datum. If those performance criteria do
not count in your application, adding code for a zero-value gain
is inelegant. If that performance matters, (and the initial extra time
needed to perform the re-containing is acceptable), then the few
lines of code needed to effect that transfer are also elegant.
I do not know of anything more elegant than the map or set for
sorted data, from a coding simplicity perspective.
You may not like to hear this, but professional programmers
know it well: One measure of code "goodness" is "How much
of a burden does this code place upon the humans that may be
called upon to understand it?" Your dilemma is that you have
to make a tradeoff between different measures of goodness.
A judge once (briefly) explained to me that the word "reasonable"
in a contract is unenforcable, essentially because its meaning is
subjective. Your "elegance" has the same attribute. My effort
to bring it into the objective realm is failing, so far.
> Thanks!
Have fun.
-- --Larry Brasfield email: donotspam_larry_brasfield@hotmail.com Above views may belong only to me.
- Next message: Carl Daniel [VC++ MVP]: "Re: Version of NMAKE"
- Previous message: mr.sir bossman: "ATL + WTL"
- In reply to: Daniel Lidström: "Re: Copy contents of map into vector"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|