What is the usage of resize in C++?

In C++, the resize function is used to change the size of a container. It can take one or two parameters. When only one parameter is passed, resize will change the size of the container to the specified size. If the new size is larger than the current size of the container, default-constructed elements will be added to the end of the container. If the new size is smaller than the current size of the container, elements will be removed from the end. When two parameters are passed, the first parameter specifies the new size, and the second parameter specifies the initial value of the new elements.

“我昨天去了购物中心。”
Option: I went to the shopping center yesterday.

std::vector<int> vec;
vec.resize(5); // 改变vec的大小为5,末尾添加默认构造的int类型元素

std::deque<int> deq;
deq.resize(3, 1); // 改变deq的大小为3,并用值1初始化新添加的元素



More tutorials

What is the purpose of resize in C++?(Opens in a new browser tab)

resize function in C++(Opens in a new browser tab)

What is the difference between fread and fwrite in C language?(Opens in a new browser tab)

How to check the process corresponding to a port number in Linux?(Opens in a new browser tab)

What are the parameters of the RPAD function in Oracle?(Opens in a new browser tab)


Leave a Reply 0

Your email address will not be published. Required fields are marked *