Suppose the following data structure typedef std::vector<std::vector<int> > MYARRAY
is defined. Then for the variable MYARRAY var
, how can I allocate memory for this variable before pushing data in it. For example,
std::vector<int> p1;
p1.push_back(1);
p1.push_back(2);
std::vector<int> p2;
p2.push_back(22);
p2.push_back(33);
var.push_back(p1);
var.push_back(p2);
If we do not allocate memory for var
, then it will allocate memory automatically. So how can I allocate memory for this var before pushing data insider it? If it is std::vector<int> var2
, I can just use var2.reserve(n)
to allocate memory before using it.
EDIT: Two suggestions have been made, but neither can work:
var.reserve(3); for(int i=0; i<3; i++) var[i].reserve[20];
I use VC 2010 to compile and run the codes in the debug mode, and the following error message is given:
Solution 2: create the object in the beginning
std::vector<std::vector<int> > var(3, std::vector<int>(5));
After you created this variable, you can see this variable in VC 2010: Its contents are already there. Therefore, if you push data on this variable, it will allocate memory once again.
EDIT 2:
Someone is interested in why I need allocate memory before using this variable, and the main reason is because of run-time library of windows. The variable var
is defined in a executable program as an empty variable, and its contents are given by a function defined in a dynamic library. If both are using dynamic run-time library, it will not be an issue. But in my case both are linked with static run-time library, which means that each module is in charge of its memory allocation. Since var
is defined in the executable program, it also should take care of its memory allocation.
The way you reserve memory for a vector is independent of what type the vector contains. If you want to reserve space for n
elements in MYARRAY
, call MYARRAY.reserve(n);
.
Its contents are already there. Therefore, if you push data on this variable, it will allocate memory once again.
Right. Do you want to reserve memory or not? If you want to reserve memory, you'll have to use the memory you reserved. In order for you to "push data on this variable", you'd have to have the data somewhere, somewhere other than the memory you reserved. If you reserve memory and use that memory, you'll never have anything to push, because that would imply that you have something someplace other than in the memory you reserved that you need to add to the vector, which you couldn't possibly have.
You basically have three choices:
1) Don't reserve memory. Assemble the objects wherever you want and then push them into the vector. vec.push_back(myVector);
2) Reserve memory. Assemble the objects in place in the vector. vec[n].push_back(myInt);
3) Reserve memory. Assemble the objects wherever you want and then assign them into the memory you reserved. vec[n]=myIntVector
Notice that in none of these cases do you reserve memory and then push into the vector.
Like you already pointed out, std::vector
has the reserve
method that will reserve space for more data items. If you were to do p1.reserve(3)
the vector
would attempt to allocate space for 3 integers. If you run var.reserve(3)
, var
will attempt to allocate space for 3 std::vector<int>
's, which is what it sounds like you want to do.
To allocate for the std::vector
's inside of var
, you could do:
for(int x=0; x<var.size(); x++) var[x].reserve(n);
If you want to allocate space before inserting the vectors, you can declare var
as:
std::vector<std::vector<int> > var(VEC_COUNT, std::vector<int>(n));
And then copy the new vectors in.
You cannot reserve space for vector's data before the vector exists. I believe you're looking for this:
void reserve(MYARRAY &arr, size_t dim1, size_t dim2)
{
arr.resize(dim1);
for (size_t idx = 0; idx < dim1; ++idx) {
arr[idx].reserve(dim2);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.