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<int> ans;
ans.erase(unique(ans.begin(), ans.end()), ans.end());
c++vector 상용 함수 예제 해석 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 c+vector 상용 함수 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 도 많은 응원 바 랍 니 다!이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
HDU 원활 공사 & & 원활 공사 (차 트 법)모 성 은 도시 의 교통 상황 을 조사 하여 기 존의 도시 도로 통계 표를 얻 었 고 표 에는 모든 도로 가 직접 연 결 된 도시 가 열거 되 어 있다.성 정부의 '원활 한 공사' 목 표 는 성 전체의 어느 두 도시...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.