F_JustWei's Studio.

vector中resize与reserve的区别

字数统计: 1.1k阅读时长: 5 min
2021/05/15 Share

vector中resize与reserve的区别

resize()

resize() 函数的作用是改变 vector 元素个数。

resize(n,m) 第二个参数可以省略,n 代表改变元素个数为 n ,m 代表初始化为 m 。

  • resize() 可以改变有效空间的大小。
  • 如果 n 比 vector 的 size 小,结果是 size 减小到 n ,然后删除 n 之后的数据。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void test1() {
vector<int> v;
v.reserve(20);
v.resize(10);
cout << "vector.size() = " << v.size() << endl;
cout << "vector.capacity() = " << v.capacity() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
v.resize(5);
cout << "vector.size() = " << v.size() << endl;
cout << "vector.capacity() = " << v.capacity() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
}
输出:
1
2
3
4
5
6
vector.size()     = 10
vector.capacity() = 20
0 0 0 0 0 0 0 0 0 0
vector.size() = 5
vector.capacity() = 20
0 0 0 0 0
  • 如果 n 比 vector 的 size 大,比 vector 的 capacity 小,结果是增加 size 并初始化,如果指定了第二参数,则初始化为指定值,没指定初始化则为缺省值,capacity 不变。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void test2() {
vector<int> v;
v.reserve(20);
v.resize(10);
cout << "vector.size() = " << v.size() << endl;
cout << "vector.capacity() = " << v.capacity() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
v.resize(15, 1);//v.resize(15)
cout << "vector.size() = " << v.size() << endl;
cout << "vector.capacity() = " << v.capacity() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
}
输出:
1
2
3
4
5
6
vector.size()     = 10
vector.capacity() = 20
0 0 0 0 0 0 0 0 0 0
vector.size() = 15
vector.capacity() = 20
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1
  • 如果 n 比 vector 中的 capacity 大,结果是先增加容量,然后增加 size 并初始化。capacity 和 size 均改变。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void test1() {
vector<int> v;
v.reserve(20);
v.resize(10);
cout << "vector.size() = " << v.size() << endl;
cout << "vector.capacity() = " << v.capacity() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
v.resize(30, 1);//v.resize(30);
cout << "vector.size() = " << v.size() << endl;
cout << "vector.capacity() = " << v.capacity() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
}
输出:
1
2
3
4
5
6
vector.size()     = 10
vector.capacity() = 20
0 0 0 0 0 0 0 0 0 0
vector.size() = 30
vector.capacity() = 30
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

reserve()

reserve() 函数的作用是改变总空间的大小。

reverse(n)只有一个参数。

  • 如果 n 的值比大于 vector 的 size,则 capacity 增容到 n ,size 不变。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void test1() {
vector<int> v;
v.reserve(20);
v.resize(10);
cout << "vector.size() = " << v.size() << endl;
cout << "vector.capacity() = " << v.capacity() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
v.reserve(30);
cout << "vector.size() = " << v.size() << endl;
cout << "vector.capacity() = " << v.capacity() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
}
输出:
1
2
3
4
5
6
vector.size()     = 10
vector.capacity() = 20
0 0 0 0 0 0 0 0 0 0
vector.size() = 10
vector.capacity() = 30
0 0 0 0 0 0 0 0 0 0
  • 如果 n 的值比小于或者等于 vector 的 size ,则 capacity 不变,size 也不变。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void test1() {
vector<int> v;
v.reserve(20);
v.resize(10);
cout << "vector.size() = " << v.size() << endl;
cout << "vector.capacity() = " << v.capacity() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
v.reserve(10);
cout << "vector.size() = " << v.size() << endl;
cout << "vector.capacity() = " << v.capacity() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
}
输出:
1
2
3
4
5
6
vector.size()     = 10
vector.capacity() = 20
0 0 0 0 0 0 0 0 0 0
vector.size() = 10
vector.capacity() = 20
0 0 0 0 0 0 0 0 0 0
CATALOG
  1. 1. vector中resize与reserve的区别
    1. 1.0.1. resize()
      1. 1.0.1.0.1. 示例:
      2. 1.0.1.0.2. 输出:
      3. 1.0.1.0.3. 示例:
      4. 1.0.1.0.4. 输出:
      5. 1.0.1.0.5. 示例:
      6. 1.0.1.0.6. 输出:
  2. 1.0.2. reserve()
    1. 1.0.2.0.1. 示例:
    2. 1.0.2.0.2. 输出:
    3. 1.0.2.0.3. 示例:
    4. 1.0.2.0.4. 输出: