std::vector::resize() 與 std::vector::reserve() 在 C++ 中


向量具有向量當插入或刪除元素時可自動調整自身大小的能力,容器會自動處理這些儲存空間。

vector resize() 和 vector reserve() 的主要區別在於 resize() 用於更改向量大小,而 reserve() 不會。reserve() 僅用於儲存至少指定數量的元素,而無需重新分配記憶體。但在 resize() 中,如果這個數字小於當前數字,則會調整記憶體大小,並刪除其上多餘的空間。

vector::resize()

Vector resize() is used to change its size.

示例

原始碼步驟

Begin
   Declare a variable v of vector type.
   Declare another variable it as iterator of vector type.
   Declare another two variable c and i to the ineger datatype.
   while (TRUE) do
      print “1.Size of the Vector”.
      print “2.Insert Element into the Vector”.
      print “3.Resize the vector”.
      print “4.Display by Iterator”.
      print “5.Exit”.
      print “Enter your Choice:”.
      Enter the value of variable c.
      Switch(c)
         Case 1.
            Print “Size of Vector:”.
            Call size() function to print the size of the vector.
            Break.
         Case 2.
            Print “Enter value to be inserted:”.
            Enter the value of variable i.
            Call push_back() function to input the values in the vector.
            Break.
         Case 3.
            Print “Resize the vector elements:”.
            Call resize() function to resize the vector.
            Break.
         Case 4.
           Print “Displaying Vector by Iterator:”.
            for (it = v.begin(); it != v.end(); it++)
               print the value of iterator it.
            Break.
         case 5.
            call exit() function to take exit.
            break.
         Default.
            Print “Wrong choice”.
End.

示例

#include <iostream>
#include <vector>
using namespace std;
int main() {
   vector<int> v;
   vector<int>::iterator it;
   int c, i;
   while (1) {
      cout<<"1.Size of the Vector"<<endl;
      cout<<"2.Insert Element into the Vector"<<endl;
      cout<<"3.Resize the vector"<<endl;
      cout<<"4.Display by Iterator"<<endl;
      cout<<"5.Exit"<<endl;
      cout<<"Enter your Choice: ";
      cin>>c;
      switch(c) {
         case 1:
            cout<<"Size of Vector: ";
            cout<<v.size()<<endl; //printing the size of the vector
            break;
         case 2:
            cout<<"Enter value to be inserted: ";
            cin>>i;
            v.push_back(i); //inserting values
            break;
         case 3:
            cout<<"Resize the vector elements:"<<endl;
            v.resize(4); //resize the vector
            break;
         case 4:
            cout<<"Displaying Vector by Iterator: ";
            for (it = v.begin(); it != v.end(); it++) //printing all values of the vector {
               cout<<*it<<" ";
            }
            cout<<endl;
            break;
         case 5:
            exit(1);
            break;
         default:
            cout<<"Wrong Choice"<<endl;
      }
   }
   return 0;
}

輸出

1.Size of the Vector
2.Insert Element into the Vector
3.Resize the vector
4.Display by Iterator
5.Exit
Enter your Choice: 1
Size of Vector: 0
1.Size of the Vector
2.Insert Element into the Vector
3.Resize the vector
4.Display by Iterator
5.Exit
Enter your Choice: 2
Enter value to be inserted: 1
1.Size of the Vector
2.Insert Element into the Vector
3.Resize the vector
4.Display by Iterator
5.Exit
Enter your Choice: 2
Enter value to be inserted: 2
1.Size of the Vector
2.Insert Element into the Vector
3.Resize the vector
4.Display by Iterator
5.Exit
Enter your Choice: 2
Enter value to be inserted: 4
1.Size of the Vector
2.Insert Element into the Vector
3.Resize the vector
4.Display by Iterator
5.Exit
Enter your Choice: 2
Enter value to be inserted: 5
1.Size of the Vector
2.Insert Element into the Vector
3.Resize the vector
4.Display by Iterator
5.Exit
Enter your Choice: 2
Enter value to be inserted: 5
1.Size of the Vector
2.Insert Element into the Vector
3.Resize the vector
4.Display by Iterator
5.Exit
Enter your Choice: 4
Displaying Vector by Iterator: 1 2 4 5 5
1.Size of the Vector
2.Insert Element into the Vector
3.Resize the vector
4.Display by Iterator
5.Exit
Enter your Choice: 3
Resize the vector elements:
1.Size of the Vector
2.Insert Element into the Vector
3.Resize the vector
4.Display by Iterator
5.Exit
Enter your Choice: 4
Displaying Vector by Iterator: 1 2 4 5
1.Size of the Vector
2.Insert Element into the Vector
3.Resize the vector
4.Display by Iterator
5.Exit
Enter your Choice: 5

vector::reserve()

Vector reserve() 指示建立了該向量,以便它可以儲存至少指定數量的元素而無需重新分配記憶體。示例中的步驟

演算法

Begin
   Declare a variable v of vector type.
   Declare another variable it as iterator of vector type.
   Declare another two variable c and i to the ineger datatype.
   while (1) do
      print “1.Size of the Vector”.
      print “2.Insert Element into the Vector”.
      print “3.Reserve the vector”.
      print “4.Display by Iterator”.
      print “5.Exit”.
      print “Enter your Choice:”.
      Enter the value of variable c.
      Switch(c)
         Case 1.
            Print “Size of Vector:”.
            Call size() function to print the size of the vector.
            Break.
         Case 2.
            Print “Enter value to be inserted:”.
            Enter the value of variable i.
            Call push_back() function to input the values in the vector.
            Break.
         Case 3.
            Print “Reserve the vector elements:”.
            Call reserve() function to reserve the size of the vector.
               Break.
         Case 4.
            Print “Displaying Vector by Iterator:”.
            for (it = v.begin(); it != v.end(); it++)
               print the value of iterator it.
            Break.
         case 5.
            call exit() function to take exit.
            break.
      Default.
         Print “Wrong choice”.
End.

示例

#include <iostream>
#include <vector>
using namespace std;
int main() {
   vector<int> v;
   vector<int>::iterator it;
   int c, i;
   while (1) {
      cout<<"1.Size of the Vector"<<endl;
      cout<<"2.Insert Element into the Vector"<<endl;
      cout<<"3.Reserve the vector"<<endl;
      cout<<"4.Display by Iterator"<<endl;
      cout<<"5.Exit"<<endl;
      cout<<"Enter your Choice: ";
      cin>>c;
      switch(c) {
         case 1:
            cout<<"Size of Vector: ";
            cout<<v.size()<<endl;
            break;
         case 2:
            cout<<"Enter value to be inserted: ";
            cin>>i;
            v.push_back(i);
            break;
         case 3:
            cout<<"Reserve the vector elements."<<endl;
            v.reserve(100); //reserve the vector elements
            break;
         case 4:
            cout<<"Displaying Vector by Iterator: ";
            for (it = v.begin(); it != v.end(); it++) {
               cout<<*it<<" ";
            }
            cout<<endl;
            break;
         case 5:
            exit(1);
            break;
         default:
            cout<<"Wrong Choice"<<endl;
      }
   }
   return 0;
}

輸出


1.Size of the Vector
2.Insert Element into the Vector
3.Reserve the vector
4.Display by Iterator
5.Exit
Enter your Choice: 1
Size of Vector: 0
1.Size of the Vector
2.Insert Element into the Vector
3.Reserve the vector
4.Display by Iterator
5.Exit
Enter your Choice: 2
Enter value to be inserted: 1
1.Size of the Vector
2.Insert Element into the Vector
3.Reserve the vector
4.Display by Iterator
5.Exit
Enter your Choice: 2
Enter value to be inserted: 2
1.Size of the Vector
2.Insert Element into the Vector
3.Reserve the vector
4.Display by Iterator
5.Exit
Enter your Choice: 2
Enter value to be inserted: 3
1.Size of the Vector
2.Insert Element into the Vector
3.Reserve the vector
4.Display by Iterator
5.Exit
Enter your Choice: 2
Enter value to be inserted: 4
1.Size of the Vector
2.Insert Element into the Vector
3.Reserve the vector
4.Display by Iterator
5.Exit
Enter your Choice: 2
Enter value to be inserted: 5
1.Size of the Vector
2.Insert Element into the Vector
3.Reserve the vector
4.Display by Iterator
5.Exit
Enter your Choice: 3
Reserve the vector elements.
1.Size of the Vector
2.Insert Element into the Vector
3.Reserve the vector
4.Display by Iterator
5.Exit
Enter your Choice: 4
Displaying Vector by Iterator: 1 2 3 4 5
1.Size of the Vector
2.Insert Element into the Vector
3.Reserve the vector
4.Display by Iterator
5.Exit
Enter your Choice: 4
Displaying Vector by Iterator: 1 2 3 4 5
1.Size of the Vector
2.Insert Element into the Vector
3.Reserve the vector
4.Display by Iterator
5.Exit

更新日期:2019-07-30

超過 3K 次瀏覽

開啟您的職業

完成課程獲得認證

立即開始
廣告