summaryrefslogtreecommitdiffhomepage
path: root/c++.html.markdown
diff options
context:
space:
mode:
authorhyphz <drmoose94@gmail.com>2017-07-18 17:56:42 +0100
committerhyphz <drmoose94@gmail.com>2017-07-18 17:56:42 +0100
commit5ab5cb9800822d607be2c6ac943377811db98158 (patch)
tree3c804707822744c20da1de54ff60fc8c3197781b /c++.html.markdown
parent62102d02992f83b3a1fb745a39f36332dd4435b7 (diff)
parent6e7c5c793327f4a63b13e555894597915ca91fda (diff)
Merge remote-tracking branch 'adambard/master'
Diffstat (limited to 'c++.html.markdown')
-rw-r--r--c++.html.markdown183
1 files changed, 153 insertions, 30 deletions
diff --git a/c++.html.markdown b/c++.html.markdown
index a59b4db8..1461c93e 100644
--- a/c++.html.markdown
+++ b/c++.html.markdown
@@ -6,6 +6,8 @@ contributors:
- ["Matt Kline", "https://github.com/mrkline"]
- ["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
---
@@ -215,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"
@@ -330,7 +332,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 +354,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
{
@@ -454,7 +456,7 @@ void Dog::print() const
Dog::~Dog()
{
- cout << "Goodbye " << name << "\n";
+ std::cout << "Goodbye " << name << "\n";
}
int main() {
@@ -468,7 +470,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 {
@@ -806,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 {
@@ -825,10 +827,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<pair<int, int> > tester;
@@ -856,7 +858,7 @@ sort(tester.begin(), tester.end(), [](const pair<int, int>& lhs, const pair<int,
vector<int> 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 +942,13 @@ Foo f1;
f1 = f2;
-// How to truly clear a container:
-class Foo { ... };
-vector<Foo> 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<Foo>());
-
-
///////////////////////////////////////
// Tuples (C++11 and above)
///////////////////////////////////////
#include<tuple>
-// 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.
@@ -972,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
@@ -995,12 +981,149 @@ 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
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<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:
+// Normal looping
+for(int i=0; i<Vector_name.size(); i++)
+// It will iterate through the vector from index '0' till last index
+
+// Iterator
+vector<Data_Type>::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
+
+
+// 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>
+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<ST.end();it++) {
+ cout << *it << endl;
+}
+// Output:
+// 10
+// 30
+
+// To clear the complete container we use Container_name.clear()
+ST.clear();
+cout << ST.size(); // will print the size of set ST
+// Output: 0
+
+// NOTE: for duplicate elements we can use multiset
+
+// Map
+// Maps store elements formed by a combination of a key value
+// and a mapped value, following a specific order.
+
+#include<map>
+map<char, int> mymap; // Will initialize the map with key as char and value as int
+
+mymap.insert(pair<char,int>('A',1));
+// Will insert value 1 for key A
+mymap.insert(pair<char,int>('Z',26));
+// Will insert value 26 for key Z
+
+// To iterate
+map<char,int>::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
+
+
+///////////////////////////////////
+// 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 **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 **logical not** to yield false
+
+// 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: