mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 23:33:00 +03:00
Some minor (mostly stylistic) fixes in C++
This commit is contained in:
parent
50c8f7b4dd
commit
47679dfcbe
@ -958,10 +958,10 @@ const int maxN = 1e9;
|
|||||||
const int maxL = 15;
|
const int maxL = 15;
|
||||||
auto second = make_tuple(maxN, maxL);
|
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
|
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
|
cout << get<0>(second) << " " << get<1>(second) << "\n"; // prints: 1000000000 15
|
||||||
|
|
||||||
// Unpacking tuple into variables
|
// Unpacking tuple into variables
|
||||||
@ -989,42 +989,42 @@ cout << get<5>(concatenated_tuple) << "\n"; // prints: 'A'
|
|||||||
|
|
||||||
|
|
||||||
/////////////////////
|
/////////////////////
|
||||||
// CONTAINERS
|
// Containers
|
||||||
/////////////////////
|
/////////////////////
|
||||||
|
|
||||||
// Containers or the Standard Template Library are some predefined templates
|
// Containers or the Standard Template Library are some predefined templates.
|
||||||
// They manages the storage space for its elements and provide
|
// They manage the storage space for its elements and provide
|
||||||
// member functions to access and manipulate them
|
// 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
|
// Allow us to Define the Array or list of objects at run time
|
||||||
#include<vector> // will include the header file for vector
|
#include<vector>
|
||||||
vector<Data_Type> Vector_name; // used to initialize the vector
|
vector<Data_Type> Vector_name; // used to initialize the vector
|
||||||
cin >> val;
|
cin >> val;
|
||||||
Vector_name.push_back(val); // will push the value of variable into array
|
Vector_name.push_back(val); // will push the value of variable into array
|
||||||
|
|
||||||
// To iterate through vector, we have 2 choices
|
// To iterate through vector, we have 2 choices:
|
||||||
// using normal looping
|
// Normal looping
|
||||||
for(int i=0; i<Vector_name.size(); i++)
|
for(int i=0; i<Vector_name.size(); i++)
|
||||||
// It will iterate through the vector from index '0' till last index
|
// It will iterate through the vector from index '0' till last index
|
||||||
|
|
||||||
// Using Iterator
|
// Iterator
|
||||||
vector<Data_Type>::iterator it; // initialize the iteartor for vector
|
vector<Data_Type>::iterator it; // initialize the iteartor for vector
|
||||||
for(it=vector_name.begin(); it!=vector_name.end();++it)
|
for(it=vector_name.begin(); it!=vector_name.end();++it)
|
||||||
|
|
||||||
// For accessing the element of the vector
|
// For accessing the element of the vector
|
||||||
// Operator []
|
// 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
|
// Set
|
||||||
// Sets are containers that store unique elements following a specific order
|
// Sets are containers that store unique elements following a specific order.
|
||||||
// Very useful container to store unique values in sorted order
|
// Set is a very useful container to store unique values in sorted order
|
||||||
// without any other functions or code
|
// without any other functions or code.
|
||||||
|
|
||||||
#include<set> // Will include the header file for sets
|
#include<set>
|
||||||
set<int> ST; // Will initialize the set of int data type
|
set<int> ST; // Will initialize the set of int data type
|
||||||
ST.insert(30); // Will insert the value 30 in set ST
|
ST.insert(30); // Will insert the value 30 in set ST
|
||||||
ST.insert(10); // Will insert the value 10 in set ST
|
ST.insert(10); // Will insert the value 10 in set ST
|
||||||
@ -1038,26 +1038,25 @@ ST.erase(20); // Will erase element with value 20
|
|||||||
// Set ST: 10 30
|
// Set ST: 10 30
|
||||||
// To iterate through Set we use iterators
|
// To iterate through Set we use iterators
|
||||||
set<int>::iterator it;
|
set<int>::iterator it;
|
||||||
for(it=ST.begin();it<ST.end();it++)
|
for(it=ST.begin();it<ST.end();it++) {
|
||||||
{
|
|
||||||
cout << *it << endl;
|
cout << *it << endl;
|
||||||
}
|
}
|
||||||
// OUTPUT:
|
// Output:
|
||||||
// 10
|
// 10
|
||||||
// 30
|
// 30
|
||||||
|
|
||||||
// To clear the complete container we use Container_name.clear()
|
// To clear the complete container we use Container_name.clear()
|
||||||
ST.clear();
|
ST.clear();
|
||||||
cout << ST.size(); // will print the size of set ST
|
cout << ST.size(); // will print the size of set ST
|
||||||
// OUTPUT: 0
|
// Output: 0
|
||||||
|
|
||||||
// NOTE: for duplicate elements we can use multiset
|
// NOTE: for duplicate elements we can use multiset
|
||||||
|
|
||||||
// MAP
|
// Map
|
||||||
// Maps store elements formed by a combination of a key value
|
// Maps store elements formed by a combination of a key value
|
||||||
// and a mapped value, following a specific order
|
// and a mapped value, following a specific order.
|
||||||
|
|
||||||
#include<map> // Will include the header file for map
|
#include<map>
|
||||||
map<char, int> mymap; // Will initalize the map with key as char and value as int
|
map<char, int> mymap; // Will initalize the map with key as char and value as int
|
||||||
|
|
||||||
mymap.insert(pair<char,int>('A',1));
|
mymap.insert(pair<char,int>('A',1));
|
||||||
@ -1077,7 +1076,8 @@ for (it=mymap.begin(); it!=mymap.end(); ++it)
|
|||||||
it = mymap.find('Z');
|
it = mymap.find('Z');
|
||||||
cout << it->second;
|
cout << it->second;
|
||||||
|
|
||||||
// OUTPUT: 26
|
// Output: 26
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////
|
///////////////////////////////////
|
||||||
// Logical and Bitwise operators
|
// Logical and Bitwise operators
|
||||||
@ -1092,12 +1092,12 @@ cout<<it->second;
|
|||||||
|
|
||||||
true && false // Performs **logical and** to yield false
|
true && false // Performs **logical and** to yield false
|
||||||
true || false // Performs **logical or** to yield true
|
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
|
// Instead of using symbols equivalent keywords can be used
|
||||||
true and false // Performs **logical and** to yield false
|
true and false // Performs **logical and** to yield false
|
||||||
true or false // Performs **logical or** to yield true
|
true or false // Performs **logical or** to yield true
|
||||||
not true // Performs **logcical not** to yield
|
not true // Performs **logical not** to yield false
|
||||||
|
|
||||||
// Bitwise operators
|
// Bitwise operators
|
||||||
|
|
||||||
@ -1108,9 +1108,9 @@ not true // Performs **logcical not** to yield
|
|||||||
|
|
||||||
|
|
||||||
// **>>** Right Shift Operator
|
// **>>** 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
|
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 or
|
||||||
|
Loading…
Reference in New Issue
Block a user