Last active
August 28, 2016 21:22
-
-
Save drpventura/47548c1166905297a8336c825046074e to your computer and use it in GitHub Desktop.
STL copy function, and comparing builtin arrays with std::array container class. See the video at https://youtu.be/L3IOAsgTpu4.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <iterator> | |
#include <array> | |
using namespace std; | |
void cube(int& l) { | |
l = l * l * l; | |
} | |
int main() { | |
// int nums[] {1, 3, 5, 7, 11}; | |
// int* others; | |
// | |
// others = nums; | |
array<int, 5> nums {2, 4, 6, 8, 10}; | |
cout << "nums has size of " << nums.size() << endl; | |
nums.at(0) = 200; | |
array<int, 5> others; | |
others = nums; | |
others.at(0) = 100; | |
cout << "others = "; | |
copy(begin(others), end(others), ostream_iterator<int>(cout, " ")); | |
cout << endl; | |
cout << "num = "; | |
copy(begin(nums), end(nums), ostream_iterator<int>(cout, " ")); | |
cout << endl; | |
// nums.at(5) = 7; | |
// nums.at(-1) = 22; | |
// nums.at(6) = 13; | |
cout << "cubed nums = "; | |
for_each(begin(nums), end(nums), [](int& l){ | |
l = l * l * l; | |
}); | |
// for_each(begin(nums), end(nums), [](auto l){ | |
// cout << l << ' '; | |
// }); | |
copy(begin(nums), end(nums), ostream_iterator<int>(cout, " ")); | |
cout << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment