Vector Container: 두 판 사이의 차이

기술노트
편집 요약 없음
태그: 되돌려진 기여
(컴퓨터 과학 용어 정리 - Vector Container 추가)
태그: 수동 되돌리기
1번째 줄: 1번째 줄:
= C++ Vector 컨테이너 =
== [C++] Vector Container ==


Vector는 C++ STL에서 제공하는 동적 배열 컨테이너로, 자동으로 메모리를 할당하고 관리합니다. 데이터 타입을 템플릿 매개변수로 지정할 수 있으며, 스택과 유사한 push/pop 연산을 지원합니다.
<br>


== 기본 개념 ==
<syntaxhighlight>cpp
 
Vector는 연속된 메모리 공간에 원소를 저장하며, 크기가 동적으로 조절됩니다. 내부적으로는 다음 두 가지 중요한 속성을 관리합니다:
* '''size''': 실제로 저장된 원소의 개수
* '''capacity''': 메모리에 할당된 공간의 크기
 
Vector의 크기가 capacity를 초과하면 더 큰 메모리 공간을 할당하고 기존 요소를 복사하는 재할당(reallocation) 과정이 발생합니다.
 
== 헤더 파일 포함 ==
 
<syntaxhighlight lang="cpp">
#include <vector>
#include <vector>
</syntaxhighlight>
</syntaxhighlight>


== 선언 및 초기화 ==
자동으로 메모리를 할당해주는 Cpp 라이브러리
 
<syntaxhighlight lang="cpp">
// 빈 벡터 생성
vector<int> v1;
 
// 특정 값으로 초기화된 벡터 생성 (5개의 2로 초기화)
vector<int> v2(5, 2);  // {2, 2, 2, 2, 2}
 
// 다른 벡터의 복사본 생성
vector<int> v3(v2);
 
// 초기화 리스트 사용
vector<int> v4 = {1, 2, 3, 4, 5};
 
// 특정 범위의 요소로 초기화
vector<int> v5(v4.begin(), v4.begin() + 3);  // {1, 2, 3}
</syntaxhighlight>
 
== 주요 멤버 함수 ==
 
=== 요소 접근 ===
 
<syntaxhighlight lang="cpp">
// 인덱스로 접근 (범위 검사 없음, 빠름)
int elem1 = v[0];
 
// 안전한 접근 방법 (범위 검사 있음, 예외 발생 가능)
int elem2 = v.at(0);
 
// 첫 번째와 마지막 요소 접근
int first = v.front();
int last = v.back();
</syntaxhighlight>
 
=== 요소 추가 및 삭제 ===


<syntaxhighlight lang="cpp">
데이터 타입을 정할 수 있으며, push pop은 스택과 유사한 방식이다.
// 끝에 요소 추가
v.push_back(6);


// 끝에서 요소 제거
<br>
v.pop_back();


// 특정 위치에 요소 삽입 (iterator 필요)
=== 생성 ===
v.insert(v.begin() + 2, 10);  // 세 번째 위치에 10 삽입


// 특정 위치의 요소 삭제 (iterator 필요)
* `vector<"Type"> v;`
v.erase(v.begin() + 1); // 두 번째 요소 삭제
* `vector<"Type"> v2(v); ` : v2에 v 복사


// 모든 요소를 특정 값으로 할당
==== Function ====
v.assign(5, 2);  // {2, 2, 2, 2, 2}


// 모든 요소 제거 (capacity는 유지)
* `v.assign(5, 2);` : 2 값으로 5개 원소 할당
v.clear();
* `v.at(index);` : index번째 원소 참조 (범위 점검 o)
</syntaxhighlight>
* `v[index];` : index번째 원소 참조 (범위 점검 x)
* `v.front(); v.back();` : 첫번째와 마지막 원소 참조
* `v.clear();` : 모든 원소 제거 (메모리는 유지)
* `v.push_back(data); v.pop_back(data);` : 마지막 원소 뒤에 data 삽입, 마지막 원소 제거
* `v.begin(); v.end();` : 첫번째 원소, 마지막의 다음을 가리킴 (iterator 필요)
* `v.resize(n);` : n으로 크기 변경
* `v.size();` : vector 원소 개수 리턴
* `v.capacity();` : 할당된 공간 크기 리턴
* `v.empty();` : 비어있는 지 여부 확인 (true, false)


=== 크기 및 용량 관리 ===
<syntaxhighlight>
 
capacity : 할당된 메모리 크기
<syntaxhighlight lang="cpp">
size : 할당된 메모리 원소 개수
// 현재 요소 개수 확인
size_t size = v.size();
 
// 할당된 메모리 공간 크기 확인
size_t cap = v.capacity();
 
// 비어있는지 확인
bool isEmpty = v.empty();
 
// 크기 변경 (필요시 요소 추가 또는 제거)
v.resize(10);  // 크기를 10으로 변경
 
// 최소 용량 확보 (필요시에만 재할당)
v.reserve(100);  // 최소 100개 요소를 저장할 공간 확보
 
// 여분의 용량 제거
v.shrink_to_fit();
</syntaxhighlight>
</syntaxhighlight>


== 반복자(Iterator) 사용 ==
<br>


반복자는 컨테이너의 요소를 순회하는 데 사용됩니다.
<syntaxhighlight>cpp
 
#include<iostream>
<syntaxhighlight lang="cpp">
#include<vector>
// 기본 for 루프 예제
#include<string>
vector<int>::iterator iter;
for(iter = v.begin(); iter != v.end(); iter++) {
    cout << *iter << endl;
}
 
// 범위 기반 for 루프 (C++11 이상)
for(const auto& elem : v) {
    cout << elem << endl;
}
 
// 역방향 반복자
for(auto rit = v.rbegin(); rit != v.rend(); ++rit) {
    cout << *rit << endl;  // 역순으로 출력
}
</syntaxhighlight>
 
== 알고리즘과 함께 사용 ==
 
<code><algorithm></code> 헤더와 함께 사용하면 다양한 연산을 효율적으로 수행할 수 있습니다.
 
<syntaxhighlight lang="cpp">
#include <algorithm>
 
// 정렬
sort(v.begin(), v.end());
 
// 역순 정렬
sort(v.begin(), v.end(), greater<int>());
 
// 특정 값 찾기
auto it = find(v.begin(), v.end(), 3);
if(it != v.end()) {
    cout << "값 3을 찾았습니다! 위치: " << (it - v.begin()) << endl;
}
 
// 요소 개수 세기
int count_of_twos = count(v.begin(), v.end(), 2);
</syntaxhighlight>
 
== 성능 특성 ==
 
* 임의 접근: O(1)
* 끝에 요소 추가/제거: 상환 O(1)
* 중간에 요소 삽입/제거: O(n)
* 검색: O(n)
 
Vector는 임의 접근이 빈번하거나 끝에서의 삽입/삭제가 주로 필요한 경우 효율적인 자료구조입니다.
 
== 실제 사용 예제 ==
 
<syntaxhighlight lang="cpp">
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
using namespace std;


int main() {
int main(void) {
    // 문자열 vector 생성
     vector<int> v;
     vector<string> names = {"Kim", "Park", "Lee"};
   
    // 요소 추가
    names.push_back("Choi");
    names.push_back("Jung");
   
    // 정렬
    sort(names.begin(), names.end());
   
    // 출력
    cout << "이름 목록 (알파벳 순):" << endl;
    for(const auto& name : names) {
        cout << name << endl;
    }
      
      
     // 특정 요소 검색
     v.push_back(1);
    string search_name = "Park";
     v.push_back(2);
     auto it = find(names.begin(), names.end(), search_name);
    v.push_back(3);
      
      
     if(it != names.end()) {
     vector<int>::iterator iter;
        cout << search_name << "은(는) 목록의 "
    for(iter = v.begin(); iter != v.end(); iter++) {
            << (it - names.begin() + 1) << "번째에 있습니다." << endl;
         cout << *iter << endl;
    } else {
         cout << search_name << "은(는) 목록에 없습니다." << endl;
     }
     }
   
    return 0;
}
}
</syntaxhighlight>
</syntaxhighlight>


== 주의사항 ==
<br>


# Iterator Invalidation: vector의 크기를 변경하는 작업(push_back, resize 등)은 반복자를 무효화할 수 있습니다.
<br>
# 재할당: capacity를 초과하면 메모리 재할당이 발생하여 성능에 영향을 줄 수 있습니다. 필요한 경우 미리 <code>reserve()</code>를 호출하세요.
# 잘못된 접근: <code>[]</code> 연산자는 범위 검사를 하지 않으므로 주의해야 합니다. 안전한 접근이 필요하면 <code>at()</code> 메서드를 사용하세요.


== 참고 자료 ==
===== [참고 자료] =====
* [https://en.cppreference.com/w/cpp/container/vector C++ Reference]
* [https://blockdmask.tistory.com/70 원본 블로그]


[[분류:C++]]
* [링크](https://blockdmask.tistory.com/70)
[[분류:STL]]
[[분류:컨테이너]]

2025년 4월 21일 (월) 14:50 판

[C++] Vector Container


cpp
#include <vector>

자동으로 메모리를 할당해주는 Cpp 라이브러리

데이터 타입을 정할 수 있으며, push pop은 스택과 유사한 방식이다.


생성

  • `vector<"Type"> v;`
  • `vector<"Type"> v2(v); ` : v2에 v 복사

Function

  • `v.assign(5, 2);` : 2 값으로 5개 원소 할당
  • `v.at(index);` : index번째 원소 참조 (범위 점검 o)
  • `v[index];` : index번째 원소 참조 (범위 점검 x)
  • `v.front(); v.back();` : 첫번째와 마지막 원소 참조
  • `v.clear();` : 모든 원소 제거 (메모리는 유지)
  • `v.push_back(data); v.pop_back(data);` : 마지막 원소 뒤에 data 삽입, 마지막 원소 제거
  • `v.begin(); v.end();` : 첫번째 원소, 마지막의 다음을 가리킴 (iterator 필요)
  • `v.resize(n);` : n으로 크기 변경
  • `v.size();` : vector 원소 개수 리턴
  • `v.capacity();` : 할당된 공간 크기 리턴
  • `v.empty();` : 비어있는 지 여부 확인 (true, false)
capacity : 할당된 메모리 크기
size : 할당된 메모리 원소 개수


cpp
#include<iostream>
#include<vector>
#include<string>
using namespace std;

int main(void) {
    vector<int> v;
    
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    
    vector<int>::iterator iter;
    for(iter = v.begin(); iter != v.end(); iter++) {
        cout << *iter << endl;
    }
}



[참고 자료]