Skip to content

Instantly share code, notes, and snippets.

@dazfuller
Created December 16, 2011 15:20
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 dazfuller/1486435 to your computer and use it in GitHub Desktop.
Save dazfuller/1486435 to your computer and use it in GitHub Desktop.
Ignoring the Voices: C++11 Example - Lambda Capture Lists
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void PrintList(const vector<int>& list)
{
for (auto i : list)
{
cout << i << " ";
}
cout << endl;
}
int main(int argc, char** argv)
{
vector<int> myList = { 4, 1, 6, 2, 13 };
PrintList(myList);
int stepCount = 0;
sort(myList.begin(), myList.end(), [&](int x, int y) -> bool { ++stepCount; return x < y; });
PrintList(myList);
cout << "List was sorted in " << stepCount << " step(s)" << endl;
}
@dazfuller
Copy link
Author

Compiled as follows with GCC 4.6.1 on Ubuntu 11.10

g++ -Wall -Werror -std=c++0x -o lambda_capture_list lambda_capture_lists.cpp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment