473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

List, set and map

When should I use each of these three containers? What is the
difference?
For list and vector, it is obvious. How about set and map?

Thanks a lot.

Jul 9 '06 #1
5 16508
ju******@gmail.com wrote:
When should I use each of these three containers? What is the
difference?
For list and vector, it is obvious. How about set and map?

Thanks a lot.
A list is a sequence. It has an ordering. Inserts and deletes
are constant time operations (it's effectively a double-linked
list).

Sets and Maps are associative containers. That is, you get
fast (n log n) location of a given element. They effectively
are trees sorted by a key. The difference between set and map
is that set just is a sorted container of single values. Maps
are pairs of keys (which is used for the indexing) and a paired
value.
Jul 9 '06 #2
In article <11**********************@75g2000cwc.googlegroups. com>,
ju******@gmail.com says...
When should I use each of these three containers? What is the
difference?
For list and vector, it is obvious. How about set and map?
A map has a key, and some data attached to that key. A set only has a
key, with nothing else attached to it.

A couple of examples: if you wanted to print out the words in a file,
sorted into alphabetical order, a set would make sense -- read the
words from the file into a set, then write them out from the set to
the output file.

If you wanted to store data about employees such as their name,
employee ID number, birthday, current pay rate, etc., but wanted to
support looking them up by name, a map would make sense. You'd use
the name as the key, and all the other "stuff" as the data attached
to that key.

Choosing between list and vector might not be as obvious as it
initially appears. It's true that a list supports inserting or
deleting in the middle of the list in constant time. It's also true,
however, that finding that spot in the middle of the list normally
takes linear time. Worse yet, where a vector uses contiguous storage,
a list normally uses non-contiguous storage. That can make it quite
slow to find a particular spot in the list. In a different direction,
std::list requires a doubly-linked list. A list with a lot of nodes
and only a small amount of data in each node can waste a great deal
of memory.

These are useful a lot less often than they might initially appear.
In fact, I'm pretty sure in the 10 years (or so) since I first
started using (then pre-standard) STL, I haven't run into a single
situation where I put an std::list to real use. I've had a couple
that might have been close calls, and I can _imagine_ some where
they'd make but it hasn't arisen in real use for me yet.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 10 '06 #3

"Jerry Coffin" <jc*****@taeus.comwrote in message
news:MP************************@news.sunsite.dk...
In article <11**********************@75g2000cwc.googlegroups. com>,
ju******@gmail.com says...
These are useful a lot less often than they might initially appear.
In fact, I'm pretty sure in the 10 years (or so) since I first
started using (then pre-standard) STL, I haven't run into a single
situation where I put an std::list to real use. I've had a couple
that might have been close calls, and I can _imagine_ some where
they'd make but it hasn't arisen in real use for me yet.
Not to disagree with your comments, but I sometimes use
a list instead of a vector when I don't need to access by
index and I don't know the size ahead of time.

Since vectors maintain contiguous memory, push_backs
can cause a lot of copying if you can't reserve ahead
of time. This is just my opinion.

At the office we constantly have discussions
about this but I find that using lists can reduce
memory fragmentation. This is sometimes worth
the overhead of the doubly linked list. It needs
benchmarking though.

If I can reserve the size or if I need to access by
index, I will use a vector. If I can't reserve the
size and don't need to access by index it depends.
Jul 10 '06 #4
Duane Hebert wrote:
"Jerry Coffin" <jc*****@taeus.comwrote in message
news:MP************************@news.sunsite.dk...
>In article <11**********************@75g2000cwc.googlegroups. com>,
ju******@gmail.com says...
>These are useful a lot less often than they might initially appear.
In fact, I'm pretty sure in the 10 years (or so) since I first
started using (then pre-standard) STL, I haven't run into a single
situation where I put an std::list to real use. I've had a couple
that might have been close calls, and I can _imagine_ some where
they'd make but it hasn't arisen in real use for me yet.

Not to disagree with your comments, but I sometimes use
a list instead of a vector when I don't need to access by
index and I don't know the size ahead of time.

Since vectors maintain contiguous memory, push_backs
can cause a lot of copying if you can't reserve ahead
of time. This is just my opinion.

At the office we constantly have discussions
about this but I find that using lists can reduce
memory fragmentation. This is sometimes worth
the overhead of the doubly linked list. It needs
benchmarking though.

If I can reserve the size or if I need to access by
index, I will use a vector. If I can't reserve the
size and don't need to access by index it depends.
Conventional wisdom dictates that vectors are faster than lists for
"most" purposes. Certainly there are cases where the copying upon
reallocation can be a severe bottleneck, but generally the contiguous
memory storage of vectors and the attendant benefits of spatial locality
provide a significant performance edge over lists.

That said, I often use lists too, notably in situations where I don't
want inserts to invalidate existing iterators.

Jul 10 '06 #5
Duane Hebert wrote:
I sometimes use a list instead of a vector when I don't need to
access by index and I don't know the size ahead of time.

Since vectors maintain contiguous memory, push_backs
can cause a lot of copying if you can't reserve ahead
of time. This is just my opinion.

At the office we constantly have discussions
about this but I find that using lists can reduce
memory fragmentation. This is sometimes worth
the overhead of the doubly linked list.
Don't forget std::deque. It has random access, but without
the contiguous memory requirement (and thus without the
copying).

HTH,
Michiel Salters

Jul 11 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: massimo | last post by:
Hey, I wrote this program which should take the numbers entered and sort them out. It doesnąt matter what order, if decreasing or increasing. I guess I'm confused in the sorting part. Anyone...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
24
by: Robin Cole | last post by:
I'd like a code review if anyone has the time. The code implements a basic skip list library for generic use. I use the following header for debug macros: /* public.h - Public declarations and...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
0
by: drewy2k12 | last post by:
Heres the story, I have to create a doubly linked list for class, and i have no clue on how to do it, i can barely create a single linked list. It has to have both a head and a tail pointer, and...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
12
by: kalyan | last post by:
Hi, I am using Linux + SysV Shared memory (sorry, but my question is all about offset + pointers and not about linux/IPC) and hence use offset's instead on pointers to store the linked list in...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.