64,652
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<memory>
#include<vector>
using namespace std;
int main()
{
allocator<vector<int>> aa;
auto b = aa.allocate(10);//括号内的数字不管是多少下面输出的都是10
aa.construct(b,10,1);
cout << (*b).size() << endl;
return 0;
}
allocator<vector<int>> aa;
auto b = aa.allocate(10); //返回的是分配内存的首地址
aa.construct(b,10,1); //在该地址处进行构造,即对Vector<int>进行构造,传入的是10和1,表明Vector大小为10,每一个int为1.
allocator<T>
allocator是个容器,allocate返回的是分配的首地址,construct是对传入的指针(T *类型)进行构造。后面的是构造函数。#include<iostream>
#include<memory>
#include<vector>
using namespace std;
int main()
{
allocator<vector<int>> aa;
auto b = aa.allocate(123);
aa.construct(b, 10, 1);
cout << (*b).capacity() << endl;//输出10
for (auto g : *b)
cout << g << endl;
return 0;
}
输出的不应该是123吗?结果还是10[/quote]
那是我错了,allocate相当于c语言中的malloc只管分配内存,其他不影响#include<iostream>
#include<memory>
#include<vector>
using namespace std;
int main()
{
allocator<vector<int>> aa;
auto b = aa.allocate(123);
aa.construct(b, 10, 1);
cout << (*b).capacity() << endl;//输出10
for (auto g : *b)
cout << g << endl;
return 0;
}
输出的不应该是123吗?结果还是10