Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [SIZE=7][B]Introduction to Vectors[/B][/SIZE]
- To do just about anything of interest in a program, we need a group of data to work with.
- For example, our program might need:
- [LIST]
- [*]A list of Twitter’s trending tags
- [*]A set of payment options for a car
- [*]A catalog of eBooks read over the last year
- [/LIST]
- The need for storing a collection of data is endless.
- 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?
- In this lesson, we will start with one of the simplest, and arguably the most useful, ways of storing data in C++: a vector.
- A [I]vector[/I] is a sequence of elements that you can access by index.
- [SIZE=7][B]Creating a Vector[/B][/SIZE]
- 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:
- [CODE=cpp]#include <vector>[/CODE]
- 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.
- And the syntax to create a vector looks like:
Add Comment
Please, Sign In to add comment