c++vector 상용 함수 예시 해석

2803 단어 c + +vector함수.
c++vector 상용 함수
Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.
vector 도 하나의 배열 이지 만 사용 하 는 메모리 크기 는 동적 으로 변 합 니 다.vector 가 사용 하 는 메모리 가 가득 차 면 메모 리 를 다시 할당 하고 원래 의 모든 요 소 를 할당 합 니 다.잦 은 메모리 재 할당 을 피하 기 위해 데 이 터 를 이전 합 니 다.vector 는 필요 한 메모리 보다 실제 분 배 된 메모리 가 많 습 니 다.예 를 들 어 10 개의 int 데이터 가 vector 에 있 습 니 다.vector 가 실제 사용 하 는 메모 리 는 20 개의 int 메모리 입 니 다.데이터 가 실제 사용 하 는 메모리 의 비율 을 초과 할 때 vector 는 자동 으로 메모 리 를 재배 치 하고 데 이 터 를 이전 합 니 다.vector 가 실제 사용 하 는 메모 리 는 capacity()로 볼 수 있 습 니 다.

#include<iostream>
#include<vector>
using namespace std;
int main(){
  vector<int> ans;
  for(int i=0; i<10; i++) ans.push_back(i);
  ans.erase(ans.begin()+2);
  cout<<"       :";
  for(int j=0; j<ans.size(); j++) cout<<ans[j]<<" ";
  ans.erase(ans.begin(), ans.begin()+2);
  cout<<endl<<"   2   :";
  for(int k=0; k<ans.size(); k++) cout<<ans[k]<<" ";
  //            ,         ,      
  ans.insert(ans.begin()+1, 100);
  cout<<endl<<"        100:";
  for(int m=0; m<ans.size(); m++) cout<<ans[m]<<" ";
  //vector      ,          
  vector<int> temp(5, -1);
  cout<<endl<<"temp    5,    -1:";
  for(int l=0; l<temp.size(); l++) cout<<temp[l]<<" ";
  //resize(int n)  vector         ,   n      ,       0,         
  temp.resize(8);
  cout<<endl<<" temp      8:";
  for(int h=0; h<temp.size(); h++) cout<<temp[h]<<" ";
  //   vector               ;                     
  temp.resize(10, 1111);
  cout<<endl<<"temp     10,            11111:";
  for(int g=0; g<temp.size(); g++)cout<<temp[g]<<" ";
  cout<<endl<<"  temp      :"<<temp.front()<<endl<<"  temp       :"<<temp.back();
  //  empty() size     vector    , vector     , empty()  true, size()   0
return 0;}
또한\#include라 이브 러 리 의 유 니 크 함수 에 맞 춰 vector 의 중복 요 소 를 삭제 할 수 있 습 니 다.

vector<int> ans;
ans.erase(unique(ans.begin(), ans.end()), ans.end());
c++vector 상용 함수 예제 해석 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 c+vector 상용 함수 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 도 많은 응원 바 랍 니 다!

좋은 웹페이지 즐겨찾기