Back to Course

Introduction to C++

0% Complete
0/0 Steps
  1. C++: Basics
    5 Topics
    |
    6 Quizzes
  2. C++: Compilation and Execution
    5 Topics
    |
    6 Quizzes
  3. C++: Variables
    9 Topics
    |
    10 Quizzes
  4. C++: Conditions Operators
    5 Topics
    |
    6 Quizzes
  5. C++: Logical Operators
    3 Topics
    |
    4 Quizzes
  6. C++: Loops and Errors
    8 Topics
    |
    9 Quizzes
  7. C++: Vectors
    7 Topics
    |
    8 Quizzes
  8. C++: Functions
    1 Topic
Lesson 7, Topic 2
In Progress

C++: Vector Creation

Yasin Cakal 27 Nov 2021
Lesson Progress
0% Complete

Now that we know what vectors are, how do we create them? Let’s learn the easiest way to do so. It is necessary that a vector is created under the header <vector>. This means that std::vector is added under this header.

However, to be able to add and use vectors, you need to add a library that adds a preprocessor directive enabling the compiler to read the relevant library and create vectors. Here, the library is named vectors. To add this library, we write #include <vectors>.

The syntax used for vector creation is vector <data type> vector_name;. So, for example, you want to create a group of doubles under the name daily_profit.

Let’s add this vector to our code vector<double> daily_profit;. Without adding the line using namespace std; we may have to add std:: before the above-mentioned syntax making it: std::vector<data type> vector_name;.

Things to Remember: You can not change the data type of a vector once it is declared.

Responses

Leave a Reply

%d bloggers like this: