Skip to content

Instantly share code, notes, and snippets.

@drpventura
Last active August 28, 2016 21:22
Show Gist options
  • Save drpventura/47548c1166905297a8336c825046074e to your computer and use it in GitHub Desktop.
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.
#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