Loading... # 实验八 STL标准模板库 ```C++ #include <iostream> #include <vector> #include <algorithm> using namespace std; template<typename T> void outputUsingVectorFunctions(const vector<T>& data) { for (size_t i = 0; i < data.size(); i++) { cout << data[i] << " "; } cout << endl; } template<typename T> void outputUsingIterator(const vector<T>& data) { typename vector<T>::const_iterator it; for (it = data.begin(); it != data.end(); it++) { cout << *it << " "; } cout << endl; } template<typename T> void outputUsingForEach(const vector<T>& data) { for_each(data.begin(), data.end(), [](const T& value) { cout << value << " "; }); cout << endl; } int main() { vector<int> numbers; int input; cout << "请输入整数(输入0结束):" << endl; while (true) { cin >> input; if (input == 0) { break; } numbers.push_back(input); } cout << "使用vector的成员函数输出:" << endl; outputUsingVectorFunctions(numbers); cout << "使用迭代器输出:" << endl; outputUsingIterator(numbers); cout << "使用for_each输出:" << endl; outputUsingForEach(numbers); return 0; } ``` 最后修改:2023 年 08 月 05 日 © 允许规范转载 赞 1 如果觉得我的文章对你有用,请随意赞赏