C++ vector::reserve() 函数



C++ `vector::reserve()` 函数用于预留向量的容量。容量必须足够大,以便能够容纳 n 个元素。任何给定向量,只要其容量大于或等于我们在 `reserve()` 函数中指定的新的容量,其容量就可以通过 `reserve()` 函数来增加。

`reserve()` 函数只会预留向量的空间;它不会增加向量的大小。如果预留的向量大小大于当前大小,所有这些调整都将失效。`reserve()` 函数的时间复杂度最多为线性。

语法

以下是 C++ `vector::reserve()` 函数的语法:

void reserve (size_type n);

参数

n − 表示将存储在向量中的元素数量。

示例 1

让我们考虑以下示例,我们将使用 `reserve()` 函数。

#include <iostream>
#include <vector>
using namespace std;

int main(void) {
   vector<int> v1;
   vector<int> v2;
   ssize_t size;
   size = v1.capacity();
   for (int i = 0; i < 25; ++i) {
      v1.push_back(i);
      if (size != v1.capacity()) {
         size = v1.capacity();
         cout << "Expanding vector v1 to hold " << size<< " elements" << endl;
      }
   }
   cout << endl << endl;
   v2.reserve(25);
   for (int i = 0; i < 25; ++i) {
      v2.push_back(i);
      if (size != v2.capacity()) {
         size = v2.capacity();
         cout << "Expanding vector v2 to hold " << size<< " elements" << endl;
      }
   }
   return 0;
}

输出

编译并运行上述程序后,将产生以下结果:

Expanding vector v1 to hold 1 elements
Expanding vector v1 to hold 2 elements
Expanding vector v1 to hold 4 elements
Expanding vector v1 to hold 8 elements
Expanding vector v1 to hold 16 elements
Expanding vector v1 to hold 32 elements


Expanding vector v2 to hold 25 elements

示例 2

考虑另一种情况,我们将分配和释放空间。

#include <cstddef>
#include <iostream>
#include <new>
#include <vector>
template<class Tp>

struct tutorial{
   typedef Tp value_type;
   tutorial() = default;
   template<class x>
   tutorial(const tutorial<x>&) {}
   Tp* allocate(std::size_t n){
      n *= sizeof(Tp);
      Tp* p = static_cast<Tp*>(::operator new(n));
      std::cout << "allocating " << n << " bytes @ " << p << '\n';
      return p;
   }
   void deallocate(Tp* p, std::size_t n){
      std::cout << "deallocating " << n * sizeof *p << " bytes @ " << p << "\n\n";
      ::operator delete(p);
   }
};
template<class x, class y>
bool operator==(const tutorial<x>&, const tutorial<y>&) { return true; }
template<class x, class y>
bool operator!=(const tutorial<x>&, const tutorial<y>&) { return false; }
int main(){
   constexpr int max_elements = 10;
   std::cout << "using reserve() function : \n";
   {
      std::vector<int, tutorial<int>> v1;
      v1.reserve(max_elements); 
      for (int n = 0; n < max_elements; ++n)
         v1.push_back(n);
   }
}

输出

运行上述程序后,将产生以下结果:

using reserve() function : 
allocating 40 bytes @ 0x55981828e2c0
deallocating 40 bytes @ 0x55981828e2c0

示例 3

在下面的示例中,我们将检查使用 `reserve()` 函数和不使用 `reserve()` 函数的情况。

#include <cstddef>
#include <iostream>
#include <new>
#include <vector>
template<class a>

struct tutorial{
   typedef a value_type;
   tutorial() = default;
   template<class x>
   tutorial(const tutorial<x>&) {}
   a* allocate(std::size_t n){
      n *= sizeof(a);
      a* p = static_cast<a*>(::operator new(n));
      std::cout << "allocating " << n << " bytes @ " << p << '\n';
      return p;
   }
   void deallocate(a* p, std::size_t n){
      std::cout << "deallocating " << n * sizeof *p << " bytes @ " << p << "\n\n";
      ::operator delete(p);
   }
};
template<class x, class y>
bool operator==(const tutorial<x>&, const tutorial<y>&) { return true; }
template<class x, class y>
bool operator!=(const tutorial<x>&, const tutorial<y>&) { return false;}
int main(){
   constexpr int max_elements = 8;
   std::cout << "using reserve() function: \n";
   {
      std::vector<int, tutorial<int>> v1;
      v1.reserve(max_elements); 
      for (int n = 0; n < max_elements; ++n)
         v1.push_back(n);
   }
   std::cout << "without reserve() function : \n";
   {
      std::vector<int, tutorial<int>> v1;
      for (int n = 0; n < max_elements; ++n){
         if (v1.size() == v1.capacity())std::cout << "size() == capacity() == " << v1.size() << '\n';
            v1.push_back(n);
      }
   }
}

输出

执行上述程序后,将产生以下结果:

using reserve() function: 
allocating 32 bytes @ 0x55d7dd6ca2c0
deallocating 32 bytes @ 0x55d7dd6ca2c0

without reserve() function : 
size() == capacity() == 0
allocating 4 bytes @ 0x55d7dd6ca2f0
size() == capacity() == 1
allocating 8 bytes @ 0x55d7dd6ca310
deallocating 4 bytes @ 0x55d7dd6ca2f0

size() == capacity() == 2
allocating 16 bytes @ 0x55d7dd6ca2f0
deallocating 8 bytes @ 0x55d7dd6ca310

size() == capacity() == 4
allocating 32 bytes @ 0x55d7dd6ca2c0
deallocating 16 bytes @ 0x55d7dd6ca2f0

deallocating 32 bytes @ 0x55d7dd6ca2c0
广告