I'm just starting with my c++ code writing. For my project, I need a vector of my own class (lipid). The reason for the vector usage is that I want to be able to change the vector size between runs.
Anyhow, I created the vector as such:
vector <lipid> lipids;
and I have change the size of the vector late on as such:
lipids.resize(lipidsNum);
My problem is that I will most probably want to use a constructor on the lipids and I can't seem to find the way how. I tried things like:
vector will automatically call the default constructor. You don't have to worry about it. It will also automatically destruct objects when the vector is destroyed (or when the size is reduced)
If you want to call a constructor other than the default, you can pass it as the 2nd parameter to resize:
Is it possible to create or destroy a specific object in a vector?
If you have a vector of objects (like a vector<lipid>), then vector creates/destroys the objects when you resize the vector.
You can add/remove specific elements with insert() and erase() functions, but note that there are performance hits when you insert/erase because other elements in the vector need to be shifted over to adjust for the change.
Alternatively you can use push_back and pop_back to add/remove elements from the end of the vector. Performance for that is much better than insert/erase.
alternatively, how can I create 1000 different lipids?
Set the size of the vector to 1000:
1 2 3 4 5
vector<lipid> lipids(1000); // you now have 1000 lipids
lipids.resize(2000); // you now have 2000 lipids
lipids.resize(10); // now you only have 10
I need to create 1000 lipids which are different one from another. I don't really care about performance, the vector is created once at the start time of the program. I think that with a decent computer it will take me several seconds to create the vector, the run time of the program should be about two or three weeks.
void init_lipid_vector(vector<lipid> & lipids)
{
//set your vector's size
lipids.resize(1000);
//a temporary variable to hold
//the current file entry
lipid current;
//open the file for input
ifstream fin("data.dat");
int i=0;
while(true)
{
//read current lipid from file...
fin>>current;
//quit when reaching the end of file
//or if some error occurs
if (!fin) break;
//and store it to the vector
lipids[i++]=current;
}
//close the file
fin.close();
}
I am planning on storing these lipids on a file. Is this method read the file line by line? I assume that I need to overload the ">>" operator, isn't it so?
void init_lipid_vector(vector<lipid> & lipids)
{
//reserve the vector so that you have space for
// at least 1000 elements without having to reallocate
lipids.reserve(1000);
//a temporary variable to hold
//the current file entry
lipid current;
//open the file for input
ifstream fin("data.dat");
while(true)
{
//read current lipid from file...
fin>>current;
//quit when reaching the end of file
//or if some error occurs
if (!fin) break;
//and store it to the vector
lipids.push_back(current);
}
//close the file
fin.close();
}
Advantages to this approach:
- if the file contains less than 1000 lipids, you won't have lingering lipids at the end of your vector. If the file only had 800 lipids, then your vector will only have 800 lipids.
- if the file contains more than 1000 lipids, your program won't explode. The vector will grow appropriately.
Moreover, shrinking the size of the vector does not reduce the capacity. So you can regrow it without new memory allocations until the capacity is exceeded. Use std::deque if you want to have a vector like interface but fast insertions at the beginning and end. Also deque has no capacity interface so it is more likely to actually grow and shrink when you resize.
1 2 3 4 5
vector<lipid> lipids(1000); // you now have 1000 lipids
lipids.resize(2000); // you now have 2000 lipids
lipids.resize(10); // now you only have 10, kf: capacity is still >= 2000