Home/ Article Practice/ C++STL练习

C++STL练习

2024-08-08 10:12  views:365  source:srxh1314    

#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> myVector;
myVector.push_back(3);
myVector.push_back(7);
myVector.push_back(11);
myVector.push_back(5);
cout << "Elements in the vector: ";
for (int element : myVector) {
cout << element << " ";
}
cout << endl;
cout << "First element: " << myVector[0] << endl;
cout << "Second element: " << myVector.at(1) << endl;
cout << "Size of the vector: " << myVector.size() << endl;
myVector.erase(myVector.begin() + 2);
cout << "Elements in the vector after erasing: ";
for (int element : myVector) {
cout << element << " ";
}
cout << endl;
myVector.clear();
cout << "Size of the vector after clearing: " << myVector.size() << endl;
return 0;
}
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> numbers;
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);
cout << "First element: " << numbers.front() << endl;
cout << "Last element: " << numbers.back() << endl;
cout << "List elements: ";
for (list<int>::iterator it = numbers.begin(); it != numbers.end(); ++it) {
cout << *it << " ";
}
cout << endl;
numbers.pop_back();
cout << "List elements after removing the last element: ";
for (list<int>::iterator it = numbers.begin(); it != numbers.end(); ++it) {
cout << *it << " ";
}
cout << endl;
return 0;
}
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int> q;
q.push(10);
q.push(20);
q.push(30);
cout << "Size of queue is: " << q.size() << endl;
cout << "First element: " << q.front() << endl;
cout << "Last element: " << q.back() << endl;
q.pop();
cout << "First element after removing: " << q.front() << endl;
cout << "Size of queue is: " << q.size() << endl;
cout << "iterat the q without clearing" << endl;
queue<int> w;
while(!w.empty()) {
cout<<w.front()<<" ";
w.pop();
}
cout << w.size() << " " << q.size();
return 0;
}



Disclaimer: The above articles are added by users themselves and are only for typing and communication purposes. They do not represent the views of this website, and this website does not assume any legal responsibility. This statement is hereby made! If there is any infringement of your rights, please contact us promptly to delete it.

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)