/
vector11.cpp
40 lines (33 loc) · 1.84 KB
/
vector11.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// -*- Mode:C++; Coding:us-ascii-unix; fill-column:132 -*-
/**********************************************************************************************************************************/
/**
@file vector11.cpp
@author Mitch Richling <https://www.mitchr.me/>
@Copyright Copyright 2000,2013,2014 by Mitch Richling. All rights reserved.
@brief Example of some C++11 goodness for STL vectors.@EOL
@Keywords Standard C++11 STL vector
@Std C++11
***********************************************************************************************************************************/
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <vector> /* STL vector C++11 */
#include <iostream> /* C++ iostream C++11 */
#include <algorithm> /* STL algorithm C++11 */
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
// You can now initialize a vector with a list
std::vector<int> demoVec04 = {0, 1, 2, 3, 4};
/* We don't need to use iterators to traverse a vector: */
std::cout << "The vector: ";
for(auto x : demoVec04)
std::cout << x << " ";
std::cout << std::endl;
// We can also assign with a list
demoVec04 = {4, 3, 2, 1, 0};
/* We can also traverse by refrence: */
for(auto &x : demoVec04)
x *= 2;
/* We can print it out with a lambda and for_each too: */
std::cout << "The vector: ";
std::for_each(demoVec04.begin(), demoVec04.end(), [] (int x) { std::cout << x << " "; });
std::cout << std::endl;
} // end main