码迷,mamicode.com
首页 > 其他好文 > 详细

vector函数

时间:2017-11-04 00:00:54      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:函数   sign   remove   double   increase   rom   insert   realloc   array   

A vector<T> is like an array of T, but supports copying, assignment, and comparison. Its size can be set and changed at run time, and it can efficiently implement a stack (O(1) time to push or pop). It has random iterators like string, which behave like type T* (or const T* if the vector is const). If T is numeric, elements are initialized to 0. It is not possible to have an initialization list such as {1,2,3}.
  vector<T>()            // Empty vector, elements of type T
  vector<T>(n)           // n elements, default initialized
  vector<T>(n, x)        // n elements each initialized to x
  vector<T> v2=v;        // Copy v to v2
  v2=v;                  // Assignment
  v2<v                   // Also >, ==, !=, <=, >= if defined for T
  vector<T>(b, e)        // Initialize to sequence [b, e)
  v.size()               // n
  vector<T>::size_type   // Type of v.size(), usually unsigned int
  v.empty()              // true if v.size() == 0
  v[i]                   // i‘th element, 0 <= i < v.size() (unchecked), may be assigned to
  v.at(i)                // v[i] with bounds check, throws out_of_range
  v.begin(), v.end()     // Iterators [b, e)
  vector<T>::iterator    // Iterator type, also const_iterator
  v.back()               // v[v.size()-1] (unchecked if empty)
  v.push_back(x)         // Increase size by 1, copy x to last element
  v.pop_back()           // Decrease size by 1 (unchecked if empty)
  v.front()              // v[0] (unchecked)
  v.resize(n)            // Change size to n >= 0 (unchecked)
  v.insert(d, x)         // Insert x in front of iterator d, shift, increase size by 1
  v.insert(d, n, x)      // Insert n copies of x in front of d
  v.insert(d, b, e)      // Insert copy of [b, e) in front of d
  v.erase(d)             // Remove *d, shift, decrease size by 1
  v.erase(d, e)          // Remove subsequence [d, e)
  v.clear()              // v.erase(v.begin(), v.end())
  v.reserve(n)           // Anticipate that v will grow to size n >= v.size()
  v.capacity()           // Reserved size
For insert and erase, d and e must point into v (and d <= e) or the program may crash. Elements from *d to the end are shifted and the size is changed as needed. Saved copies of iterators may become invalid after any change of size or capacity (not checked).
To implement push_back() efficiently, a vector typically doubles the reserved space when it runs out in order to minimize memory reallocation and copying. reserve() allows this strategy to be optimized.

  // Read words from input into a stack, print in reverse order
  string s;
  vector<string> v;
  while (cin >> s)
    v.push_back(s);
  while (!v.empty()) {
    cout << v.back() << endl;
    v.pop_back();
  }

 

vector函数

标签:函数   sign   remove   double   increase   rom   insert   realloc   array   

原文地址:http://www.cnblogs.com/enyala/p/7780522.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!