इस पहले के आर्टिकल मे , vector , map और iterator को discuss किया था | ये सभी c++ language के मुख्य concept है लेकिन इन सभी को c++ language मई एक अलग ही category मे रखा गया है जिसे STL programming कहते है | इस आर्टिकल मे , C++ लैंग्वेज के STL के बेसिक को discuss करेगे | इससे पहले के आर्टिकल मई template के concept को अच्छी तरह से discuss किया था | C++ langugae मे , कुछ predefine template होते है जिसे STL (Standard Template Library) कहते है | STL को discuss करने से पहले template concept को revised करेगे |
C++ Template
Template से general programming की जाती है | इसका मतलब है की Template से बनाया गये function और class किसी particular type से independate होती है |
Template एक blueprint और formula होते है जिससे class और function को define किया जाता है | सभी container जैसे vector , map , stack आदि के लिए एक डेफिनिशन होती है | उदाहरन के लिए vector <int> or vector <string> |
इसके अलावा , template को function के साथ class के लिय use किया जा सकता है | ये दो प्रकार से होता है :-
Function Template
Function Template की डेफिनिशन का genral form निन्म है :-
template <class type> return-type function-name(parameter list) {
// body of function
}
यह पर
type एक placeholder है जो की function के द्वारा return की गयी value और parameter के type को डिफाइन करता है | उदाहरन के लिए
उदाहरन :
#include <iostream>
#include <string>
using namespace std;
template <typename any>
inline any const& add (any const& a, any const& b) {
return a+b;
}
void main () {
int i = 39;
int j = 20;
cout << “Addition : ” << add(i, j) << endl;
double f1 = 13.5;
double f2 = 20.7;
cout << “Addition : ” << add(f1, f2) << endl;
getch();
}
इस उदाहरन मई एक ही function add() को दो टाइप integer और float के लिए use किया जा सकता है |
Addition : 59
Addition : 34.20
Class Template
जिस प्रकार से function template को define किया जा सकता है उसी प्रकार class template को भी define किया जासकता है | इस्सका syntax होता है :-
template <class type> class class-name {
.
.
.
}
यह पर
type एक placeholder है जो की function के द्वारा return की गयी value और parameter के type को डिफाइन करता है |
C++ STL (Standard Template Library)
उपर दिए गये विवरण से template को अच्छी तरह से समज गये होगे अब C++ STL (Standard Template Library),C++ template classes का एक powerful set है जिसे genral programming के लिए use किया जा सकता है | इसमें function और class दोनों के लिए use किया जा सकता है | STL programming मुख्यत algorithms and data structures जैसे vectors, lists, queues, and stacks को implement करने के लिए लाभदायक होती है |
C++ Standard Template Library मे निन्म तीन मुख्य component होता है :-
1.Containers
Containers का use , किसी प्रकार के टाइप के दो या दो से अधिक objects के collection को manage करने के लिए किया जाता है | C++ STL (Standard Template Library) मे निन्म मुख्य containers होता है :- deque, list, vector, map आदि |
2.Algorithms
Algorithms task का समूह होता है जो की किसी containers पर apply होता है | इसमें नींम ऑपरेशन मुख्य होते है :- initialization, sorting, searching, and transforming |
initialization : इसमें containers के element को initial किया जाता है |
sorting : इसमें containers के elements को किसी विशेष arrangement मे व्यस्थित किया जाता है |
searching : इसमें containers के elements मे से किसी element को find out किया जाता है |
transforming : इसमें किसी containers के elements को दुसरे containers के elements मे transform करना |
3.Iterators
Iterators , किसी container के element पर operation के कर्म को डिफाइन करता है | इसमें containers or subsets of containers को डिफाइन किया जाता है |
इस course मे सभी C++ STL components क सर से discuss करेगे | क्योकि इन C++ STL components मे कई सारे pre-defined functions होते है जिसे complicated tasks को बड़े ही सरल तरीके के किया जा सकता है |
नीचे दिए गये उदाहरन मे , vector container (a C++ Standard Template) को demonstrate किया जाता है जो की c++ array की तरह ही होता है |
उदाहरन
#include <iostream>
#include <vector>
using namespace std;
int main() {
// create a vector to store int
vector<int> myvector;
int i;
// display the original size of myvector
cout << “vector size = ” << myvector.size() << endl;
// push 5 values into the vector
for(i = 0; i < 5; i++) {
myvector.push_back(i);
}
// display extended size of myvector
cout << “extended vector size = ” << myvector.size() << endl;
// access 5 values from the myvector
for(i = 0; i < 5; i++) {
cout << “value of myvector [” << i << “] = ” << myvector[i] << endl;
}
इस पहले के आर्टिकल मे , vector , map और iterator को discuss किया था | ये सभी c++ language के मुख्य concept है लेकिन इन सभी को c++ language मई एक अलग ही category मे रखा गया है जिसे STL programming कहते है | इस आर्टिकल मे , C++ लैंग्वेज के STL के बेसिक को discuss करेगे | इससे पहले के आर्टिकल मई template के concept को अच्छी तरह से discuss किया था | C++ langugae मे , कुछ predefine template होते है जिसे STL (Standard Template Library) कहते है | STL को discuss करने से पहले template concept को revised करेगे |
C++ Template
Template से general programming की जाती है | इसका मतलब है की Template से बनाया गये function और class किसी particular type से independate होती है |
Template एक blueprint और formula होते है जिससे class और function को define किया जाता है | सभी container जैसे vector , map , stack आदि के लिए एक डेफिनिशन होती है | उदाहरन के लिए vector <int> or vector <string> |
इसके अलावा , template को function के साथ class के लिय use किया जा सकता है | ये दो प्रकार से होता है :-
Function Template
Function Template की डेफिनिशन का genral form निन्म है :-
template <class type> return-type function-name(parameter list) {
// body of function
}
यह पर
type एक placeholder है जो की function के द्वारा return की गयी value और parameter के type को डिफाइन करता है | उदाहरन के लिए
उदाहरन :
#include <iostream>
#include <string>
using namespace std;
template <typename any>
inline any const& add (any const& a, any const& b) {
return a+b;
}
void main () {
int i = 39;
int j = 20;
cout << “Addition : ” << add(i, j) << endl;
double f1 = 13.5;
double f2 = 20.7;
cout << “Addition : ” << add(f1, f2) << endl;
getch();
}
इस उदाहरन मई एक ही function add() को दो टाइप integer और float के लिए use किया जा सकता है |
Addition : 59
Addition : 34.20
Class Template
जिस प्रकार से function template को define किया जा सकता है उसी प्रकार class template को भी define किया जासकता है | इस्सका syntax होता है :-
template <class type> class class-name {
.
.
.
}
यह पर
type एक placeholder है जो की function के द्वारा return की गयी value और parameter के type को डिफाइन करता है |
C++ STL (Standard Template Library)
उपर दिए गये विवरण से template को अच्छी तरह से समज गये होगे अब C++ STL (Standard Template Library),C++ template classes का एक powerful set है जिसे genral programming के लिए use किया जा सकता है | इसमें function और class दोनों के लिए use किया जा सकता है | STL programming मुख्यत algorithms and data structures जैसे vectors, lists, queues, and stacks को implement करने के लिए लाभदायक होती है |
C++ Standard Template Library मे निन्म तीन मुख्य component होता है :-
1.Containers
Containers का use , किसी प्रकार के टाइप के दो या दो से अधिक objects के collection को manage करने के लिए किया जाता है | C++ STL (Standard Template Library) मे निन्म मुख्य containers होता है :- deque, list, vector, map आदि |
2.Algorithms
Algorithms task का समूह होता है जो की किसी containers पर apply होता है | इसमें नींम ऑपरेशन मुख्य होते है :- initialization, sorting, searching, and transforming |
initialization : इसमें containers के element को initial किया जाता है |
sorting : इसमें containers के elements को किसी विशेष arrangement मे व्यस्थित किया जाता है |
searching : इसमें containers के elements मे से किसी element को find out किया जाता है |
transforming : इसमें किसी containers के elements को दुसरे containers के elements मे transform करना |
3.Iterators
Iterators , किसी container के element पर operation के कर्म को डिफाइन करता है | इसमें containers or subsets of containers को डिफाइन किया जाता है |
इस course मे सभी C++ STL components क सर से discuss करेगे | क्योकि इन C++ STL components मे कई सारे pre-defined functions होते है जिसे complicated tasks को बड़े ही सरल तरीके के किया जा सकता है |
नीचे दिए गये उदाहरन मे , vector container (a C++ Standard Template) को demonstrate किया जाता है जो की c++ array की तरह ही होता है |
उदाहरन
#include <iostream>
#include <vector>
using namespace std;
int main() {
// create a vector to store int
vector<int> myvector;
int i;
// display the original size of myvector
cout << “vector size = ” << myvector.size() << endl;
// push 5 values into the vector
for(i = 0; i < 5; i++) {
myvector.push_back(i);
}
// display extended size of myvector
cout << “extended vector size = ” << myvector.size() << endl;
// access 5 values from the myvector
for(i = 0; i < 5; i++) {
cout << “value of myvector [” << i << “] = ” << myvector[i] << endl;
}
// use iterator to access the values
vector<int>::iterator i = myvector.begin();
while( i != myvector.end()) {
cout << “value of myvector = ” << *i << endl;
v++;
}
getch();
}
When the above code is compiled and executed, it produces the following result −
vector size = 0
extended vector size = 5
value of myvector [0] = 0
value of myvector [1] = 1
value of myvector [2] = 2
value of myvector [3] = 3
value of myvector [4] = 4
value of myvector = 0
value of myvector = 1
value of myvector = 2
value of myvector = 3
value of myvector = 4
इस उदाहरन मे vector class के निन्म functions को discuss किया गया है :-
The push_back( ) member function का use , vector मई value को assign करने के लिए कजिया जाता है |
The size( ) function का उसे किसी vector की size को find out करने के लिए किया जाता है |
The function begin( ) से किसी vector के first element को return किया जाता है |
The function end( ) से किसी vector के last element को return किया जाता है |
// use iterator to access the values
vector<int>::iterator i = myvector.begin();
while( i != myvector.end()) {
cout << “value of myvector = ” << *i << endl;
v++;
}
getch();
}
When the above code is compiled and executed, it produces the following result −
vector size = 0
extended vector size = 5
value of myvector [0] = 0
value of myvector [1] = 1
value of myvector [2] = 2
value of myvector [3] = 3
value of myvector [4] = 4
value of myvector = 0
value of myvector = 1
value of myvector = 2
value of myvector = 3
value of myvector = 4
इस उदाहरन मे vector class के निन्म functions को discuss किया गया है :-
The push_back( ) member function का use , vector मई value को assign करने के लिए कजिया जाता है |
The size( ) function का उसे किसी vector की size को find out करने के लिए किया जाता है |
The function begin( ) से किसी vector के first element को return किया जाता है |
The function end( ) से किसी vector के last element को return किया जाता है |