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 From 281ba5b37a9e4e42d33442b0d3a062d99c964a95 Mon Sep 17 00:00:00 2001 From: Ankush goyal Date: Mon, 27 Jun 2016 02:19:51 +0530 Subject: Containers Added (#1942) * Containers Added * Text Formatting Added required spaces between text and // --- c++.html.markdown | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index a02e7e5b..6b81f95f 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -6,6 +6,7 @@ contributors: - ["Matt Kline", "https://github.com/mrkline"] - ["Geoff Liu", "http://geoffliu.me"] - ["Connor Waters", "http://github.com/connorwaters"] + - ["Ankush Goyal", "http://github.com/ankushg07"] lang: en --- @@ -985,6 +986,99 @@ cout << get<0>(concatenated_tuple) << "\n"; // prints: 10 cout << get<3>(concatenated_tuple) << "\n"; // prints: 15 cout << get<5>(concatenated_tuple) << "\n"; // prints: 'A' + +///////////////////// +// CONTAINERS +///////////////////// + +// Containers or the Standard Template Library are some predefined templates +// They manages the storage space for its elements and provide +// member functions to access and manipulate them + +// Few containers are as follows:- + +// Vectors (Dynamic arrays) +// Allow us to Define the Array or list of objects at run time +#include // will include the header file for vector +vector< Data_Type > Vector_name; // used to initialize the vector +cin>>val; +Vector_name.push_back(val); // will push the value of variable into array + +// To iterate through vector, we have 2 choices +// using normal looping +for(int i=0; i::iterator it; // initialize the iteartor for vector +for(it=vector_name.begin(); it!=vector_name.end();++it) + +// For accessing the element of the vector +// Operator [] +var= vector_name[index]; //will assign value at that index to var + + +// Set +// Sets are containers that store unique elements following a specific order +// Very useful container to store unique values in sorted order +// without any other functions or code + +#include // Will include the header file for sets +set< int > ST; // Will initialize the set of int data type +ST.insert(30); // Will insert the value 30 in set ST +ST.insert(10); // Will insert the value 10 in set ST +ST.insert(20); // Will insert the value 20 in set ST +ST.insert(30); // Will insert the value 30 in set ST +// Now elements of sets are as follows +// 10 20 30 + +// To erase an element +ST.erase(20); // Will erase element with value 20 +// Set ST: 10 30 +// To iterate through Set we use iterators +set< int >::iterator it; +for(it=ST.begin();it // Will include the header file for map +map< char, int >mymap; // Will initalize the map with key as char and value as int + +mymap.insert ( pair('A',1) ); +// Will insert value 1 for key A +mymap.insert ( pair('Z',26) ); +// Will insert value 26 for key Z + +// To iterate +map::iterator it; +for (it=mymap.begin(); it!=mymap.end(); ++it) + std::cout << it->first << "->" << it->second <<'\n'; +// Output: +// A->1 +// Z->26 + +// To find the value correponsing to a key +it = mymap.find('Z'); +cout<second; + +// OUTPUT: 26 + + ``` Further Reading: -- cgit v1.2.3 From c50ff9299651ba3c856ee23cbdabeda2784b864c Mon Sep 17 00:00:00 2001 From: Jatin Dhankhar Date: Mon, 27 Jun 2016 19:00:07 +0530 Subject: Added the Logical and bitwise operators section, fixes merge issue with #1817 (#2292) * Added the Logical and bitwise operators section * Added a note for Short Circuit evaluation Excerpt from https://en.wikipedia.org/wiki/Short-circuit_evaluation C++ uses minimal evaluation, or McCarthy evaluation (after John McCarthy (computer scientist)) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the AND function evaluates to false, the overall value must be false; and when the first argument of the OR function evaluates to true, the overall value must be true. In some programming languages (Lisp), the usual Boolean operators are short-circuit. --- c++.html.markdown | 57 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 6 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 6b81f95f..290633f3 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -7,6 +7,7 @@ contributors: - ["Geoff Liu", "http://geoffliu.me"] - ["Connor Waters", "http://github.com/connorwaters"] - ["Ankush Goyal", "http://github.com/ankushg07"] + - ["Jatin Dhankhar", "https://github.com/jatindhankhar"] lang: en --- @@ -1005,7 +1006,7 @@ cin>>val; Vector_name.push_back(val); // will push the value of variable into array // To iterate through vector, we have 2 choices -// using normal looping +// using normal looping for(int i=0; i // Will include the header file for sets @@ -1031,7 +1032,7 @@ ST.insert(20); // Will insert the value 20 in set ST ST.insert(30); // Will insert the value 30 in set ST // Now elements of sets are as follows // 10 20 30 - + // To erase an element ST.erase(20); // Will erase element with value 20 // Set ST: 10 30 @@ -1041,7 +1042,7 @@ for(it=ST.begin();it('A',1) ); mymap.insert ( pair('Z',26) ); // Will insert value 26 for key Z -// To iterate +// To iterate map::iterator it; for (it=mymap.begin(); it!=mymap.end(); ++it) std::cout << it->first << "->" << it->second <<'\n'; -// Output: +// Output: // A->1 // Z->26 @@ -1078,6 +1079,50 @@ cout<second; // OUTPUT: 26 +/////////////////////////////////// +// Logical and Bitwise operators +////////////////////////////////// + +// Most of the operators in C++ are same as in other languages + +// Logical operators + +// C++ uses Short - circuit evaluation for boolean expressions, i.e, the second argument is executed or +// evaluated only if the first argument does not suffice to determine the value of the expression + +true && false // Performs **logical and** to yield false +true || false // Performs **logical or** to yield true +! true // Performs **logcical not** to yield + +// Instead of using symbols equivalent keywords can be used +true and false // Performs **logical and** to yield false +true or false // Performs **logical or** to yield true +not true // Performs **logcical not** to yield + +// Bitwise operators + +// **<<** Left Shift Operator +// << shifts bits to the left +4 << 1 // Shifts bits of 4 to left by 1 to give 8 +// x << n can be thought as x * 2^n + + +// **>>** Right Shift Operator +// << shifts bits to the right +4 >> 1 // Shifts bits of 4 to right by 1 to give 2 +// x << n can be thought as x / 2^n + +~4 // Performs a bitwise not +4 | 3 // Performs bitwise or +4 & 3 // Performs bitwise and +4 ^ 3 // Performs bitwise xor + +// Equivalent keywords are +compl 4 // Performs a bitwise not +4 bitor 3 // Performs bitwise or +4 bitand 3 // Performs bitwise and +4 xor 3 // Performs bitwise xor + ``` Further Reading: -- cgit v1.2.3 From 47679dfcbe82811b7042a5994baab448adc15dd4 Mon Sep 17 00:00:00 2001 From: Valery Cherepanov Date: Tue, 2 Aug 2016 16:32:38 +0300 Subject: Some minor (mostly stylistic) fixes in C++ --- c++.html.markdown | 98 +++++++++++++++++++++++++++---------------------------- 1 file changed, 49 insertions(+), 49 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 290633f3..5dc1af59 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -808,8 +808,8 @@ void doSomethingWithAFile(const std::string& filename) // have default comparators, but you can override it. class Foo { public: - int j; - Foo(int a) : j(a) {} + int j; + Foo(int a) : j(a) {} }; struct compareFunction { bool operator()(const Foo& a, const Foo& b) const { @@ -948,7 +948,7 @@ f1 = f2; #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. @@ -958,10 +958,10 @@ const int maxN = 1e9; const int maxL = 15; auto second = make_tuple(maxN, maxL); -// printing elements of 'first' tuple +// Printing elements of 'first' tuple cout << get<0>(first) << " " << get<1>(first) << "\n"; //prints : 10 A -// printing elements of 'second' tuple +// Printing elements of 'second' tuple cout << get<0>(second) << " " << get<1>(second) << "\n"; // prints: 1000000000 15 // Unpacking tuple into variables @@ -989,43 +989,43 @@ cout << get<5>(concatenated_tuple) << "\n"; // prints: 'A' ///////////////////// -// CONTAINERS +// Containers ///////////////////// -// Containers or the Standard Template Library are some predefined templates -// They manages the storage space for its elements and provide -// member functions to access and manipulate them +// Containers or the Standard Template Library are some predefined templates. +// They manage the storage space for its elements and provide +// member functions to access and manipulate them. -// Few containers are as follows:- +// Few containers are as follows: -// Vectors (Dynamic arrays) +// Vector (Dynamic array) // Allow us to Define the Array or list of objects at run time -#include // will include the header file for vector -vector< Data_Type > Vector_name; // used to initialize the vector -cin>>val; +#include +vector Vector_name; // used to initialize the vector +cin >> val; Vector_name.push_back(val); // will push the value of variable into array -// To iterate through vector, we have 2 choices -// using normal looping +// To iterate through vector, we have 2 choices: +// Normal looping for(int i=0; i::iterator it; // initialize the iteartor for vector for(it=vector_name.begin(); it!=vector_name.end();++it) // For accessing the element of the vector // Operator [] -var= vector_name[index]; //will assign value at that index to var +var = vector_name[index]; // Will assign value at that index to var // Set -// Sets are containers that store unique elements following a specific order -// Very useful container to store unique values in sorted order -// without any other functions or code +// Sets are containers that store unique elements following a specific order. +// Set is a very useful container to store unique values in sorted order +// without any other functions or code. -#include // Will include the header file for sets -set< int > ST; // Will initialize the set of int data type +#include +set ST; // Will initialize the set of int data type ST.insert(30); // Will insert the value 30 in set ST ST.insert(10); // Will insert the value 10 in set ST ST.insert(20); // Will insert the value 20 in set ST @@ -1037,47 +1037,47 @@ ST.insert(30); // Will insert the value 30 in set ST ST.erase(20); // Will erase element with value 20 // Set ST: 10 30 // To iterate through Set we use iterators -set< int >::iterator it; -for(it=ST.begin();it::iterator it; +for(it=ST.begin();it // Will include the header file for map -map< char, int >mymap; // Will initalize the map with key as char and value as int +#include +map mymap; // Will initalize the map with key as char and value as int -mymap.insert ( pair('A',1) ); +mymap.insert(pair('A',1)); // Will insert value 1 for key A -mymap.insert ( pair('Z',26) ); +mymap.insert(pair('Z',26)); // Will insert value 26 for key Z // To iterate map::iterator it; for (it=mymap.begin(); it!=mymap.end(); ++it) - std::cout << it->first << "->" << it->second <<'\n'; + std::cout << it->first << "->" << it->second << '\n'; // Output: // A->1 // Z->26 // To find the value correponsing to a key it = mymap.find('Z'); -cout<second; +cout << it->second; + +// Output: 26 -// OUTPUT: 26 /////////////////////////////////// // Logical and Bitwise operators @@ -1087,17 +1087,17 @@ cout<second; // Logical operators -// C++ uses Short - circuit evaluation for boolean expressions, i.e, the second argument is executed or +// C++ uses Short-circuit evaluation for boolean expressions, i.e, the second argument is executed or // evaluated only if the first argument does not suffice to determine the value of the expression true && false // Performs **logical and** to yield false true || false // Performs **logical or** to yield true -! true // Performs **logcical not** to yield +! true // Performs **logical not** to yield false // Instead of using symbols equivalent keywords can be used true and false // Performs **logical and** to yield false -true or false // Performs **logical or** to yield true -not true // Performs **logcical not** to yield +true or false // Performs **logical or** to yield true +not true // Performs **logical not** to yield false // Bitwise operators @@ -1108,20 +1108,20 @@ not true // Performs **logcical not** to yield // **>>** Right Shift Operator -// << shifts bits to the right +// >> shifts bits to the right 4 >> 1 // Shifts bits of 4 to right by 1 to give 2 -// x << n can be thought as x / 2^n +// x >> n can be thought as x / 2^n -~4 // Performs a bitwise not +~4 // Performs a bitwise not 4 | 3 // Performs bitwise or 4 & 3 // Performs bitwise and 4 ^ 3 // Performs bitwise xor // Equivalent keywords are -compl 4 // Performs a bitwise not -4 bitor 3 // Performs bitwise or +compl 4 // Performs a bitwise not +4 bitor 3 // Performs bitwise or 4 bitand 3 // Performs bitwise and -4 xor 3 // Performs bitwise xor +4 xor 3 // Performs bitwise xor ``` -- cgit v1.2.3 From 042ed5038101d59a9d438cc2410fd9c91a343753 Mon Sep 17 00:00:00 2001 From: Andrew Chellis Date: Mon, 24 Oct 2016 03:43:42 -0400 Subject: [c++/en] Fix typo of iterator (#2501) iteartor -> iterator --- 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 5dc1af59..7dc9afbe 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -1011,7 +1011,7 @@ for(int i=0; i::iterator it; // initialize the iteartor for vector +vector::iterator it; // initialize the iterator for vector for(it=vector_name.begin(); it!=vector_name.end();++it) // For accessing the element of the vector -- cgit v1.2.3 From 1f1f59d9b4c5c194d0e0c9362d9bab951094bbc9 Mon Sep 17 00:00:00 2001 From: George Gognadze Date: Sun, 30 Oct 2016 13:58:59 +0400 Subject: c++/en typo, formatting fixes (#2531) Space should be removed. before: // concatenated_tuple becomes = (10, 'A', 1e9, 15, 11, 'A' ,3.14141) after: // concatenated_tuple becomes = (10, 'A', 1e9, 15, 11, 'A', 3.14141) Typo initialize is correct form. before: map mymap; // Will initalize the map with key as char and value as int after: map mymap; // Will initialize the map with key as char and value as int Typo corresponding is correct form. before: // To find the value correponsing to a key after: // To find the value corresponding to a key --- c++.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 7dc9afbe..24d38df7 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -981,7 +981,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 @@ -1057,7 +1057,7 @@ cout << ST.size(); // will print the size of set ST // and a mapped value, following a specific order. #include -map mymap; // Will initalize the map with key as char and value as int +map mymap; // Will initialize the map with key as char and value as int mymap.insert(pair('A',1)); // Will insert value 1 for key A @@ -1072,7 +1072,7 @@ for (it=mymap.begin(); it!=mymap.end(); ++it) // A->1 // Z->26 -// To find the value correponsing to a key +// To find the value corresponding to a key it = mymap.find('Z'); cout << it->second; -- cgit v1.2.3 From ae16d450781aecd9ff4423deb2cf67317d309080 Mon Sep 17 00:00:00 2001 From: omgee Date: Tue, 3 Jan 2017 09:41:35 +0200 Subject: [c++/en,es,it,br,ru,ch] Fix forgotten namespace std:: (#2619) --- 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 24d38df7..5220fb0b 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -456,7 +456,7 @@ void Dog::print() const Dog::~Dog() { - cout << "Goodbye " << name << "\n"; + std::cout << "Goodbye " << name << "\n"; } int main() { -- cgit v1.2.3 From eb44c5bc91e5b14a43a1a61cd62775d7b2abd008 Mon Sep 17 00:00:00 2001 From: ven Date: Thu, 18 May 2017 12:48:46 +0200 Subject: Update c++.html.markdown --- 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 5220fb0b..1461c93e 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -217,7 +217,7 @@ cout << myString + myOtherString; // "Hello World" cout << myString + " You"; // "Hello You" -// C++ strings are mutable and have value semantics. +// C++ strings are mutable. myString.append(" Dog"); cout << myString; // "Hello Dog" -- cgit v1.2.3