程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 實戰c++中的vector系列--C++11對vector成員函數的擴展(cbegin()、cend()、crbegin()、crend()、emplace()、data())

實戰c++中的vector系列--C++11對vector成員函數的擴展(cbegin()、cend()、crbegin()、crend()、emplace()、data())

編輯:C++入門知識

實戰c++中的vector系列--C++11對vector成員函數的擴展(cbegin()、cend()、crbegin()、crend()、emplace()、data())


前面提到的emplace_back就是C++11所增加的內容。

所以這篇博客就是想羅列一下C++11對vector容器的擴充。

std::vector::cbegin和std::vector::cend
這兩個方法是與std::vector::begin和std::vector::end相對應的,從字面就能看出來,多了一個’c’,顧名思義就是const的意思。
所以:
std::vector::cbegin: Returns a const_iterator pointing to the first element in the container.
std::vector::cend: Returns a const_iterator pointing to the past-the-end element in the container.

#include 
#include 

int main ()
{
  std::vector myvector = {10,20,30,40,50};

  std::cout << "myvector contains:";

  for (auto it = myvector.cbegin(); it != myvector.cend(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}
Output:
myvector contains: 10 20 30 40 50

std::vector::crbegin和std::vector::crend
這兩個方法就不解釋了,與上面的相比就是多了個’r’, reverse的縮寫,反轉迭代器,代碼就省略了。

std::vector::emplace
之前已經對emplace_back進行了討論,其實還有一個方法叫emplace。
我想說的就是,emplace之於emplace_back就像insert之於push_back。
看英文描述就直觀:

emplace:Construct and insert element
emplace_back:Construct and insert element at the end

如何使用:

#include 
#include 

int main ()
{
  std::vector myvector = {10,20,30};

  auto it = myvector.emplace ( myvector.begin()+1, 100 );
  myvector.emplace ( it, 200 );
  myvector.emplace ( myvector.end(), 300 );

  std::cout << "myvector contains:";
  for (auto& x: myvector)
    std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}
Output:
myvector contains: 10 200 100 20 30 300

std::vector::data
Returns a direct pointer to the memory array used internally by the vector to store its owned elements.

#include 
#include 

int main ()
{
  std::vector myvector (5);
  int* p = myvector.data();
  *p = 10;
  ++p;
  *p = 20;
  p[2] = 100;
  std::cout << "myvector contains:";
  for (unsigned i=0; i<myvector.size(); ++i)="" std::cout="" <<="" '="" myvector[i];="" '\n';="" return="" 0;="" }="" output:="" myvector="" contains:="" 10="" 20="" 0="" 100="" 0

std::vector::shrink_to_fit
Requests the container to reduce its capacity to fit its size.
就是減少空間

#include 
#include 
int main ()
{
  std::vector myvector (100);
  std::cout << "1. capacity of myvector: " << myvector.capacity() << '\n';
  std::cout << "1. size of myvector: " << myvector.size() << '\n';

  myvector.resize(10);
  std::cout << "2. capacity of myvector: " << myvector.capacity() << '\n';
  std::cout << "2. size of myvector: " << myvector.size() << '\n';

  myvector.shrink_to_fit();
  std::cout << "3. capacity of myvector: " << myvector.capacity() << '\n';
 std::cout << "3. size of myvector: " << myvector.size() << '\n';
  return 0;
}
//輸出
1. capacity of myvector: 100
1. size of myvector: 100
2. capacity of myvector: 100
2. size of myvector: 10
3. capacity of myvector: 10
3. size of myvector: 10

此時,就是要明白size和capacity的區別,也就會更加理解resize和reserve的區別了!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved