Archived

This topic is now archived and is closed to further replies.

Mr Cucumber

C++ Vectors

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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:
    
#include <vector>
#include <iostream>

using namespace std;

void main ()
{
int vecSize = 10; // vectors can be created w/ variable sizes


vector< int > vec (vecSize); // vector with 10 elements

for (int idx=0; idx<vecSize; idx++)
vec[idx] = idx * 2; // set each item in vector

for (idx=0; idx < vecSize; idx++)
cout << vec[idx] << endl; // print them out

// no need to deallocate vector elements!

}

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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:



#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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
...as is everything in the C++ conversions of standard C libraries, i.e. cmath, cstdio, etc.

I''m trying not to use any of the ".h" standard libraries anymore. Driving my tech lead nuts.

Share this post


Link to post
Share on other sites