Article 3: If resize() function is used on std::vector
then push_back will insert after the size requested in the function.
Explanation:
int main()
{
std::vector<int> data;
for ( int i = 1; i <= 5; i++)
{
data.push_back(i);
}
// Here Data : 1,2,3,4,5
data.resize(10);
// Here Data : 1,2,3,4,5,0,0,0,0,0
for ( int i = 11; i <= 15; i++)
{
data.push_back(i);
}
// Here Data : 1,2,3,4,5,0,0,0,0,0,11,12,13,14,15
}
So whenever resize() function is used the push_back() will do the insertion from the
end own-wards.
NOTE: To avoid such
situation use [] to insert after resize() function.
e.g.
for ( int i = 10; i <=
15; i++)
{
data[i] = i;
}
// Here Data :
1,2,3,4,5,6,7,8,9,10
No comments:
Post a Comment