So, std::vector is...??

Started by
26 comments, last by XTAL256 13 years, 9 months ago
What is this, exactly? How is it coded internally (seeing as how Microsoft likes to wrap everything into a DLL or LIB...)? How is it used? Why is it needed? Why is it called a vector? I'm assuming that it does not have coordinates somewhere in the class? **Insert any other questions I'm forgetting to ask...**

I started programming 25 years ago when I was 8 years old. I first heard about this thing a month or two ago. It seems to have taken the programming world by storm, so I'd like to know as many details as possible.

No, I am not a professional programmer. I'm just a hobbyist having fun...

Advertisement
Quote:What is this, exactly?
A vector is a dynamically sized array that grows with the number of elements you insert into it, and is built to behave as close to a raw array as possible.

Quote:How is it coded internally?
Templates. Because the STL containers are coded as templates, they themselves cannot be ported to a lib or dll. However, specific instantiations of these containers, such as ostream<char, type_traits<char>> for cout, clog, and cerr have been moved to the Visual Studio's C-runtime library.

Quote:How is it used?
C++ Reference on vector for samples and usage.

Quote:Why is it needed?
It is needed because programmers find themselves needing an array whose size is unknown or whose size is not known at compile time.
std::vector is not made by microsoft. It is part of the standard of C++ I believe found in the Standard Template Library and you are most likely to find it in the includes of your compiler. The vector is a sizable container. It is similar in works to a dynamic array class that provides an interface to add, subtract, and change it's objects.

Normally, if you declared a dynamic array with a pointer and allocated it's memory, in order to change it's size, you would have to delete the current array and allocate more or less memory for it. The vector does that for you.

Yo dawg, don't even trip.

Quote:Original post by boogyman19946
std::vector is not made by microsoft.


Yes, I know. My point was that Microsoft hides the code to any and all internal C/C++ ANSI and other standards in DLLs and LIBs. This means I would need to understand x86 assembly (which I don't) to see how these are implemented internally. Compilers used to provide the code along with the libraries.


Quote:Original post by boogyman19946The vector is a sizable container. It is similar in works to a dynamic array class that provides an interface to add, subtract, and change it's objects.

Normally, if you declared a dynamic array with a pointer and allocated it's memory, in order to change it's size, you would have to delete the current array and allocate more or less memory for it. The vector does that for you.


So it can replace linked lists?

No, I am not a professional programmer. I'm just a hobbyist having fun...

The main difference between a linked list and a vector is that a linked list supports insertion of objects at unusual indexing. A vector still must enter it's objects at either the front or the end of it's indexing. If you do not need to have objects in a certain order, vector is the way to go, if you do, linked list is asking for use.

Yo dawg, don't even trip.

Quote:Original post by maspeir
So it can replace linked lists?


It could. It depends on what you're looking for. Can you be limited to sequential access, and are you looking for fast insertion and deletions? If you are, then go with std::list. Do you need random accessing of elements? If you are, then go with std::vector. Do you need quick look up of an arbitrary element? If so, then go with std::set or std::map.
Looks like I'm going to have fun!

No, I am not a professional programmer. I'm just a hobbyist having fun...

Quote:Original post by maspeir
Quote:Original post by boogyman19946
std::vector is not made by microsoft.


Yes, I know. My point was that Microsoft hides the code to any and all internal C/C++ ANSI and other standards in DLLs and LIBs. This means I would need to understand x86 assembly (which I don't) to see how these are implemented internally. Compilers used to provide the code along with the libraries.


Being a templated type, they physically can't hide this code from you, it's all inside the vector header file.
I also seem to have all of the source code to the standard libs with the full version of Visual Studio. The express editions definately hide it from you though when they can.
Quote:Original post by maspeir
What is this, exactly?

It's a dynamic array.

Quote:How is it coded internally (seeing as how Microsoft likes to wrap everything into a DLL or LIB...)?

It has a pointer to the data, a number of elements that the block of data can hold and a number of elements actually being used.

Quote:How is it used? Why is it needed?

It's a container that has very fast access to an element by index, you can quickly add or remove elements at the end. It basically behaves like an array, but you can change its size dynamically. I find myself using it all the time.

Quote:Why is it called a vector? I'm assuming that it does not have coordinates somewhere in the class?

vector is just a bad name. They called it that because (3,1,5) is a 3D vector, so they call an arbitrary finite sequence a vector. For it these sequences to be vectors (by the mathematical definition) you should be able to add them, subtract them and scale them.

Quote:I started programming 25 years ago when I was 8 years old.

Me too (give or take one year).

Quote:I first heard about this thing a month or two ago. It seems to have taken the programming world by storm, so I'd like to know as many details as possible.

Have you heard of abstract data types? vector has been around for a long time. And you probably have implemented something similar at one point or another.

If you still don't see the point, I'll try to come up with a couple of examples where you may want to use one.
Quote:Yes, I know. My point was that Microsoft hides the code to any and all internal C/C++ ANSI and other standards in DLLs and LIBs. This means I would need to understand x86 assembly (which I don't) to see how these are implemented internally. Compilers used to provide the code along with the libraries.
vector is a class template, so the code should be available. (In VC++, you can just right-click on #include <vector> and open the file.)

However, you may not get that much out of looking at the source code, as SC++L containers and algorithms code is often pretty difficult to read. (This is partially due to the general conventions that tend to be used, but also specifically due to the liberal use of leading underscores to avoid symbol conflicts.)
Quote:So it can replace linked lists?
As noted previously, the SC++L includes a variety of container types (vector, deque, list, map, set, etc.), so there are plenty of container types to choose from. (In other words, vector doesn't really replace linked lists; if it were to be said to replace anything, it would be 'raw' dynamically allocated arrays.)

This topic is closed to new replies.

Advertisement