如何在C ++中调整数组大小? [英] How to resize array in C++?
本文介绍了如何在C ++中调整数组大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
您可以在初始化后调整C ++数组的大小吗?
我需要在C ++中做相当于下面的C#代码
I need to do the equivalent of the following C# code in C++
Array.Resize(ref A, A.Length - 1);
如何在C ++中实现?
How to achieve this in C++?
推荐答案
数组的大小在C ++中是静态的。您不能动态调整它的大小。这就是 std :: vector
is for:
The size of an array is static in C++. You cannot dynamically resize it. That's what std::vector
is for:
std::vector<int> v; // size of the vector starts at 0
v.push_back(10); // v now has 1 element
v.push_back(20); // v now has 2 elements
v.push_back(30); // v now has 3 elements
v.pop_back(); // removes the 30 and resizes v to 2
v.resize(v.size() - 1); // resizes v to 1
这篇关于如何在C ++中调整数组大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文