当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ vector::resize()用法及代码示例


向量被称为动态数组,当插入或删除元素时可以自动更改其大小。该存储由容器维护。

vector::resize()

该函数通过插入或删除容器中的元素来实际更改容器的内容。事情是这样发生的,

  • 如果给定的 n 值小于当前的大小,则多余的元素将被拆除。
  • 如果 n 大于容器的当前大小,则即将到来的元素将附加到向量的末尾。

用法:

vectorname.resize(int n, int val)

以下示例程序旨在说明该函数的工作原理

1.矢量容器的尺寸减小。

CPP


// resizing of the vector
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> vec;
    // 5 elements are inserted
    // in the vector
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(4);
    vec.push_back(5);
    cout << "Contents of vector before resizing:"
         << endl;
     
    // displaying the contents of the
    // vector before resizing
    for (int i = 0; i < vec.size(); i++)
        cout << vec[i] << " ";
    cout << endl;
    // vector is resized
    vec.resize(4);
    cout << "Contents of vector after resizing:"
         << endl;
     
    // displaying the contents of the
    // vector after resizing
    for (int i = 0; i < vec.size(); i++)
        cout << vec[i] << " ";
    return 0;
}
输出
Contents of vector before resizing:
1 2 3 4 5 
Contents of vector after resizing:
1 2 3 4 

2.矢量容器的大小增加。

CPP


// resizing of the vector
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> vec;
    // 5 elements are inserted 
    // in the vector
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(4);
    vec.push_back(5);
    cout << "Contents of vector before resizing:"
         << endl;
     
    // displaying the contents of the
    // vector before resizing
    for (int i = 0; i < vec.size(); i++)
        cout << vec[i] << " ";
    cout << endl;
    // vector is resized
    vec.resize(8);
    cout << "Contents of vector after resizing:"
         << endl;
    // displaying the contents of 
    // the vector after resizing
    for (int i = 0; i < vec.size(); i++)
        cout << vec[i] << " ";
    return 0;
}
输出
Contents of vector before resizing:
1 2 3 4 5 
Contents of vector after resizing:
1 2 3 4 5 0 0 0 

3.增加向量容器的大小,并使用指定值初始化新元素。

CPP


// resizing of the vector
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> vec;
    // 5 elements are inserted
    // in the vector
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(4);
    vec.push_back(5);
    cout << "Contents of vector before resizing:"
         << endl;
    // displaying the contents of 
    // the vector before resizing
    for (int i = 0; i < vec.size(); i++)
        cout << vec[i] << " ";
    cout << endl;
    // vector is resized
    vec.resize(12, 9);
    cout << "Contents of vector after resizing:"
         << endl;
     
    // displaying the contents 
    // of the vector after resizing
    for (int i = 0; i < vec.size(); i++)
        cout << vec[i] << " ";
    return 0;
}
输出
Contents of vector before resizing:
1 2 3 4 5 
Contents of vector after resizing:
1 2 3 4 5 9 9 9 9 9 9 9 

如何在 2D 矢量(矢量的矢量)的情况下调整大小?

方法/遵循的步骤 - 种类 1:

  • 我们可以先调整外容器向量<vector<int>>的大小
  • 通过这样做,我们把它变成了 { { }, { }, { } }
  • 然后我们将初始化内部容器。
vector.resize(n, value);

C++


//Resizing a 2D Vector / Matrix
#include <bits/stdc++.h>
using namespace std;
void displayMatrix(vector<vector<int>> v){
    int N = v.size();
    int M = v[0].size();
    for(int i=0; i<N; i++){
        for(int j=0; j<M; j++){
            cout<<v[i][j]<<" ";
        }
        cout<<"\n";
    }
}
int main()
{
     
    vector<vector<int>> v;
     
    cout<<"N: "<<v.size()<<"\n"; //4
     
    v.resize(4, vector<int> (4, -1));
     
    cout<<"N: "<<v.size()<<" M: "<<v[0].size()<<"\n"; //4
    displayMatrix(v);
    return 0;
}
//Code done by Balakrishnan R (rbkraj000)
输出
N: 0
N: 4 M: 4
-1 -1 -1 -1 
-1 -1 -1 -1 
-1 -1 -1 -1 
-1 -1 -1 -1 

另一种方法 - Variety-2:

C++


//Resizing a 2D Vector / Matrix
#include <bits/stdc++.h>
using namespace std;
void displayMatrix(vector<vector<int> > v)
{
    int N = v.size();
    int M = v[0].size();
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cout << v[i][j] << " ";
        }
        cout << "\n";
    }
}
int main()
{
    vector<vector<int> > v;
    cout << "N: " << v.size() << "\n"; // 4
    v.resize(4);
    for (int i = 0; i < 4; i++)
        v[i].resize(4, -1);
    cout << "N: " << v.size() << " M: " << v[0].size()
         << "\n"; // 4
    displayMatrix(v);
    return 0;
}
//Code done by Balakrishnan R (rbkraj000)
输出
N: 0
N: 4 M: 4
-1 -1 -1 -1 
-1 -1 -1 -1 
-1 -1 -1 -1 
-1 -1 -1 -1 

本文中的上述 2 种方法由 Balakrishnan R. 贡献。



相关用法


注:本文由纯净天空筛选整理自akash1295大神的英文原创作品 vector : : resize() in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。