c*********n 发帖数: 128 | 1 Does this function have memory problem?
void pushSomthingIntoVector(vector & v) { //T is a class
v.assign(0, T(100)) //T(int a) is a constructor of class T
v.push_back(T(50))
}
void anotherFunction() {
vector v;
pushSomethingIntoVector(v);
cout << v[0] << endl;
cout << v[1] << endl;
}
T(100) and T(50) are created locally within the function
pushSomthingIntoVector(vector & v), does it exsit outside of the function
? say, in the two lines of "cout" ?
Should I change the code as follows: | t****t 发帖数: 6806 | 2 all STL containers do copy in and copy out. you take care of all objects
outside the container, and the container will take care of the objects it
contains.
【在 c*********n 的大作中提到】 : Does this function have memory problem? : void pushSomthingIntoVector(vector & v) { //T is a class : v.assign(0, T(100)) //T(int a) is a constructor of class T : v.push_back(T(50)) : } : void anotherFunction() { : vector v; : pushSomethingIntoVector(v); : cout << v[0] << endl; : cout << v[1] << endl;
|
|