C++ 雙端佇列庫 - deque() 函式



描述

C++ 範圍建構函式 std::deque::deque() 構造一個雙端佇列,其元素數量與範圍內的元素數量相同起始結束。此容器的儲存需求由內部分配器.

宣告

以下是來自 std::deque 標頭檔案的 std::deque::deque() 建構函式的宣告。

C++98

template <class InputIterator>
deque (InputIterator first, InputIterator last,
       const allocator_type& alloc = allocator_type());

C++11

template <class InputIterator>
deque (InputIterator first, InputIterator last,
       const allocator_type& alloc = allocator_type());

引數

  • alloc − 儲存分配器。

  • first − 範圍中初始位置的輸入迭代器。

  • last − 範圍中最終位置的輸入迭代器。

返回值

建構函式永不返回值。

異常

如果由起始結束指定的範圍無效,則結果未定義。

時間複雜度

線性,即 O(n)

示例

以下示例演示了 std::deque::deque() 建構函式的用法。

#include <iostream>
#include <deque>

using namespace std;

int main(void) {

   deque<int> d1 = {1, 2, 3, 4, 5};
   deque<int> d2(d1.begin(), d1.begin() + 3);

   cout << "Contents of deque are" << endl;

   for (int i = 0; i < d2.size(); ++i)
      cout << d2[i] << endl;

   return 0;
}

讓我們編譯並執行上面的程式,這將產生以下結果:

Contents of deque are
1
2
3
deque.htm
廣告