ItzEdInYourBed

69 - C++ Vectors

Jul 12th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. [SIZE=7][B]Introduction to Vectors[/B][/SIZE]
  2. To do just about anything of interest in a program, we need a group of data to work with.
  3. For example, our program might need:
  4. [LIST]
  5. [*]A list of Twitter’s trending tags
  6. [*]A set of payment options for a car
  7. [*]A catalog of eBooks read over the last year
  8. [/LIST]
  9. The need for storing a collection of data is endless.
  10. We are familiar with data types like [ICODE]int[/ICODE] and [ICODE]double[/ICODE], but how do we store a group of [ICODE]int[/ICODE]s or a group of [ICODE]double[/ICODE]s?
  11. In this lesson, we will start with one of the simplest, and arguably the most useful, ways of storing data in C++: a vector.
  12. A [I]vector[/I] is a sequence of elements that you can access by index.
  13.  
  14. [SIZE=7][B]Creating a Vector[/B][/SIZE]
  15. The [ICODE]std::vector[/ICODE] lives in the [ICODE]<vector>[/ICODE] header. So first, we need to add this line of code at the top of the program:
  16. [CODE=cpp]#include <vector>[/CODE]
  17. For review, [ICODE]#include[/ICODE] is a preprocessor directive that tells the compiler to include whatever library that follows. In our case that is the standard [ICODE]vector[/ICODE] library.
  18.  
  19. And the syntax to create a vector looks like:
Add Comment
Please, Sign In to add comment