为什么这样给Vector 赋值, size 不会增长
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class test
{
public:
vector<int*> i;
test(void){
i.clear();
i.reserve(3);
}
void foo(int slotId, int *temp){
(this->i)[slotId] = temp;
}
};
int main(void)
{
test *t1 = new test();
int a = 0;
int b = 1;
int c = 2;
int d = 3;
t1->foo(0, &a);
t1->foo(1, &b);
t1->foo(2, &c);
//t1->i.push_back(&d); //add this, size will be 1
cout << "vector size is " << (t1->i).size() << endl; //size only = 0
for(int k = 0; k < 3; k++){
cout << "slot id is " << k << " int number is " << * ( (t1->i)[k] ) << endl;
}
/*
vector<int*>::iterator it = (t1->i).begin()
for(int k = 0; it != (t1->i).end(); it++, k++){
cout << "slot id is " << k << " int number is " << *(*it) << endl;
}*/
delete t1;
return 0;
}