From 281ba5b37a9e4e42d33442b0d3a062d99c964a95 Mon Sep 17 00:00:00 2001 From: Ankush goyal Date: Mon, 27 Jun 2016 02:19:51 +0530 Subject: Containers Added (#1942) * Containers Added * Text Formatting Added required spaces between text and // --- c++.html.markdown | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) (limited to 'c++.html.markdown') 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 // 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::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 // 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 // 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('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 correponsing to a key +it = mymap.find('Z'); +cout<second; + +// OUTPUT: 26 + + ``` Further Reading: -- cgit v1.2.3