STL maps

also a question about container adaptors

Page 1 of 1

3 Replies - 1001 Views - Last Post: 25 June 2008 - 12:51 PM Rate Topic: -----

#1 The Architect 2.0  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 351
  • Joined: 22-May 08

STL maps

Posted 25 June 2008 - 11:51 AM

concerning the STL map, it is organized according to its keys, correct? and its iterators also transverse the keys(as opposed to the values)? therefore, it is impossible to retrieve a sorted list of values from a STL map?

also, wth are the point of container adaptors? from what i understand, they're essentially wrappers(i may or may not be using this term correctly) for containers. why would you use them instead of the original container?

Is This A Good Question/Topic? 0
  • +

Replies To: STL maps

#2 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: STL maps

Posted 25 June 2008 - 12:12 PM

View PostThe Architect 2.0, on 25 Jun, 2008 - 11:51 AM, said:

concerning the STL map, it is organized according to its keys, correct?

Yes, they are organized from the lowest value to the highest value.

View PostThe Architect 2.0, on 25 Jun, 2008 - 11:51 AM, said:

and its iterators also transverse the keys(as opposed to the values)? therefore, it is impossible to retrieve a sorted list of values from a STL map?


This depends. If the keys and the values are associative, then the values will be sorted. This entirely depends on how you implement the map.

View PostThe Architect 2.0, on 25 Jun, 2008 - 11:51 AM, said:

also, wth are the point of container adaptors? from what i understand, they're essentially wrappers(i may or may not be using this term correctly) for containers. why would you use them instead of the original container?


Container adapters are basically implementations of a STL container, that expand the behavior of the container.

The container that you do use, will depend on the situation.

The three main container adapters are
priority_queue - Great if you always want to get the highest value out of the data, and you don't care if the rest of the data appears to be sorted. This will implement a weak sorting system. IE, it will not spend a lot of time on sorting the actual data.
queue - Implements a first in, first out context. This is great for things like event handing and such. When you put things into the container, they will be added to the back of the line. When you take things out, you will always take things from the front of the line.
stack - Implements a last in, first out context. IE, you put things in to the back of the line, and take them out of the back of the line. This is used extensively in hardware and software, for easy storing and removal of data.
Was This Post Helpful? 0
  • +
  • -

#3 The Architect 2.0  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 351
  • Joined: 22-May 08

Re: STL maps

Posted 25 June 2008 - 12:40 PM

so, as long as the key is 'derived' from the value, it'll be sorted?

but aren't you LOSING behavior due to the 'loss' of functions from the underlying container? or are you meaning that they EMPHASIZE certain behaviors?
Was This Post Helpful? 0
  • +
  • -

#4 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: STL maps

Posted 25 June 2008 - 12:51 PM

View PostThe Architect 2.0, on 25 Jun, 2008 - 12:40 PM, said:

so, as long as the key is 'derived' from the value, it'll be sorted?
Yes

View PostThe Architect 2.0, on 25 Jun, 2008 - 12:40 PM, said:

but aren't you LOSING behavior due to the 'loss' of functions from the underlying container? or are you meaning that they EMPHASIZE certain behaviors?
Yes, you lose some of the behavior (iterators, for example). The point of the adapters is to add additional functionality that the other containers do not support, or to simplify the interface.

Take the priority_queue. Generally, if you were to use another container (say, a vector), you would have to sort the data manually, or just use a STL Algorithm. priority_queue has sorting bulk in, which can greatly simply your code. Plus, since it only implements a weak sorting algorithm, it will have better performance then a vector.

As for the other two adapters, stacks (basically a vector) and queues (basically a linked list) are used a lot in computer science. These adapters, more or less, provide a interface that is similar to concepts discussed in CS.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1