diff options
author | Ankush goyal <ankushg07@users.noreply.github.com> | 2016-06-27 02:19:51 +0530 |
---|---|---|
committer | ven <vendethiel@hotmail.fr> | 2016-06-26 22:49:51 +0200 |
commit | 281ba5b37a9e4e42d33442b0d3a062d99c964a95 (patch) | |
tree | 7a83caf4bb96006daf0fd82d5c65c475584625dc /c++.html.markdown | |
parent | 5977df5be45c0f3468abf402921cf925c996a472 (diff) |
Containers Added (#1942)
* Containers Added
* Text Formatting
Added required spaces between text and //
Diffstat (limited to 'c++.html.markdown')
-rw-r--r-- | c++.html.markdown | 94 |
1 files changed, 94 insertions, 0 deletions
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<vector> // 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<Vector_name.size(); i++) +// It will iterate through the vector from index '0' till last index + +// Using Iterator +vector<Data_Type>::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<set> // 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<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> // 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<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 correponsing to a key +it = mymap.find('Z'); +cout<<it->second; + +// OUTPUT: 26 + + ``` Further Reading: |