我想在调用 Heap 构造函数时创建对象 A (aArray) 的向量并操作 aArray.a 的值。目前,aArray.a 的值保持不变。我怎样才能正确地做到这一点?
谢谢您的帮助!
#include <vector>
#include <iostream>
struct A
{
A(int av):a(av){}
int a;
};
struct Heap
{
Heap(std::vector<A> *hVal)
{
for (int i=0; i<hVal->size(); ++i)
{
hVec.push_back(hVal->at(i));
}
std::cout << std::endl;
for(int i=0; i<hVec.size(); ++i)
{
hVec[i].a*=2;
std::cout << "hVec[i].a*=2 is " << hVec[i].a << "\n";
}
}
std::vector<A> hVec;
};
int main()
{
A a0(2), a1(4), a2(8);
std::vector<A> aArray;
aArray.push_back(a0);
aArray.push_back(a1);
aArray.push_back(a2);
Heap h(&aArray);
for(int i=0; i<aArray.size(); ++i)
{
std::cout << "aArray[i].a = " << aArray[i].a << "\n";
}
return 0;
}