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 From d69afc403942ce7417d35bdd4e573da641ca7016 Mon Sep 17 00:00:00 2001 From: Pratik Karki Date: Fri, 25 Aug 2017 14:24:43 +0545 Subject: fix language code suffix(#2832) --- 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 1461c93e..a0f06c84 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -8,7 +8,7 @@ contributors: - ["Connor Waters", "http://github.com/connorwaters"] - ["Ankush Goyal", "http://github.com/ankushg07"] - ["Jatin Dhankhar", "https://github.com/jatindhankhar"] -lang: en + --- C++ is a systems programming language that, -- cgit v1.2.3 From c076537cf9a90d0be4848885ef09895b5aa12b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=20Polykanine=20A=2EK=2EA=2E=20Menelion=20Elens=C3=BA?= =?UTF-8?q?l=C3=AB?= Date: Wed, 30 Aug 2017 02:46:49 +0300 Subject: [c++/en] Added public declaration. Fixed #2805 --- 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 a0f06c84..23013410 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -8,7 +8,6 @@ contributors: - ["Connor Waters", "http://github.com/connorwaters"] - ["Ankush Goyal", "http://github.com/ankushg07"] - ["Jatin Dhankhar", "https://github.com/jatindhankhar"] - --- C++ is a systems programming language that, @@ -474,6 +473,7 @@ int main() { // without a public or protected method for doing so class OwnedDog : public Dog { +public: void setOwner(const std::string& dogsOwner); // Override the behavior of the print function for all OwnedDogs. See -- cgit v1.2.3 From cd379d9e9eff734ae02708ba986b0fde1fa52d33 Mon Sep 17 00:00:00 2001 From: Mario Date: Tue, 12 Sep 2017 10:21:23 +0200 Subject: [c++/en] container -> vector (#2838) * container -> vector fixed errors: - "vector_name" and "Vector_name" (different case) would have resulted in a compile time error, now: "my_vector" enhancements: - typedef for consistency - two push_backs to show its purpose - both iteration types now have a working execution block (both output the vector's content) - the first "classic loop" now also shows the operator [], which therefor is removed from below - include and for with a white spaces for readability * removed the typedef the `typedef` was used to show that we will be using `string` as our base for all operations, but we are free to use any other type; of course it is technically not needed and might look like a redundancy. the two `cin` also look redundant, so I changed this into one `cin` and two `push_back`s --- c++.html.markdown | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 23013410..8d1c7a26 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -1000,24 +1000,24 @@ cout << get<5>(concatenated_tuple) << "\n"; // prints: 'A' // Vector (Dynamic array) // Allow us to Define the Array or list of objects at run time -#include -vector Vector_name; // used to initialize the vector +#include +string val; +vector my_vector; // initialize the vector cin >> val; -Vector_name.push_back(val); // will push the value of variable into array +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 vector, we have 2 choices: -// Normal looping -for(int i=0; i::iterator it; // initialize the iterator 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 +// 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. -- cgit v1.2.3 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 From 5f6375db90cf0dfdbe457fce4239023ed057579e Mon Sep 17 00:00:00 2001 From: Ryan Huang Date: Sat, 23 May 2020 06:42:00 +0000 Subject: Remove extra space --- 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 f3dc8e20..59aad210 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -202,7 +202,7 @@ int main() cout << "Your favorite number is " << myInt << "\n"; // prints "Your favorite number is " - cerr << "Used for error messages"; + cerr << "Used for error messages"; } ////////// -- cgit v1.2.3 From 79ebb067b36aa96ae6d6daee3f070bf0c895ea73 Mon Sep 17 00:00:00 2001 From: Spencer Burris Date: Sat, 12 Sep 2020 21:06:26 -0700 Subject: Replace newline string with newline character --- c++.html.markdown | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 59aad210..626da194 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -199,7 +199,7 @@ int main() cin >> myInt; // cout can also be formatted - cout << "Your favorite number is " << myInt << "\n"; + cout << "Your favorite number is " << myInt << '\n'; // prints "Your favorite number is " cerr << "Used for error messages"; @@ -461,7 +461,7 @@ void Dog::print() const Dog::~Dog() { - std::cout << "Goodbye " << name << "\n"; + std::cout << "Goodbye " << name << '\n'; } int main() { @@ -504,7 +504,7 @@ void OwnedDog::setOwner(const std::string& dogsOwner) void OwnedDog::print() const { Dog::print(); // Call the print function in the base Dog class - std::cout << "Dog is owned by " << owner << "\n"; + std::cout << "Dog is owned by " << owner << '\n'; // Prints "Dog is and weights " // "Dog is owned by " } @@ -946,7 +946,7 @@ mymap.insert(pair('Z',26)); // 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 << std::cout; // Output: // A->1 // Z->26 @@ -1117,33 +1117,33 @@ 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 +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; -cout << first_int << " " << first_char << "\n"; // prints : 10 A +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::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) -cout << get<0>(concatenated_tuple) << "\n"; // prints: 10 -cout << get<3>(concatenated_tuple) << "\n"; // prints: 15 -cout << get<5>(concatenated_tuple) << "\n"; // prints: 'A' +cout << get<0>(concatenated_tuple) << '\n'; // prints: 10 +cout << get<3>(concatenated_tuple) << '\n'; // prints: 15 +cout << get<5>(concatenated_tuple) << '\n'; // prints: 'A' /////////////////////////////////// -- cgit v1.2.3 From e07193e6b463a70a70d58bcaf2936362d9d96e75 Mon Sep 17 00:00:00 2001 From: Priti Xavier Date: Tue, 29 Sep 2020 20:50:47 +0800 Subject: Changed operator < to != (#4004) --- 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 626da194..948b52ec 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -915,7 +915,7 @@ 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 Date: Sun, 31 Jan 2021 21:55:38 +0100 Subject: Update c++.html.markdown Just forcing http:// links to be https:// when possible. I also read everything carefully and didn't find any mistake or easy improvements I could add (but I'm not that familiar with C++) --- c++.html.markdown | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'c++.html.markdown') diff --git a/c++.html.markdown b/c++.html.markdown index 948b52ec..6e94e03e 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -2,16 +2,16 @@ language: c++ filename: learncpp.cpp contributors: - - ["Steven Basart", "http://github.com/xksteven"] + - ["Steven Basart", "https://github.com/xksteven"] - ["Matt Kline", "https://github.com/mrkline"] - ["Geoff Liu", "http://geoffliu.me"] - - ["Connor Waters", "http://github.com/connorwaters"] - - ["Ankush Goyal", "http://github.com/ankushg07"] + - ["Connor Waters", "https://github.com/connorwaters"] + - ["Ankush Goyal", "https://github.com/ankushg07"] - ["Jatin Dhankhar", "https://github.com/jatindhankhar"] --- C++ is a systems programming language that, -[according to its inventor Bjarne Stroustrup](http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2014/Keynote), +[according to its inventor Bjarne Stroustrup](https://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2014/Keynote), was designed to - be a "better C" @@ -37,7 +37,7 @@ one of the most widely-used programming languages. // Just like in C, your program's entry point is a function called // main with an integer return type. // This value serves as the program's exit status. -// See http://en.wikipedia.org/wiki/Exit_status for more information. +// See https://en.wikipedia.org/wiki/Exit_status for more information. int main(int argc, char** argv) { // Command line arguments are passed in by argc and argv in the same way @@ -483,7 +483,7 @@ public: void setOwner(const std::string& dogsOwner); // Override the behavior of the print function for all OwnedDogs. See - // http://en.wikipedia.org/wiki/Polymorphism_(computer_science)#Subtyping + // https://en.wikipedia.org/wiki/Polymorphism_(computer_science)#Subtyping // for a more general introduction if you are unfamiliar with // subtype polymorphism. // The override keyword is optional but makes sure you are actually @@ -616,7 +616,7 @@ boxOfBox.insert(intBox); // template // instead. The 'class' keyword and 'typename' keywords are _mostly_ // interchangeable in this case. For the full explanation, see -// http://en.wikipedia.org/wiki/Typename +// https://en.wikipedia.org/wiki/Typename // (yes, that keyword has its own Wikipedia page). // Similarly, a template function: @@ -660,7 +660,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) +// (see https://en.cppreference.com/w/cpp/error/exception) // but any type can be thrown as an exception #include #include @@ -1030,7 +1030,7 @@ 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. -// More on Lambdas in C++ : http://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11 +// More on Lambdas in C++ : https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11 /////////////////////////////// // Range For (C++11 and above) @@ -1106,7 +1106,8 @@ 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. -- cgit v1.2.3