Click to See Complete Forum and Search --> : C++: Vectors
MrNewbie
09-14-2001, 06:51 PM
In C++, have vectors replaced the need for linked lists? I should go try to create a vector of a class because I've never done it before but I'd bet you can.
If you can you could acheive the same as you could with a linked list by incrementing or decrementing an access variable instead of using next and previous pointers.
MrNewbie
09-14-2001, 07:03 PM
Hmm, it looks like you can't create vectors of objects. This segfaults.
Ignore that, I'm dumb, I forgot you can't just assign to vectors. I'll try again.
[ 14 September 2001: Message edited by: MrNewbie ]
Strike
09-14-2001, 07:06 PM
You can make a vector of pointers, probably.
Also, try defining the class within main - might be a namespace issue or something.
MrNewbie
09-14-2001, 07:11 PM
Fixed, and you can make vectors of objects:
#include <iostream>
#include <vector>
using namespace std;
class A{
public:
int i;
};
int main(void)
{
vector<A> objs;
A tmp;
for(int i = 0; i < 10; i++)
{
tmp.i = i;
objs.push_back(tmp);
}
for(int i = 0; i < 10; i++)
{
cout << objs[i].i << endl;
}
}
So I guess vectors could be a replacement for linked lists.
Super Bakemono
09-14-2001, 11:56 PM
mmm...templates...
jemfinch
09-15-2001, 01:44 AM
Templates are the only interesting (or at least the most interesting) part of C++.
Check this site out: http://www.extreme.indiana.edu/~tveldhui/papers/pepm99/
Jeremy
lazy_cod3R
09-15-2001, 01:01 PM
So I guess vectors could be a replacement for linked lists
I think c++ also has lists, its just more efficeint to use lists over vectors in some cases such as inserting in the middle of the collection, but it can be more effecient using vectors if you want direct access to an element in your collection.
[ 15 September 2001: Message edited by: lazy_cod3R ]
Strike
09-15-2001, 01:21 PM
Originally posted by lazy_cod3R:
<STRONG>I think c++ also has lists, its just more efficeint to use lists over vectors in some cases such as inserting in the middle of the collection, but it can be more effecient using vectors if you want direct access to an element in your collection.
[ 15 September 2001: Message edited by: lazy_cod3R ]</STRONG>
It does.
From "The C++ Programming Language" by Bjarne Stroustrup:
A list provides all of the member types and operations offered by vector, with the exceptions of subscripting, capacity(), and reserve().
pinoy
09-15-2001, 10:42 PM
In C++, have vectors replaced the need for linked lists? I should go try to create a vector of a class because I've never done it before but I'd bet you can.
If you can you could acheive the same as you could with a linked list by incrementing or decrementing an access variable instead of using next and previous pointers.
No, a vector is a replacement for an array instead. It is compatible to C's array (ie it is stored as a contiguous block of memory). There is also a list (doubly linked-list) class. Both are sequence containers and both have its different dis/advantages. eg Since vector is an array you can perform pointer arithmetic (random access) on it, but inserting elements in the middle of a vector is a very costly operation. On the other hand a list has to be walked sequentially to get an element, but it is easy to add elements in the middle of the list, by just modifying the back and next pointers.
Also remember that, when creating a vector of objects (not pointers), it's going to perform copies (ie call copy constructor, or assignment operator). The example above (of vector<A> ) is usually fine, since it contains primitive type members only. If there were pointers as members, you probably should write a copy constructor and assignment operator.