From c3e769e4ac50d4475a530969663e073f4ff002ca Mon Sep 17 00:00:00 2001 From: Brook Zhou Date: Sat, 31 Oct 2015 16:17:58 -0700 Subject: [cpp/en] comparator function for std containers --- c++.html.markdown | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index d03092e5..6b452b1b 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -801,6 +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 +// 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 fooMap; +std::map fooMap; +fooMap[Foo(1)] = 1; +fooMap.find(Foo(1)); //true ///////////////////// // Fun stuff -- cgit v1.2.3 From fd26c8ddfb6d4bfa969b323a2e98ce1b74bc8127 Mon Sep 17 00:00:00 2001 From: John Rocamora Date: Sat, 12 Dec 2015 17:51:23 -0500 Subject: Added missing semicolon --- c++.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 6b452b1b..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"); } } -- cgit v1.2.3 From 19ead59c1fde3623bc29e1fe56f33f2587c97d3a Mon Sep 17 00:00:00 2001 From: bk2dcradle Date: Tue, 5 Jan 2016 13:36:23 +0530 Subject: Add my Changes --- c++.html.markdown | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index f4aa2f5a..31dbe064 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -820,6 +820,73 @@ std::map fooMap; fooMap[Foo(1)] = 1; fooMap.find(Foo(1)); //true +/////////////////////////////////////// +// 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 > 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 header + +sort(tester.begin(), tester.end(), [](const pair& lhs, const pair& rhs) { + return lhs.second < rhs.second; + }); + +// Notice the syntax of the lambda expression, +// [] in the lambda is used to "capture" variables. +// For Example: + +vector 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 +} + ///////////////////// // Fun stuff ///////////////////// -- cgit v1.2.3 From e6866f5a26dab28d2d1b5628fbb18139c36a5139 Mon Sep 17 00:00:00 2001 From: bk2dcradle Date: Fri, 8 Jan 2016 01:21:38 +0530 Subject: More Fixes --- c++.html.markdown | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 31dbe064..44cad665 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -840,16 +840,22 @@ tester.push_back(make_pair(5, 0)); // sort is from the header sort(tester.begin(), tester.end(), [](const pair& lhs, const pair& rhs) { - return lhs.second < rhs.second; - }); + return lhs.second < rhs.second; + }); // Notice the syntax of the lambda expression, -// [] in the lambda is used to "capture" variables. -// For Example: +// [] in the lambda is used to "capture" variables +// The "Capture List" defines what from the outside of the lambda should be available inside the function body and how. +// It can be either: +// 1. a value : [x] +// 2. a reference : [&x] +// 3. any variable currently in scope by reference [&] +// 4. same as 3, but by value [=] +// Example: vector dog_ids; // number_of_dogs = 3; -for(int i = 0; i < 3; i++){ +for(int i = 0; i < 3; i++) { dog_ids.push_back(i); } @@ -861,13 +867,10 @@ int weight[3] = {30, 50, 10}; // 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]; - }); + 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 +// More on Lambdas in C++ : http://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11 /////////////////////////////// // Range For (C++11 and above) -- cgit v1.2.3 From 32f18cd992b5b6988a3b37eaa533f8215d83fe2e Mon Sep 17 00:00:00 2001 From: Jaskamal Kainth Date: Thu, 28 Jan 2016 10:38:51 +0530 Subject: Added Tuple --- c++.html.markdown | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 44cad665..b8ab656c 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -955,6 +955,54 @@ v.push_back(Foo()); // New value is copied into the first Foo we inserted // explanation of why this works. v.swap(vector()); + +/////////////////////////////////////// +// Tuples (C++11 and above) +/////////////////////////////////////// + +#include + +// Conceptually, Tuples are similar to old data structures (C-like structs) but instead of having named data members , +// its elements are accessed by their order in the tuple. + +// We start with constructing a tuple. +// +// Packing values into tuple +auto first = make_tuple ( 10 , 'A' ) ; +const int maxN = 1e9; +int maxL = 15; +auto second = make_tuple ( maxN , maxL ) ; + +// printing elements of 'first' tuple +cout << get<0>(first) << " " << get<1>(first) << "\n"; //prints : 10 A + +// printing elements of 'second' tuple +cout << get<0>(second)<< " " << get<1>(second) << "\n"; // prints: 1000000000 15 + + +// Unpacking tuple into variables + +int first_int; +char first_char; +tie (first_int , first_char ) = first; +cout << first_int << " " << first_char << "\n"; // prints : 10 A + +// We can also create tuple like this. + +tuple third ( 11 ,'A' , 3.14141); +// tuple_size returns number of elements in a tuple (as a constexpr) + +cout << tuple_size< decltype(third)>::value << "\n"; // prints: 3 + +// tuple_cat concatenates the elements of all the tuples in the same order. + +auto concatenated_tuple = tuple_cat( first, second ,third); +// concatenated_tuple becomes = (10 , 'A' , 1e9 , 15 , 11 , 'A' , 3.14141 ) + +cout << get<0>(concatenated_tuple) << "\n"; // prints: 10 +cout << get<3>(concatenated_tuple) << "\n"; // prints: 15 +cout << get<5>(concatenated_tuple) << "\n"; // prints: 'A' + ``` Further Reading: -- cgit v1.2.3 From c805148618f5b2679d6581ff41885abc7140fd4d Mon Sep 17 00:00:00 2001 From: Jaskamal Kainth Date: Thu, 28 Jan 2016 15:26:35 +0530 Subject: [C++/en] Tuples in C++ --- c++.html.markdown | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index b8ab656c..594cf15f 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -966,25 +966,24 @@ v.swap(vector()); // its elements are accessed by their order in the tuple. // We start with constructing a tuple. -// // Packing values into tuple -auto first = make_tuple ( 10 , 'A' ) ; +auto first = make_tuple( 10 , 'A' ) ; const int maxN = 1e9; -int maxL = 15; -auto second = make_tuple ( maxN , maxL ) ; +const int maxL = 15; +auto second = make_tuple( maxN , maxL ) ; // printing elements of 'first' tuple cout << get<0>(first) << " " << get<1>(first) << "\n"; //prints : 10 A // printing elements of 'second' tuple -cout << get<0>(second)<< " " << get<1>(second) << "\n"; // prints: 1000000000 15 +cout << get<0>(second) << " " << get<1>(second) << "\n"; // prints: 1000000000 15 // Unpacking tuple into variables int first_int; char first_char; -tie (first_int , first_char ) = first; +tie(first_int , first_char ) = first; cout << first_int << " " << first_char << "\n"; // prints : 10 A // We can also create tuple like this. @@ -992,7 +991,7 @@ cout << first_int << " " << first_char << "\n"; // prints : 10 A tuple third ( 11 ,'A' , 3.14141); // tuple_size returns number of elements in a tuple (as a constexpr) -cout << tuple_size< decltype(third)>::value << "\n"; // prints: 3 +cout << tuple_size < decltype(third) >::value << "\n"; // prints: 3 // tuple_cat concatenates the elements of all the tuples in the same order. -- cgit v1.2.3 From 4a1a6857ce30f19f8c04dcca4571bb27f7dc36d0 Mon Sep 17 00:00:00 2001 From: Jaskamal Kainth Date: Thu, 28 Jan 2016 17:15:44 +0530 Subject: [C++/en] Tuple , updated --- c++.html.markdown | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 594cf15f..ea6ef034 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -967,10 +967,10 @@ v.swap(vector()); // We start with constructing a tuple. // Packing values into tuple -auto first = make_tuple( 10 , 'A' ) ; +auto first = make_tuple(10,'A') ; const int maxN = 1e9; const int maxL = 15; -auto second = make_tuple( maxN , maxL ) ; +auto second = make_tuple(maxN,maxL) ; // printing elements of 'first' tuple cout << get<0>(first) << " " << get<1>(first) << "\n"; //prints : 10 A @@ -983,12 +983,12 @@ cout << get<0>(second) << " " << get<1>(second) << "\n"; // prints: 1000000000 1 int first_int; char first_char; -tie(first_int , first_char ) = first; +tie(first_int,first_char) = first; cout << first_int << " " << first_char << "\n"; // prints : 10 A // We can also create tuple like this. -tuple third ( 11 ,'A' , 3.14141); +tuple third (11,'A',3.14141); // tuple_size returns number of elements in a tuple (as a constexpr) cout << tuple_size < decltype(third) >::value << "\n"; // prints: 3 @@ -996,7 +996,7 @@ cout << tuple_size < decltype(third) >::value << "\n"; // prints: 3 // tuple_cat concatenates the elements of all the tuples in the same order. auto concatenated_tuple = tuple_cat( first, second ,third); -// concatenated_tuple becomes = (10 , 'A' , 1e9 , 15 , 11 , 'A' , 3.14141 ) +// concatenated_tuple becomes = (10,'A',1e9,15,11,'A',3.14141) cout << get<0>(concatenated_tuple) << "\n"; // prints: 10 cout << get<3>(concatenated_tuple) << "\n"; // prints: 15 -- cgit v1.2.3 From fa2b171008061bc82cf9b35e0470eebeaecb4a26 Mon Sep 17 00:00:00 2001 From: Jaskamal Kainth Date: Thu, 28 Jan 2016 18:22:11 +0530 Subject: [C++/en] Tuple , Updated --- c++.html.markdown | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index ea6ef034..1065b9e8 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -967,10 +967,10 @@ v.swap(vector()); // We start with constructing a tuple. // Packing values into tuple -auto first = make_tuple(10,'A') ; +auto first = make_tuple(10,'A'); const int maxN = 1e9; const int maxL = 15; -auto second = make_tuple(maxN,maxL) ; +auto second = make_tuple(maxN,maxL); // printing elements of 'first' tuple cout << get<0>(first) << " " << get<1>(first) << "\n"; //prints : 10 A @@ -978,7 +978,6 @@ cout << get<0>(first) << " " << get<1>(first) << "\n"; //prints : 10 A // printing elements of 'second' tuple cout << get<0>(second) << " " << get<1>(second) << "\n"; // prints: 1000000000 15 - // Unpacking tuple into variables int first_int; -- cgit v1.2.3 From e1016455d5e4472e7a533c8cdd6df8ae4f2e7854 Mon Sep 17 00:00:00 2001 From: ven Date: Thu, 28 Jan 2016 14:04:41 +0100 Subject: #2119 fixups --- c++.html.markdown | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 1065b9e8..82662b15 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -967,10 +967,10 @@ v.swap(vector()); // We start with constructing a tuple. // Packing values into tuple -auto first = make_tuple(10,'A'); +auto first = make_tuple(10, 'A'); const int maxN = 1e9; const int maxL = 15; -auto second = make_tuple(maxN,maxL); +auto second = make_tuple(maxN, maxL); // printing elements of 'first' tuple cout << get<0>(first) << " " << get<1>(first) << "\n"; //prints : 10 A @@ -982,20 +982,20 @@ cout << get<0>(second) << " " << get<1>(second) << "\n"; // prints: 1000000000 1 int first_int; char first_char; -tie(first_int,first_char) = first; +tie(first_int, first_char) = first; cout << first_int << " " << first_char << "\n"; // prints : 10 A // We can also create tuple like this. -tuple third (11,'A',3.14141); +tuple third(11, 'A', 3.14141); // tuple_size returns number of elements in a tuple (as a constexpr) -cout << tuple_size < decltype(third) >::value << "\n"; // prints: 3 +cout << tuple_size::value << "\n"; // prints: 3 // tuple_cat concatenates the elements of all the tuples in the same order. -auto concatenated_tuple = tuple_cat( first, second ,third); -// concatenated_tuple becomes = (10,'A',1e9,15,11,'A',3.14141) +auto concatenated_tuple = tuple_cat(first, second, third); +// concatenated_tuple becomes = (10, 'A', 1e9, 15, 11, 'A' ,3.14141) cout << get<0>(concatenated_tuple) << "\n"; // prints: 10 cout << get<3>(concatenated_tuple) << "\n"; // prints: 15 -- cgit v1.2.3 From 1bb2535efda67de687d4eb94a0f964f9c06dcd4a Mon Sep 17 00:00:00 2001 From: AndrejPetelin Date: Sun, 31 Jan 2016 16:08:28 +0100 Subject: Update c++.html.markdown v.empty() queries if v is empty while v.clear() actually clears it (or rather sets size to 0). --- c++.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 82662b15..a59b4db8 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -948,7 +948,7 @@ for (int i = 0; i < 10; ++i) // Following line sets size of v to 0, but destructors don't get called // and resources aren't released! -v.empty(); +v.clear(); v.push_back(Foo()); // New value is copied into the first Foo we inserted // Truly destroys all values in v. See section about temporary objects for -- cgit v1.2.3 From 1d562740f3d3b68fbb51a45f66ae6b60eee7b2de Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Mon, 15 Feb 2016 14:33:23 -0500 Subject: Remove a section from c++, fixes #2130 --- c++.html.markdown | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index a59b4db8..a02e7e5b 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -330,7 +330,7 @@ ECarTypes GetPreferredCarType() } // As of C++11 there is an easy way to assign a type to the enum which can be -// useful in serialization of data and converting enums back-and-forth between +// useful in serialization of data and converting enums back-and-forth between // the desired type and their respective constants enum ECarTypes : uint8_t { @@ -352,7 +352,7 @@ void WritePreferredCarTypeToFile(ECarTypes InputCarType) } // On the other hand you may not want enums to be accidentally cast to an integer -// type or to other enums so it is instead possible to create an enum class which +// type or to other enums so it is instead possible to create an enum class which // won't be implicitly converted enum class ECarTypes : uint8_t { @@ -468,7 +468,7 @@ int main() { // Inheritance: // This class inherits everything public and protected from the Dog class -// as well as private but may not directly access private members/methods +// as well as private but may not directly access private members/methods // without a public or protected method for doing so class OwnedDog : public Dog { @@ -825,10 +825,10 @@ fooMap.find(Foo(1)); //true /////////////////////////////////////// // lambdas are a convenient way of defining an anonymous function -// object right at the location where it is invoked or passed as +// 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 +// For example, consider sorting a vector of pairs using the second // value of the pair vector > tester; @@ -856,7 +856,7 @@ sort(tester.begin(), tester.end(), [](const pair& lhs, const pair dog_ids; // number_of_dogs = 3; for(int i = 0; i < 3; i++) { - dog_ids.push_back(i); + dog_ids.push_back(i); } int weight[3] = {30, 50, 10}; @@ -940,29 +940,13 @@ Foo f1; f1 = f2; -// How to truly clear a container: -class Foo { ... }; -vector v; -for (int i = 0; i < 10; ++i) - v.push_back(Foo()); - -// Following line sets size of v to 0, but destructors don't get called -// and resources aren't released! -v.clear(); -v.push_back(Foo()); // New value is copied into the first Foo we inserted - -// Truly destroys all values in v. See section about temporary objects for -// explanation of why this works. -v.swap(vector()); - - /////////////////////////////////////// // Tuples (C++11 and above) /////////////////////////////////////// #include -// Conceptually, Tuples are similar to old data structures (C-like structs) but instead of having named data members , +// Conceptually, Tuples are similar to old data structures (C-like structs) but instead of having named data members , // its elements are accessed by their order in the tuple. // We start with constructing a tuple. @@ -995,7 +979,7 @@ cout << tuple_size::value << "\n"; // prints: 3 // tuple_cat concatenates the elements of all the tuples in the same order. auto concatenated_tuple = tuple_cat(first, second, third); -// concatenated_tuple becomes = (10, 'A', 1e9, 15, 11, 'A' ,3.14141) +// concatenated_tuple becomes = (10, 'A', 1e9, 15, 11, 'A' ,3.14141) cout << get<0>(concatenated_tuple) << "\n"; // prints: 10 cout << get<3>(concatenated_tuple) << "\n"; // prints: 15 -- cgit v1.2.3