Skip to Content
Mastering C++ Programming
book

Mastering C++ Programming

by Jeganathan Swaminathan
September 2017
Beginner to intermediate
384 pages
8h 4m
English
Packt Publishing
Content preview from Mastering C++ Programming

Vector 

Vector is a quite useful sequence container, and it works exactly as an array, except that the vector can grow and shrink at runtime while an array is of a fixed size. However, the data structure used under the hood in an array and vector is a plain simple built-in C/C++ style array.  

Let's look at the following example to understand vectors better:

#include <iostream>#include <vector>#include <algorithm>using namespace std;int main () {  vector<int> v = { 1, 5, 2, 4, 3 };  cout << "\nSize of vector is " << v.size() << endl;  auto pos = v.begin();  cout << "\nPrint vector elements before sorting" << endl;  while ( pos != v.end() )    cout << *pos++ << "\t";  cout << endl;  sort( v.begin(), v.end() );  pos = v.begin(); cout << "\nPrint vector ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Multi-Paradigm Programming with Modern C++

Multi-Paradigm Programming with Modern C++

Georgy Pashkov

Publisher Resources

ISBN: 9781786461629Supplemental Content