Skip to content

Instantly share code, notes, and snippets.

@rmccullagh
Created November 8, 2014 01:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmccullagh/78d72cbfd7cffd071693 to your computer and use it in GitHub Desktop.
Save rmccullagh/78d72cbfd7cffd071693 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <deque>
#include <algorithm>
#include <iterator>
#include <cstring>
#include <vector>
using namespace std;
// non-breaking-s-pace
#define NBSP " "
// new line because I am sick of typing
#define NL std::cout << std::endl
void __(const char* s) {
if(getenv("DEBUG"))
std::cout << s << std::endl;
}
int add_10(int a) {
return a + 10;
}
template<class T>
void VAR_DUMP(const T &x) {
NL;
copy(x.begin(), x.end(), ostream_iterator<int>(cout, NBSP));
NL;
}
int main()
{
__("getenv('USER') == 'ryan'");
if(strcmp(getenv("USER"), "Ryan") == 0)
__("YES");
else
__("NO");
__("Question 1");
// initializer list
const int a[] { 99, 0, -11, 12, 23, 45 };
deque<int> d1, d2;
copy(begin(a), end(a), front_inserter(d1));
VAR_DUMP(d1);
transform(d1.begin(), d1.end(), back_inserter(d2), add_10);
VAR_DUMP(d2);
__("Question 2");
__("Use the generate function to insert all odd numbers into a vector");
vector<int> v(10);
int i = 0;
generate(v.begin(), v.end(), [&i]() {
if(++i % 2 != 0) {
return i;
} else {
return 0;
}
});
__("Contents of vector");
VAR_DUMP(v);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment