向量被稱為動態數組,當插入或刪除元素時可以自動更改其大小。該存儲由容器維護。
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. 貢獻。
相關用法
- C++ vector::resize()用法及代碼示例
- C++ vector::reserve()用法及代碼示例
- C++ vector::rend()用法及代碼示例
- C++ vector::rbegin()用法及代碼示例
- C++ vector::at()用法及代碼示例
- C++ vector::back()用法及代碼示例
- C++ vector::begin()用法及代碼示例
- C++ vector::capacity()用法及代碼示例
- C++ vector::cbegin()用法及代碼示例
- C++ vector::cend()用法及代碼示例
- C++ vector::clear()用法及代碼示例
- C++ vector::crbegin()用法及代碼示例
- C++ vector::crend()用法及代碼示例
- C++ vector::data()用法及代碼示例
- C++ vector::empty()用法及代碼示例
- C++ vector::end()用法及代碼示例
- C++ vector::erase()用法及代碼示例
- C++ vector::front()用法及代碼示例
- C++ vector::insert()用法及代碼示例
- C++ vector::max_size()用法及代碼示例
- C++ vector::operator[]用法及代碼示例
- C++ vector::pop_back()用法及代碼示例
- C++ vector::push_back()用法及代碼示例
- C++ vector::shrink_to_fit()用法及代碼示例
- C++ vector::size()用法及代碼示例
注:本文由純淨天空篩選整理自akash1295大神的英文原創作品 vector : : resize() in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。