Click to See Complete Forum and Search --> : How do you use vectors?


dunno
11-26-2000, 08:38 PM
Am I correct that this is basically an array without a predefined size.
This is how you create them, right?
vector<data_type> variable_name;

1) how do you add new object?
2) how do you determine how long your vector is?
3) any other neet things I should know about?

------------------
"I'll get enough sleep when I'm dead."
--Left Behind, The Movie

Dru Lee Parsec
11-26-2000, 08:54 PM
Are you talking about the Vector class in the java.util package of the JDK? Let me know before I write my "How to use the Vector class".

If you are talking about the Java Vector class then download the free documentation and look at the java.util.Vector documentation. Always Read The Fine Manual first.

dunno
11-26-2000, 09:07 PM
I'm talking about C++ vectors,
#include <vector>

------------------
"I'll get enough sleep when I'm dead."
--Left Behind, The Movie

Strike
11-26-2000, 11:12 PM
There are a couple of ways you can add objects. You can do it stack-style and use push_back(). You can do it list-style and use insert(). You can even do it array-style and just use the index operator [] and the assignment operator =, or for checked access, you can use the special vector index operator at().

dunno
11-27-2000, 08:31 PM
so to add an item to the end of the list I...

vector<data_type> v;
v.push_back(my_object);

How can I determine how many items are in the 'array'? And if does it count from 0 like regular arrays?


------------------
"I'll get enough sleep when I'm dead."
--Left Behind, The Movie

lazy_cod3R
11-28-2000, 01:52 AM
to check the size you would do

vector<datatype> v;
int size=(int)v.size();


cuz i think .size() returns an unsigned int if i remember correctly.
and yeh the first element is element 0 just like an array...

i think of a vector really as an array but more functionality http://www.linuxnewbie.org/ubb/biggrin.gif.