From 2242ad7a1796bf3c9e25a0ec977620c05ac15c58 Mon Sep 17 00:00:00 2001 From: Apoorv Choubey Date: Sat, 13 Oct 2018 22:28:57 +0530 Subject: add C++ resource --- c++.html.markdown | 2 ++ 1 file changed, 2 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 8d1c7a26..579aabf5 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -1131,3 +1131,5 @@ An up-to-date language reference can be found at Additional resources may be found at + +A tutorial covering basics of language and setting up coding environment is available at [TheChernoProject - C++](https://www.youtube.com/playlist?list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb). -- cgit v1.2.3 From 5117208ead0482ff93ee40474be7f479b880ff7f Mon Sep 17 00:00:00 2001 From: Divay Prakash Date: Sun, 14 Oct 2018 05:26:21 +0530 Subject: Fix links and list styling --- c++.html.markdown | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 579aabf5..9d6470be 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -1127,9 +1127,6 @@ compl 4 // Performs a bitwise not ``` Further Reading: -An up-to-date language reference can be found at - - -Additional resources may be found at - -A tutorial covering basics of language and setting up coding environment is available at [TheChernoProject - C++](https://www.youtube.com/playlist?list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb). +* An up-to-date language reference can be found at [CPP Reference](http://cppreference.com/w/cpp). +* Additional resources may be found at [CPlusPlus](http://cplusplus.com). +* A tutorial covering basics of language and setting up coding environment is available at [TheChernoProject - C++](https://www.youtube.com/playlist?list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb). -- cgit v1.2.3 From 3cfb769bfbc42f5e4236401707913f6725a3070c Mon Sep 17 00:00:00 2001 From: Matthew Biondi Date: Thu, 18 Oct 2018 12:26:26 -0400 Subject: Addresses #3009, corrects C++ headers description --- c++.html.markdown | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 9d6470be..8be5a278 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -71,10 +71,16 @@ void func(); // function which may accept any number of arguments // Use nullptr instead of NULL in C++ int* ip = nullptr; -// C standard headers are available in C++, -// but are prefixed with "c" and have no .h suffix. +// C standard headers are available in C++. +// C headers end in .h, while +// C++ headers are prefixed with "c" and have no ".h" suffix. + +// The C++ standard version: #include +//The C standard version: +#include + int main() { printf("Hello, world!\n"); -- cgit v1.2.3 From 129abf6113a48f928b5b8b47a54ff60184947cea Mon Sep 17 00:00:00 2001 From: Ankush Agarwal Date: Mon, 22 Oct 2018 08:44:21 -0700 Subject: Add comments about unordered_map and unordered_set --- c++.html.markdown | 2 ++ 1 file changed, 2 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 8be5a278..f1ad0790 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -1027,6 +1027,7 @@ for (it = my_vector.begin(); it != my_vector.end(); ++it) { // Set // Sets are containers that store unique elements following a specific order. +// For hash sets, use unordered_set. They are more effecient but do not preserve order. // Set is a very useful container to store unique values in sorted order // without any other functions or code. @@ -1061,6 +1062,7 @@ cout << ST.size(); // will print the size of set ST // Map // Maps store elements formed by a combination of a key value // and a mapped value, following a specific order. +// For hash maps, use unordered_map. They are more effecient but do not preserve order. #include map mymap; // Will initialize the map with key as char and value as int -- cgit v1.2.3 From 2e6208c754becae469c4056ac9ecdea40f490064 Mon Sep 17 00:00:00 2001 From: Ankush Agarwal Date: Mon, 22 Oct 2018 09:12:29 -0700 Subject: Update comments --- c++.html.markdown | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index f1ad0790..225472cb 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -1027,7 +1027,6 @@ for (it = my_vector.begin(); it != my_vector.end(); ++it) { // Set // Sets are containers that store unique elements following a specific order. -// For hash sets, use unordered_set. They are more effecient but do not preserve order. // Set is a very useful container to store unique values in sorted order // without any other functions or code. @@ -1058,11 +1057,12 @@ cout << ST.size(); // will print the size of set ST // Output: 0 // NOTE: for duplicate elements we can use multiset +// NOTE: For hash sets, use unordered_set. They are more effecient but +// do not preserve order. unordered_set is available since C++11 // Map // Maps store elements formed by a combination of a key value // and a mapped value, following a specific order. -// For hash maps, use unordered_map. They are more effecient but do not preserve order. #include map mymap; // Will initialize the map with key as char and value as int @@ -1086,6 +1086,8 @@ cout << it->second; // Output: 26 +// NOTE: For hash maps, use unordered_map. They are more effecient but do +// not preserve order. unordered_map is available since C++11. /////////////////////////////////// // Logical and Bitwise operators -- cgit v1.2.3 From 00bb4b8a70f4ddd09ce4243e817fba2c489646da Mon Sep 17 00:00:00 2001 From: Ankush Agarwal Date: Mon, 22 Oct 2018 14:13:58 -0700 Subject: efficient typo fix --- c++.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 225472cb..4113d5f4 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -1057,7 +1057,7 @@ cout << ST.size(); // will print the size of set ST // Output: 0 // NOTE: for duplicate elements we can use multiset -// NOTE: For hash sets, use unordered_set. They are more effecient but +// NOTE: For hash sets, use unordered_set. They are more efficient but // do not preserve order. unordered_set is available since C++11 // Map @@ -1086,7 +1086,7 @@ cout << it->second; // Output: 26 -// NOTE: For hash maps, use unordered_map. They are more effecient but do +// NOTE: For hash maps, use unordered_map. They are more efficient but do // not preserve order. unordered_map is available since C++11. /////////////////////////////////// -- cgit v1.2.3 From 6c4a7afacc93ccd6a36e2b50e029c9f6fbe17726 Mon Sep 17 00:00:00 2001 From: Renze Yu Date: Fri, 28 Dec 2018 23:50:16 +0800 Subject: add missing spaces after `//` in c++ --- c++.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 4113d5f4..0f7cf514 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -78,7 +78,7 @@ int* ip = nullptr; // The C++ standard version: #include -//The C standard version: +// The C standard version: #include int main() @@ -257,7 +257,7 @@ fooRef = bar; cout << &fooRef << endl; //Still prints the address of foo cout << fooRef; // Prints "I am bar" -//The address of fooRef remains the same, i.e. it is still referring to foo. +// The address of fooRef remains the same, i.e. it is still referring to foo. const string& barRef = bar; // Create a const reference to bar. @@ -822,8 +822,8 @@ struct compareFunction { return a.j < b.j; } }; -//this isn't allowed (although it can vary depending on compiler) -//std::map fooMap; +// 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 -- cgit v1.2.3 From e2ada2dc917b1c93cdb9e75795a203e08643a9c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gergely=20M=C3=A1t=C3=A9?= Date: Fri, 4 Jan 2019 13:23:05 +0100 Subject: Move section about containers its logical place Probably it was its original place, just after templates are introduced, and before first usage of container types. --- c++.html.markdown | 194 +++++++++++++++++++++++++++--------------------------- 1 file changed, 98 insertions(+), 96 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 0f7cf514..3f5a2f4e 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -809,7 +809,103 @@ 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 + +///////////////////// +// Containers +///////////////////// + +// 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: + +// Vector (Dynamic array) +// Allow us to Define the Array or list of objects at run time +#include +string val; +vector my_vector; // initialize the vector +cin >> val; +my_vector.push_back(val); // will push the value of 'val' into vector ("array") my_vector +my_vector.push_back(val); // will push the value into the vector again (now having two elements) + +// To iterate through a vector we have 2 choices: +// Either classic looping (iterating through the vector from index 0 to its last index): +for (int i = 0; i < my_vector.size(); i++) { + cout << my_vector[i] << endl; // for accessing a vector's element we can use the operator [] +} + +// or using an iterator: +vector::iterator it; // initialize the iterator for vector +for (it = my_vector.begin(); it != my_vector.end(); ++it) { + cout << *it << endl; +} + +// Set +// 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 +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 +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::iterator it; +for(it=ST.begin();it +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 +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 corresponding to a key +it = mymap.find('Z'); +cout << it->second; + +// Output: 26 + +// NOTE: For hash maps, use unordered_map. They are more efficient but do +// not preserve order. unordered_map is available since C++11. + +// 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 { @@ -828,6 +924,7 @@ std::map fooMap; fooMap[Foo(1)] = 1; fooMap.find(Foo(1)); //true + /////////////////////////////////////// // Lambda Expressions (C++11 and above) /////////////////////////////////////// @@ -994,101 +1091,6 @@ 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 manage the storage space for its elements and provide -// member functions to access and manipulate them. - -// Few containers are as follows: - -// Vector (Dynamic array) -// Allow us to Define the Array or list of objects at run time -#include -string val; -vector my_vector; // initialize the vector -cin >> val; -my_vector.push_back(val); // will push the value of 'val' into vector ("array") my_vector -my_vector.push_back(val); // will push the value into the vector again (now having two elements) - -// To iterate through a vector we have 2 choices: -// Either classic looping (iterating through the vector from index 0 to its last index): -for (int i = 0; i < my_vector.size(); i++) { - cout << my_vector[i] << endl; // for accessing a vector's element we can use the operator [] -} - -// or using an iterator: -vector::iterator it; // initialize the iterator for vector -for (it = my_vector.begin(); it != my_vector.end(); ++it) { - cout << *it << endl; -} - -// Set -// 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 -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 -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::iterator it; -for(it=ST.begin();it -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 -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 corresponding to a key -it = mymap.find('Z'); -cout << it->second; - -// Output: 26 - -// NOTE: For hash maps, use unordered_map. They are more efficient but do -// not preserve order. unordered_map is available since C++11. - /////////////////////////////////// // Logical and Bitwise operators ////////////////////////////////// -- cgit v1.2.3 From 3a1e38478ea87f922c53f04508c44e85f3c95a49 Mon Sep 17 00:00:00 2001 From: Max <46054571+Lugimax@users.noreply.github.com> Date: Sun, 10 Mar 2019 23:56:47 +0200 Subject: [en] Typo in c++ --- 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 3f5a2f4e..80ad3a6c 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -657,7 +657,7 @@ printMessage<10>(); // Prints "Learn C++ faster in only 10 minutes!" // The standard library provides a few exception types // (see http://en.cppreference.com/w/cpp/error/exception) -// but any type can be thrown an as exception +// but any type can be thrown as an exception #include #include -- cgit v1.2.3 From 696cbc66bed9971d3152cd9fd24e268caf3aec99 Mon Sep 17 00:00:00 2001 From: codesoap Date: Sun, 30 Jun 2019 12:40:47 +0200 Subject: c++: Add more explanation to the += overloading example --- c++.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 80ad3a6c..fc9f6ce2 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -553,10 +553,14 @@ Point Point::operator+(const Point& rhs) const return Point(x + rhs.x, y + rhs.y); } +// It's good practice to return a reference to the leftmost variable of +// an assignment. `(a += b) == c` will work this way. Point& Point::operator+=(const Point& rhs) { x += rhs.x; y += rhs.y; + + // `this` is a pointer to the object, on which a method is called. return *this; } -- cgit v1.2.3 From 41f2b7f168d0599e7567c6a96bec29ac87c68de4 Mon Sep 17 00:00:00 2001 From: LamdaLamdaLamda <25778959+LamdaLamdaLamda@users.noreply.github.com> Date: Sat, 3 Aug 2019 20:18:39 +0200 Subject: [C++/de] fixed wrong spelling (#3579) * Added [C++/de] * [C++/de] filename fixed. * [C++/de] language code in filename added * [C++/de] fixed wrong spelling * [C++/en] smart pointer added --- c++.html.markdown | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 4 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index fc9f6ce2..176ea1a8 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -72,7 +72,7 @@ void func(); // function which may accept any number of arguments int* ip = nullptr; // C standard headers are available in C++. -// C headers end in .h, while +// C headers end in .h, while // C++ headers are prefixed with "c" and have no ".h" suffix. // The C++ standard version: @@ -761,7 +761,7 @@ failure: // things are a little cleaner, but still sub-optimal. void doSomethingWithAFile(const char* filename) { - FILE* fh = fopen(filename, "r"); // Open the file in read mode + FILE* fh = fopen(filename, "r"); // Open the file in shared_ptrread mode if (fh == nullptr) throw std::runtime_error("Could not open the file."); @@ -814,6 +814,57 @@ void doSomethingWithAFile(const std::string& filename) // - Mutexes using lock_guard and unique_lock +///////////////////// +// Smart Pointer +///////////////////// + +// Generally a smart pointer is a class, which wraps a "raw pointer" (usage of "new" +// respectively malloc/calloc in C). The goal is to be able to +// manage the lifetime of the object being point to without explicitly deleting +// the object. The term itself simply describes a set of pointers with the +// mentioned abstraction. +// Basically smart pointers should preferred over raw pointers, to prevent +// risky memory leaks, which happens if you forget to delete the object. + +// Usage of a raw pointer: +Dog* ptr = new Dog(); +ptr->bark(); +delete ptr; + +// With the usage of smart pointers you dont have to worry about the deletion +// of a object anymore. +// A smart pointer describes a policy, to count the references on the +// pointer. As matter of fact the objects gets destroyed when the last +// reference on the object gets destroyed. + +// Usage of "std::shared_ptr": +void foo() +{ +// Its not longer necessary to delete the Dog. +std::shared_ptr doggo(new Dog()); +doggo->bark(); +} + +// Beware of possible circular references!!! +// There will be always a reference, so it will be never destroyed! +std::shared_ptr doggo_one (new Dog()); +std::shared_ptr doggo_two (new Dog()); +doggo_one = doggo_two; // p1 references p2 +doggo_two = doggo_one; // p2 references p1 + +// As mentioned before there is a set of smart pointers. The way you have to +// use it, is always the same. +// This leads us to question, when to use which one? +// std::unique_ptr - use it when you just want to hold one reference on +// the same object. +// std::shared_ptr - use it when you want to hold multiple references on the +// same object and want to make sure that it´s de-allocated +// when all refences are gone. +// std::weak_ptr - use it when you want to hold multiple references from +// different places for references for which it´s no problem +// tp de-allocate. + + ///////////////////// // Containers ///////////////////// @@ -877,7 +928,7 @@ cout << ST.size(); // will print the size of set ST // Output: 0 // NOTE: for duplicate elements we can use multiset -// NOTE: For hash sets, use unordered_set. They are more efficient but +// NOTE: For hash sets, use unordered_set. They are more efficient but // do not preserve order. unordered_set is available since C++11 // Map @@ -906,7 +957,7 @@ cout << it->second; // Output: 26 -// NOTE: For hash maps, use unordered_map. They are more efficient but do +// NOTE: For hash maps, use unordered_map. They are more efficient but do // not preserve order. unordered_map is available since C++11. // Containers with object keys of non-primitive values (custom classes) require -- cgit v1.2.3 From 43fc3289dcf623779a30f1002a58748fe3998812 Mon Sep 17 00:00:00 2001 From: Ollin Boer Bohan Date: Sat, 31 Aug 2019 08:11:27 -0700 Subject: Minor fixes to C++ Smart Pointer section * Fix minor spelling / grammar problems ("tp de-allocate", "refences", "dont"...) * Remove filler words ("Basically", "As a matter of fact"...) * Remove spaces before parens in smart pointer initialization code sample (consistent with the other sections, e.g. the `Tuples` section) * Clarify `std::weak_ptr` use case --- c++.html.markdown | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 176ea1a8..f3dc8e20 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -818,51 +818,51 @@ void doSomethingWithAFile(const std::string& filename) // Smart Pointer ///////////////////// -// Generally a smart pointer is a class, which wraps a "raw pointer" (usage of "new" +// Generally a smart pointer is a class which wraps a "raw pointer" (usage of "new" // respectively malloc/calloc in C). The goal is to be able to -// manage the lifetime of the object being point to without explicitly deleting +// manage the lifetime of the object being pointed to without ever needing to explicitly delete // the object. The term itself simply describes a set of pointers with the // mentioned abstraction. -// Basically smart pointers should preferred over raw pointers, to prevent -// risky memory leaks, which happens if you forget to delete the object. +// Smart pointers should preferred over raw pointers, to prevent +// risky memory leaks, which happen if you forget to delete an object. // Usage of a raw pointer: Dog* ptr = new Dog(); ptr->bark(); delete ptr; -// With the usage of smart pointers you dont have to worry about the deletion -// of a object anymore. -// A smart pointer describes a policy, to count the references on the -// pointer. As matter of fact the objects gets destroyed when the last -// reference on the object gets destroyed. +// By using a smart pointer, you don't have to worry about the deletion +// of the object anymore. +// A smart pointer describes a policy, to count the references to the +// pointer. The object gets destroyed when the last +// reference to the object gets destroyed. // Usage of "std::shared_ptr": void foo() { -// Its not longer necessary to delete the Dog. +// It's no longer necessary to delete the Dog. std::shared_ptr doggo(new Dog()); doggo->bark(); } // Beware of possible circular references!!! // There will be always a reference, so it will be never destroyed! -std::shared_ptr doggo_one (new Dog()); -std::shared_ptr doggo_two (new Dog()); +std::shared_ptr doggo_one(new Dog()); +std::shared_ptr doggo_two(new Dog()); doggo_one = doggo_two; // p1 references p2 doggo_two = doggo_one; // p2 references p1 -// As mentioned before there is a set of smart pointers. The way you have to -// use it, is always the same. -// This leads us to question, when to use which one? -// std::unique_ptr - use it when you just want to hold one reference on -// the same object. -// std::shared_ptr - use it when you want to hold multiple references on the -// same object and want to make sure that it´s de-allocated -// when all refences are gone. -// std::weak_ptr - use it when you want to hold multiple references from -// different places for references for which it´s no problem -// tp de-allocate. +// There are several kinds of smart pointers. +// The way you have to use them is always the same. +// This leads us to the question: when should we use each kind of smart pointer? +// std::unique_ptr - use it when you just want to hold one reference to +// the object. +// std::shared_ptr - use it when you want to hold multiple references to the +// same object and want to make sure that it's deallocated +// when all references are gone. +// std::weak_ptr - use it when you want to access +// the underlying object of a std::shared_ptr without causing that object to stay allocated. +// Weak pointers are used to prevent circular referencing. ///////////////////// -- cgit v1.2.3