3-tajriba ishi. Iteratorlardan foydalanib list konteyneri ustida amallar bajaring;
Iteratorlardan foydalanish uchun ma’lum list konteyneriga most iteratorlar yaratish lozim.
list::iterator iterator_name
#include #include
using namespace std; int main(){
list lst; lst.push_back(12); lst.push_back(23); lst.push_front(44); list::iterator it;
for(it=lst.begin();it!=lst.end();it++) cout<<*it<return 0;}
Funksiya shabloniga misollar
#include
using namespace std;
template
void swapargs(X &a, X &b)
{
X temp; temp = a; a = b;
b = temp;
cout << "swapargs funksiya shabloni chaqirildi.\n";
}
// Bunda swapargs() funksiyasi faqatgina int tipi uchun ishlaydi.
void swapargs(int &a, int &b)
{
int temp; temp = a; a = b;
b = temp;
cout << " int tipi uchun maxsus swapargs funksiyasi.\n";}
int main(){
int i=10, j=20;
double x=10.1, y=23.3; char a='x', b='z';
cout << "Original i, j: " << i << ' ' << j << '\n'; cout << "Original x, y: " << x << ' ' << y << '\n'; cout << "Original a, b: " << a << ' ' << b << '\n';
swapargs(i, j); // calls explicitly overloaded swapargs()
swapargs(x, y); // calls generic swapargs()
swapargs(a, b); // calls generic swapargs()
cout << "Swapped i, j: " << i << ' ' << j << '\n';
cout << "Swapped x, y: " << x << ' ' << y << '\n'; cout << "Swapped a, b: " << a << ' ' << b << '\n'; return 0;
}
#include using namespace std; template class mypair {
T a, b; public:
mypair (T first, T second)
{a=first; b=second;} T getmax ();
};
template
T mypair::getmax ()
{
T retval;
retval = a>b? a : b; return retval;
}
int main () {
mypair myobject (100, 75); cout << myobject.getmax(); return 0;
}
Sinf shablonida ikki xil toifadan foydalanish
#include
using namespace std;
template
class myclass
{
Type1 i; Type2 j; public:
myclass(Type1 a, Type2 b) {
i = a; j = b; }
void show() {
cout << i << ' ' << j << '\n'; }
};
int main()
{
myclass ob1(10, 0.23);
myclass ob2('X', "Assalomu alaykum.");
ob1.show(); // show int, double
ob2.show(); // show char, char *
return 0;
}
Examples:
Input : stackname.push(5);
stackname.push(1);
stackname.top();
Output : 1
Input : stackname.push(5);
stackname.push(1);
stackname.push(2);
stackname.top();
Output : 2
#include
#include
using namespace std;
int main()
{
stack mystack;
mystack.push(5);
mystack.push(1);
mystack.push(2);
// Stack top
cout << mystack.top();
return 0;
}
Examples:
Input : mystack
mystack.empty();
Output : True
Input : mystack = 1, 2, 3
Output : False
#include
#include
using namespace std;
int main()
{
stack mystack;
mystack.push(1);
// Stack becomes 1
if (mystack.empty()) {
cout << "True";
}
else {
cout << "False";
}
return 0;
}
Examples:
Input : mystack
mystack.push(6);
Output : 6
Input : mystack
mystack.push(0);
mystack.push(1);
Output : 0, 1
#include
#include
using namespace std;
int main()
{
// Empty stack
stack mystack;
mystack.push(0);
mystack.push(1);
mystack.push(2);
// Printing content of stack
while (!mystack.empty()) {
cout << ' ' << mystack.top();
mystack.pop();
}
}
Quyida Listning ba'zi funktsiyalarining ishlashini ko'rsatadigan dastur:
#include
#include
#include
using namespace std;
//ro'yxatdagi elementlarni chop etish funktsiyasi
void showlist(list g)
{
list :: iterator it;
for(it = g.begin(); it != g.end(); ++it)
cout << '\t' << *it;
cout << '\n';
}
int main()
{
list gqlist1, gqlist2;
for (int i = 0; i < 10; ++i)
{
gqlist1.push_back(i * 2);
gqlist2.push_front(i * 3);
}
cout << "\nList 1 (gqlist1) is : ";
showlist(gqlist1);
cout << "\nList 2 (gqlist2) is : ";
showlist(gqlist2);
cout << "\ngqlist1.front() : " << gqlist1.front();
cout << "\ngqlist1.back() : " << gqlist1.back();
cout << "\ngqlist1.pop_front() : ";
gqlist1.pop_front();
showlist(gqlist1);
cout << "\ngqlist2.pop_back() : ";
gqlist2.pop_back();
showlist(gqlist2);
cout << "\ngqlist1.reverse() : ";
gqlist1.reverse();
showlist(gqlist1);
cout << "\ngqlist2.sort(): ";
gqlist2.sort();
showlist(gqlist2);
return 0;}
Do'stlaringiz bilan baham: |