Jump to content
• Advertisement

Archived

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

C++ Vectors

This topic is 6623 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

Share on other sites
Advertisement
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 #include 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

Share this 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

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 loopvector< 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

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

Share on other sites
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

Share this post

Share on other sites
Nope, he meant namespace std. Everything in the standard C++ library (aka the STL) is in namespace std.

Erik

Share this 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

Share on other sites

• Advertisement
• Advertisement

• Popular Contributors

1. 1
2. 2
frob
16
3. 3
Rutin
12
4. 4
5. 5
• Advertisement

• 13
• 12
• 59
• 14
• 15
• Forum Statistics

• Total Topics
632124
• Total Posts
3004243

×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!