Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about vectors

Tags:

c++

c++11

vector

Before now I've been learning to code at school (vectors, char, two-dimentional arrays, nothing too fancy) and have been coding in C++, in CodeBlocks. A month ago I started studying from C++ Primer. I'm now at vector types and I have it all confused. When I used to use vectors I never included any other library and I declared the vector something like this:

#include <iostream>

int main ()
{
    int v[10];
    return 0;
}

Now a vector is defined in a much more complicated way , using the vector library, mentioning the element type in the definition and so on.

I was just wondering how is there such a big difference in subject I thought fairly easy. What's really the matter with these vectors?

like image 465
Jake Wright Avatar asked Jan 05 '18 14:01

Jake Wright


1 Answers

You are getting confused because the mathematical concept of a vector can mean a "collection of data" and that is what you were taught int v[10] was. The actual name for that in C++ (and most other languages) is an "array" not a vector.

The libraries referred to in C++ Primer have a class called "vector" which is an implementation of an array. They are similar, but not the same.

I hope that clears that up a bit. You are probably confused because you were taught that int v[10] is a vector, but it is "not really" in C++. It's an array. Use that term to refer to it. If you ever refer to it as a vector, you will confuse others and yourself.

like image 100
Kevin Anderson Avatar answered Oct 20 '22 09:10

Kevin Anderson