473,466 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

STL vector VS list

in STL, why vector has an API to return the n-th element, but list does
not have such API?

http://www.sgi.com/tech/stl/Vector.html
http://www.sgi.com/tech/stl/List.html

Thank you.

Jan 13 '06 #1
7 23397
si***************@gmail.com wrote:
in STL, why vector has an API to return the n-th element, but list does
not have such API?
Because a vector provides random access iterators, and a list does not.
http://www.sgi.com/tech/stl/Vector.html
http://www.sgi.com/tech/stl/List.html


http://www.sgi.com/tech/stl/RandomAccessIterator.html
http://www.sgi.com/tech/stl/BidirectionalIterator.html

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Jan 13 '06 #2
si***************@gmail.com wrote:
in STL, why vector has an API to return the n-th element, but list does
not have such API?
[..]


According to the design intent, 'list' is not a container that allows
random access to its elements. It's designed to allow quick insertions
and deletions, while not invalidating any references or iterators except
the ones to the deleted element. Providing efficient random access to
elements would not be possible. If you need random access to elements
of a container, use 'vector'. If you need robust and fast insertions
in any part of the container, use 'list'.

Read more books.

V
Jan 13 '06 #3
si***************@gmail.com wrote:
in STL, why vector has an API to return the n-th element, but list does
not have such API?

http://www.sgi.com/tech/stl/Vector.html
http://www.sgi.com/tech/stl/List.html

Thank you.

A vector is a container that supports random access. A list is not.
It's as simple as that.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Jan 13 '06 #4
si***************@gmail.com wrote in news:1137173258.277178.298990
@g49g2000cwa.googlegroups.com:
in STL, why vector has an API to return the n-th element, but list does
not have such API?

http://www.sgi.com/tech/stl/Vector.html
http://www.sgi.com/tech/stl/List.html


Vector is basically an array, so going to the n-th element of a vector is
only pointer arithmetic. List is a chain of nodes, so in order to get to
the n-th element, you must traverse the list from .begin() to the n-th
element.

Note that you can do something very similar by using std::advance on an
iterator into the list.

Jan 13 '06 #5
On 2006-01-13, si***************@gmail.com
<si***************@gmail.com> wrote:
in STL, why vector has an API to return the n-th element, but
list does not have such API?


The standard containers attempt to provide an interface that's
reasonably efficient. If an efficient implementation is not
possible for a given container, it does not provide it.
A list could offer an operator[] interface that provides random
access, but it would be very inefficient, so it does not.

The same type of reasoning dicates that vector provide push_back,
but not push_front, while deque provides both.

It's more of a guideline than a rule, though. There's usually a
way to get the inefficient behavior if you really want it.

--
Neil Cerutti
Jan 13 '06 #6
Artie Gold wrote:
si***************@gmail.com wrote:
in STL, why vector has an API to return the n-th element, but list does
not have such API?

http://www.sgi.com/tech/stl/Vector.html
http://www.sgi.com/tech/stl/List.html

Thank you.

A vector is a container that supports random access. A list is not.
It's as simple as that.


One could quite easily implement access to the nth element on a list.
But it was deliberately omitted.

Why?

Because it would not be efficient. The list would perform n operations,
while a vector could go straight to the element in one operation.
Providing an operator[] would just tempt people (who didn't get around
to reading every minutiae on the subject) to actually use it and
inadvertently write inefficient code.

Jan 13 '06 #7
You already got good answers, but here's a question for you: describe
how you would implement the operator [] ?

Background information:

- std::vector is fundamentally, an array
- std::list is fundamentally, a linked list

For an array, providing [] is trivial. For list, it is not-so trivial
to do efficiently. If you have any good ideas I'm all ears.

I don't expect any reasonably efficient solution to be found at this
time, but, it would be interesting to discuss any approach you come up
with, because, once upon a time I put some thinking into the very same
question and came up with compromises and tradeoffs between various
aspects of the implementation. It all boils down to the fundamentals of
what you expect from array and linked list.

It would make more sense to ask what use is container which is
jack-of-all-trades but master of none. The answer won't be all black
and white, that's why there are containers like std::map, std::deque
and so on.

Let's take a look at container, where we have [], doing pointer
arithmetic is going to be hard to beat:

struct container {
type* base;
type& operator [] (int index) { return base[index]; }
};

Now, the problem with this setup is that inserting into the middle is
going to be rather cumbersome to say the least, we have to *move*
elements after the insertion point to make room for the new elements
being inserted into the sequence. That goes without saying, but you
surely see the problem this imposes?

Linked list is the next step, here's similiarly naive framework:

template <typename x>
struct container {
struct node { x data; node* next; node* prev; };
node* head;
node* tail;
};

Eg. each object has pointer to previous, and next one in the sequence.
That makes implementing [] efficiently rather tricky. That's why
std::list doesn't do [].

If you want both "traits" in one container, you start designing and see
what you trade for what. Most likely you'll be balancing between these
things:

- insert/remove runtime performance
- operator [] performance
- iteration performance
- memory footprint
- and more

Sometimes it is more critical that iteration, or some other operation
is very efficient. It all depends what's the design purpose of the
container.

Please discuss this further.

Jan 15 '06 #8

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

Similar topics

14
by: Roland Bengtsson | last post by:
I have a class Conception and I have this in a vector, it should be: vector<Conception> vek; // vector vector<Conception>::iterator vek; // iterator to vek But what if I want to have pointers...
9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
2
by: sci | last post by:
class A; void function(vector<A>* l) { .... } main(){ vector<A> aAList; function(&aAList);
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
11
by: sw | last post by:
Hi, Is it possible to insert a class <vec> which has a vector<double> member, into the vector<vec> veclist for e.g? I've been getting compilation errors when trying to insert using the vector...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
82
by: Peter Olcott | last post by:
I need std::vector like capability for several custom classes. I already discussed this extensively in the thread named ArrayList without Boxing and Unboxing. The solution was to simply create...
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
1
by: Hasan007 | last post by:
The Program This assignment will use separate compilation. You MUST use the following guidelines for the design of your program: You are given the header file TelephoneList.h which contain the...
10
by: arnuld | last post by:
It is quite an ugly hack but it is all I am able to come up with for now :-( and it does the requires work. I want to improve the program, I know you people have much better ideas ;-) /* C++...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.