实验八 STL标准模板库
#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;
}