summaryrefslogtreecommitdiffhomepage
path: root/c++.html.markdown
diff options
context:
space:
mode:
Diffstat (limited to 'c++.html.markdown')
-rw-r--r--c++.html.markdown86
1 files changed, 19 insertions, 67 deletions
diff --git a/c++.html.markdown b/c++.html.markdown
index c1bacf6a..f4aa2f5a 100644
--- a/c++.html.markdown
+++ b/c++.html.markdown
@@ -149,7 +149,7 @@ namespace First {
namespace Second {
void foo()
{
- printf("This is Second::foo\n")
+ printf("This is Second::foo\n");
}
}
@@ -801,72 +801,24 @@ void doSomethingWithAFile(const std::string& filename)
// all automatically destroy their contents when they fall out of scope.
// - Mutexes using lock_guard and unique_lock
-///////////////////////////////////////
-// Lambda Expressions (C++11 and above)
-///////////////////////////////////////
-
-// lambdas are a convenient way of defining an anonymous function
-// object right at the location where it is invoked or passed as
-// an argument to a function.
-
-// For example, consider sorting a vector of pairs using the second
-// value of the pair
-
-vector<pair<int, int> > tester;
-tester.push_back(make_pair(3, 6));
-tester.push_back(make_pair(1, 9));
-tester.push_back(make_pair(5, 0));
-
-// Pass a lambda expression as third argument to the sort function
-// sort is from the <algorithm> header
-
-sort(tester.begin(), tester.end(), [](const pair<int, int>& lhs, const pair<int, int>& rhs) {
- return lhs.second < rhs.second;
- });
-
-// Notice the syntax of the lambda expression,
-// [] in the lambda is used to "capture" variables.
-// For Example:
-
-vector<int> dog_ids;
-// number_of_dogs = 3;
-for(int i = 0; i < 3; i++){
- dog_ids.push_back(i);
-}
-
-int weight[3] = {30, 50, 10};
-
-// Say you want to sort dog_ids according to the dogs' weights
-// So dog_ids should in the end become: [2, 0, 1]
-
-// Here's where lambda expressions come in handy
-
-sort(dog_ids.begin(), dog_ids.end(), [&weight](const int &lhs, const int &rhs) {
- return weight[lhs] < weight[rhs];
- });
-// Note we captured "weight" by reference in the above example.
-
-// lambda are really useful for the case of structs
-// You can use lambda expressions instead of overloading
-// the "<" operator
-
-///////////////////////////////
-// Range For (C++11 and above)
-///////////////////////////////
-
-// You can use a range for loop to iterate over a container
-int arr[] = {1, 10, 3};
-
-for(int elem: arr){
- cout << elem << endl;
-}
-
-// You can use "auto" and not worry about the type of the elements of the container
-// For example:
-
-for(auto elem: arr) {
- // Do something with each element of arr
-}
+// containers with object keys of non-primitive values (custom classes) require
+// compare function in the object itself or as a function pointer. Primitives
+// have default comparators, but you can override it.
+class Foo {
+public:
+ int j;
+ Foo(int a) : j(a) {}
+};
+struct compareFunction {
+ bool operator()(const Foo& a, const Foo& b) const {
+ return a.j < b.j;
+ }
+};
+//this isn't allowed (although it can vary depending on compiler)
+//std::map<Foo, int> fooMap;
+std::map<Foo, int, compareFunction> fooMap;
+fooMap[Foo(1)] = 1;
+fooMap.find(Foo(1)); //true
/////////////////////
// Fun stuff