首頁 文章 C++STL练习

C++STL练习

2024-08-08 10:12  瀏覽數:279  來源: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;
}



聲明:以上文章均為用戶自行添加,僅供打字交流使用,不代表本站觀點,本站不承擔任何法律責任,特此聲明!如果有侵犯到您的權利,請及時聯系我們刪除。

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