🔒 Locked
C++ Vectors
Advertisement
Original Post
What exactly are those vector things that exist in the C++ language?
I don''t mean vectors as vectors used in physics but the C++ stuff that you use < > with.
Vectors are a template class that represents an array. It''s part of the Standard Template Library (STL). STL has many separate parts, including iterators, containers, algorithms, etc. Vector is one of the containers, and it uses iterators and can be used with algorithms. STL is confusing and hard to learn, so get a book. Once you do learn it, it''s all worthwhile.
Here''s an example of how to use a vector of ints:
Here''s an example of how to use a vector of ints:
|
STL might be hard to learn but using vectors doesn''t seem very hard to use not if you look at yuour very good example
But I need to know if you can change the lenght of the vector after the first initialization?
But I need to know if you can change the lenght of the vector after the first initialization?
YES! the whole point of the vector is that it is dynamically sized.
use vector.push_back(T) to append a item to the vector
eg:
Edited by - POINT on June 20, 2000 5:24:40 PM
Edited by - POINT on June 20, 2000 5:25:45 PM
use vector.push_back(T) to append a item to the vector
eg:
#include < vector >
//
using namespace std;
//
vector< int > intList;
//
intList.push_back(3);
intList.push_back(5);
intList.push_back(2);
intList.push_back(rand() % 32);
//
//display the numbers using operator [ ]
cout << intList[ 0 ] << endl;
cout << intList[ 1 ] << endl;
cout << intList[ 2 ] << endl;
cout << intList[ 3 ] << endl;
//
//display them using iterators in a loop
vector< int >::iterator it;
//
for(it = intList.begin(); it != intList.end(); ++it)
{
cout << *i << endl;
}
//
Edited by - POINT on June 20, 2000 5:24:40 PM
Edited by - POINT on June 20, 2000 5:25:45 PM
Yes, as point said you can "push_back" one element at a time, and change the vector that way.
You may also call "resize (new_size)". This will grow the vector to the size you request. The vector will also still contain everything you had in it before. i.e., if you have filled elements 0 through 9, and then do a "vec.resize (20)", vec elements 0 through 9 will still be the same as they were before. I believe the rest of the elements (10 through 19) are set to 0 or 0.0. I believe you can also resize to a smaller size than the current vector to snip off the end of it (i.e. shrink it.)
Using the array brackets "[]" from the first example and "resize (new_size)", you can get pretty good use out of vector. You can also use "clear ()" to completely erase a vector. However, if you want to do more complicated things, you need to learn iterators. I''ll show you an example, but it might be confusing.
"begin ()" returns an iterator pointing to the beginning of the vector. "end ()" returns an iterator pointing past the end of the vector. It is very important to note that "end ()" does not point to an element, but "begin ()" does.
If you want to delete everything in a vector from the 10th element to the end, do it this way:
"vec.erase (vec.begin () + 10, vec.end ());"
NOTE: It is extremely inefficient to delete from anywhere but the end of a vector.
Hope this helps. STL is hard to learn, but worth every second.
You may also call "resize (new_size)". This will grow the vector to the size you request. The vector will also still contain everything you had in it before. i.e., if you have filled elements 0 through 9, and then do a "vec.resize (20)", vec elements 0 through 9 will still be the same as they were before. I believe the rest of the elements (10 through 19) are set to 0 or 0.0. I believe you can also resize to a smaller size than the current vector to snip off the end of it (i.e. shrink it.)
Using the array brackets "[]" from the first example and "resize (new_size)", you can get pretty good use out of vector. You can also use "clear ()" to completely erase a vector. However, if you want to do more complicated things, you need to learn iterators. I''ll show you an example, but it might be confusing.
"begin ()" returns an iterator pointing to the beginning of the vector. "end ()" returns an iterator pointing past the end of the vector. It is very important to note that "end ()" does not point to an element, but "begin ()" does.
If you want to delete everything in a vector from the 10th element to the end, do it this way:
"vec.erase (vec.begin () + 10, vec.end ());"
NOTE: It is extremely inefficient to delete from anywhere but the end of a vector.
Hope this helps. STL is hard to learn, but worth every second.
Whoops, just found one. http://www.sgi.com/Technology/STL/table_of_contents.html. Next time I''ll think before I post... or in this case, search.
lntakitopi@aol.com | http://geocities.com/guanajam/
lntakitopi@aol.com | http://geocities.com/guanajam/
quote:
Original post by Stoffel
using namespace std;
I think you menat namespace stl.. not std
Edited by - Magmai Kai Holmlor on June 26, 2000 9:52:37 PM
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement