The usage of resize() in vector is exhausted

The usage of resize() in vector is exhausted

先赞后看,养成好习惯。有帮助的话,点波关注!我会坚持更新,感谢谢您的支持!

Reference :
std::vector::resize

Requirements : There is a problem of setting the vector to 0 in the debugging program, and it is finally located that the resize is used improperly, so record it.


Simplified description of the situation

1. Initialize the vector, and then want to use resize() to assign values ​​to all elements

vector<int> test = {0,1,2,3,4};
test.resize(10,0);
// 打印结果
for(const auto &value: test) std::cout << value << ","; 
std::cout << std::endl;

It is usually thought that the printed result is 10 0s, but it is actually0,1,2,3,4,0,0,0,0,0,

2. Resize() function description

2.1 Function prototype (C++11)

void resize (size_type n);
void resize (size_type n, const value_type& val);

2.2 Official explanation

Resizes the container so that it contains n elements.

If n is smaller than the current container size, the content is reduced to its first n elements, removing those beyond (and destroying them).

If n is greater than the current container size, the content is expanded by inserting at the end as many elements as needed to reach a size of n. If val is specified, the new elements are initialized as copies of val, otherwise, they are value-initialized.

If n is also greater than the current container capacity, an automatic reallocation of the allocated storage space takes place.

Notice that this function changes the actual content of the container by inserting or erasing elements from it.

2.3 Instructions for use

  • Resizes the container to contain n elements
  • If n is less than the current container size, the content is reduced to its first n elements, and 删除those beyond (and freeing them).
  • If n is greater than the current container size, the content is expanded by the required number of elements at the end 插入to reach the size of n. If val is specified, the new elements are initialized to be copies of val, otherwise, they are initialized to 0.
  • If n is also greater than the current container capacity, the allocated storage is automatically reallocated.

Note that this function 插入或删除changes the actual content of the container by element.

2.4 Practical examples

Borrow the official example.

// resizing vector
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector;

  // set some initial content:
  for (int i=1;i<10;i++) myvector.push_back(i);

  myvector.resize(5);
  myvector.resize(8,100);
  myvector.resize(12);

  std::cout << "myvector contains:";
  for (int i=0;i<myvector.size();i++)
    std::cout << ' ' << myvector[i];
  std::cout << '\n';

  return 0;
}

Result :
myvector contains: 1 2 3 4 5 100 100 100 0 0 0 0

Explanation :

  • Initialize 9 elements, from 1 to 9.
  • The first operation resize(5) will delete the last 4 elements, leaving 1 2 3 4 5 at this time.
  • The second operation, resize(8,100), will insert 3 elements at the end, and the value of the 3 elements is 100.
  • The third operation, resize(12), will insert 12-8=4 elements at the end again. Since the element does not provide an initial value, the default value is 0.

2.5 Expansion

The real requirement in the project is to set all the elements in the member variable vector to 0 at the beginning of the program, and finally use the std::fill() fill function, which clear()will clear the value but not release the memory.

std::fill(input_data_.begin(), input_data_.end(), 0);  // 填充
input_data_.clear(); // 清空后,遍历打印则看不到结果

Guess you like

Origin blog.csdn.net/weixin_36354875/article/details/126274661