C ++程序在STL中实现排序容器
在此C ++程序中,我们在STL中实现了Sorting容器。
功能和说明:
Functions used here:l.push_back() = It is used to push elements into a list from the front.
l.sort() = Sorts the elements of the list.
Where l is a list object.
范例程式码
#include <iostream>#include <list>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
list<int> l;
list<int>::iterator it;
int c, i;
while (1) {
cout<<"1.Insert Element to the list"<<endl;
cout<<"2.Display the List"<<endl;
cout<<"3.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>c;
switch(c) {
case 1:
cout<<"Enter value to be inserted";
cin>>i;
l.push_back(i);
break;
case 2:
l.sort();
cout<<"Elements of the List: ";
for (it = l.begin(); it != l.end(); it++)
cout<<*it<<" ";
cout<<endl;
break;
case 3:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
输出结果
1.Insert Element to the list2.Display the List
3.Exit
Enter your Choice: 1
Enter value to be inserted7
1.Insert Element to the list
2.Display the List
3.Exit
Enter your Choice: 1
Enter value to be inserted10
1.Insert Element to the list
2.Display the List
3.Exit
Enter your Choice: 1
Enter value to be inserted6
1.Insert Element to the list
2.Display the List
3.Exit
Enter your Choice: 1
Enter value to be inserted4
1.Insert Element to the list
2.Display the List
3.Exit
Enter your Choice: 2
Elements of the List: 4 6 7 10
1.Insert Element to the list
2.Display the List
3.Exit
Enter your Choice: 3
Exit code: 1
以上是 C ++程序在STL中实现排序容器 的全部内容, 来源链接: utcz.com/z/322252.html