Jake Wright
Jake Wright

Reputation: 373

Confused about vectors

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?

Upvotes: 8

Views: 2039

Answers (3)

Kevin Anderson
Kevin Anderson

Reputation: 7010

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.

Upvotes: 13

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726509

The confusion here comes from several "name collisions" in C++ and its Standard Library.

  • C++ has built-in objects called arrays. Variable v in your program is of type "array of ten integers".
  • Other programming languages refer to arrays as vectors, prompting instructors familiar with multiple programming languages to call C++ arrays "vectors." This is what your course instructor did, even though it is inconsistent with C++ nomenclature of types.
  • C++ Standard Library defines a template class called std::vector. This is what your book calls "vectors", which is correct.
  • C++ Standard Library defines another template class called std::array to represent a fixed-size arrays. This creates a confusion with built-in C++ arrays.

To avoid confusion, refer to int v[10] as "built-in array," std::array<int, 10> v as "array container," and std::vector<int> v as "vector."

Upvotes: 12

iBug
iBug

Reputation: 37227

Because those aren't really, really vectors.

In C++, when people say "vector", they're mostly referring to an STL container, std::vector. It is essentially a dynamic array that's convenient to use and powerful.

What you have written is a simple array with a fixed length. It has very limited functionality. If you have an array that is defined to hold 10 elements, you can't change it later so that it can hold 11 elements. It's size is static.

To use std::vector container, include its corresponding header first:

#include <vector>
using std::vector;

It's a templated class so you must suggest a type when defining an instance:

vector<int> v;

You can then do various things on it, for it being a kind of a dynamic array:

v.resize(10); // Note its size is 0 at definition
v[9] = 123;
v[7] = 456;

Note you can't resize an array like that. Though dynamic allocation is an alternative, you'd soon mess up with it.

You can also copy a vector directly without writing a loop as you have to when dealing with arrays:

vector<int> v2 = v;

Vectors can also be returned from functions, while arrays can't:

vector<int> getVector(void){
    vector<int> ret = {1, 2, 3, 4};
    return ret;
}

vector<int> v = getVector();

This answer is only a vague introduction that should help you clear off the basics. You can refer to the link above for more information.

Upvotes: 7

Related Questions