#include <iostream>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
//打印数组
class bianli
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void printVector(int val)
{
cout << val <<" ";
}
int returnVal(int val)
{
return val*val;
}
void test01()
{
vector<int> v1; //此时,v1的大小和容量都为0.
for (int i = 0; i < 10; i++)
{
v1.push_back(i);//向v1中插入数据.
}
//目标容器需要提前开辟空间 否则无法用transform和for_each
vector<int> v2(v1.size());
//将v1中的数据全部搬到v2中;注意:v2不需要给出结束位置.
transform(v1.begin(), v1.end(), v2.begin(),returnVal);
for (vector<int>::iterator a = v1.begin(); a != v1.end(); a++)
{
cout << *a << " ";
}
cout<<endl;
for_each(v2.begin(), v2.end(), printVector);
}
int main() {
test01();
}
08-15
1万+