8

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?

Bitsplease
  • 306
  • 2
  • 12
Jake Wright
  • 373
  • 1
  • 8
  • 17
    `int v[10]` is an **array** not a vector. – Code-Apprentice Jan 05 '18 at 14:36
  • 11
    What you call "vector" (assuming you mean `v` in the above example) is really an *array*. The `std::vector` template can be seen as a *dynamic array*, where you can add and remove elements when needed, at runtime. An array have a size that is fixed at compiletime. For example the array `v` in your example, it have 10 elements, and will always have 10 elements. It doesn't matter if some of them are not initialized, those elements still exists. – Some programmer dude Jan 05 '18 at 14:36
  • I don't quite understand the question *"What's really the matter with these vectors?"*. Could you clarify? – eerorika Jan 05 '18 at 14:38
  • 1
    This might help: https://stackoverflow.com/questions/15079057/arrays-vs-vectors-introductory-similarities-and-differences – Rakete1111 Jan 05 '18 at 14:38
  • 7
    That makes a lot of sense, the word for array in my native language is "vector" so that got me confused. – Jake Wright Jan 05 '18 at 14:38
  • 3
    Possible duplicate of [Arrays vs Vectors: Introductory Similarities and Differences](https://stackoverflow.com/questions/15079057/arrays-vs-vectors-introductory-similarities-and-differences) – Code-Apprentice Jan 05 '18 at 14:52
  • @BiancaStan: Can you elaborate on that? Which meaning of the English word "array" are you referring to which is best translated with the Romanian word "vector" (if that's actually the language we're talking about)? – Christian Hackl Jan 05 '18 at 14:58
  • As an additional note, the `std::array` is a more adequate container than his equivalent row array for most of the cases, because it avoid many mistakes and add useful features. – Adrian Maire Jan 05 '18 at 15:09
  • Wow. Five upvotes. Was there some "Winter of Love" announced? – Tadeusz Kopec for Ukraine Jan 05 '18 at 15:42
  • `#include` does include a header, not a library. There exist header only libraries, but even then you most likely don't include the full library. –  Jan 05 '18 at 16:01
  • btw - learn to use std::vector - its easy to use and powerful. Scan a few SO c++ questions and you will see 'use std::vector' as the standard answer – pm100 Jan 06 '18 at 00:44
  • @ChristianHackl: The one dimentional array in English is taught in Romanian schools with the term "vector", as the bidimentional array is refered to as a "matrix". Having not ever heard of the library vector type, I confused it with the "vectors" I was used to, which turned out to be one-dimentional arrays. – Jake Wright Jan 06 '18 at 17:46
  • @BiancaStan Those terms agree with what is typically taught in Math class in English-speaking universities (or at least that's the way I was taught). But while the concepts and structures are definitely similar for computers (most or all computer science departments grew out of Math departments, not engineering), the names used are different. Because of this, your confusion could have happened to a native English speaker too. – Kevin Anderson Jan 07 '18 at 03:38

3 Answers3

13

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.

Daniel H
  • 7,223
  • 2
  • 26
  • 41
Kevin Anderson
  • 6,850
  • 4
  • 32
  • 54
  • I agree that this is the source of the confusion.. But I think that in a math sense both std::vector and int v[10] are mathematical vectors. The distinction is in the properties and characteristics of the datatypes used to store the data. We as developers will use the term "array" usually to describe the primitive raw memory allocation that is v[10] and vector or DynamicArray or other name for the a class/datatype that is used to store the data.. – Rob Jan 05 '18 at 15:05
  • 4
    @Rob actually in mathematics a vector (not the `std::vector`) has a fixed size of elements, ie it lives in some vector space (eg R^3, the euclidian space) of a given dimensionality. The fact that in the c++ world the term "vector" specifically refers to "dynamic size" indeed is rather confusing – 463035818_is_not_an_ai Jan 05 '18 at 15:09
  • 2
    @Rob Maybe `std::valarray` sounds more like a "mathematical vector"? – iBug Jan 05 '18 at 15:10
  • @tobi303 semantics. Can you use v[10] and std::vector logically for the purpose of storing values as mathematical "vector"? YES.. therefore they are both (and there are other) C++ constructs to achieve this goal that is all that matters. The key from this point is to pick the construct that is the most appropriate based on the needs of the program. – Rob Jan 05 '18 at 15:11
  • 1
    While I agree with most of your points about suitability of usage @Rob, I think the original question is a pure "word usage confusion" point, rather than what containers are most appropriate for which uses for various mathematical representations and/or algorithms. That the OP said that there was a translation issue with her native speaking language and "vector" and "array" being confused with one another reinforces this view. I agree the distinctions are important when programming, but this question was simpler than that IMO. – Kevin Anderson Jan 05 '18 at 20:14
12

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."

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Don't forget [`std::valarray`](http://en.cppreference.com/w/cpp/numeric/valarray), which is more mathematical in intent, but rarely used IME. – metal Jan 08 '18 at 19:27
7

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.

iBug
  • 35,554
  • 7
  • 89
  • 134