• Mindscape 🔥
    • Playlist 🎧
  • 🤖 Artifical Intelligence

    • 1. Basics; Linear Algebra
    • 2. Basics; Linear Algebra (2), Search (1)
    • 3. Search (2)
    • 4. Knowledge and Logic (1)
    • 5. Knowledge and Logic (2)
    • 6. Probability
    • 7. Information Theory
    • 8. Probabilitc Reasoning (2)
    • 9. Probabilitc Reasoning (3)
    • 10. Machine Learning (1)
    • 11. Machine Learning (2)
    • 12. Machine Learning (3)
    • 13. Linear Models
    • 14. Other Classic ML Models (1)
    • 15. Other Classic ML Models (2)
  • 🔒 Computer Security

    • 01. Overview
    • 02. 정보보안정책 및 법규
    • 03. Cryptographic Tools
    • 04. User Authentication
    • 05. Access Control
    • 06. Database Security
    • 07. Malicious Software
    • 08. Firmware Analysis
  • 🗄️ Database System

    • 1. Introduction
    • 2. Relational Model
    • 3. SQL
    • 6. E-R Model
    • 7. Relational Database Design (1)
    • 7. Relational Database Design (2)
    • 13. Data Storage Structures
    • 14. Indexing
    • 15. Query Processing
  • 📝 Software Engineering

    • 2. Introduction to Software Engineering
    • 3. Process
    • 4. Process Models
    • 5. Agile
    • 6. Requirements
    • 7. Requirements Elicitation and Documentation
    • 8. Architecture
    • 9. Unified Modelling Language
    • 10. Object-Oriented Analysis
    • Object-Oriented Design
  • 🧠 Algorithm

    • Python 시간 초과 방지를 위한 팁
    • C++ std::vector 사용법 정리
    • Vim 사용 매뉴얼
    • 1018번: 체스판 다시 칠하기
    • 1966번: 프린터 큐

C++ std::vector 사용법 정리

vector

1. 기본 선언 및 초기화

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

int main() {
    vector<int> v1;                  // 빈 벡터
    vector<int> v2(5);               // 크기 5, 값은 0으로 초기화
    vector<int> v3(5, 10);           // 크기 5, 값은 10으로 초기화
    vector<int> v4 = {1, 2, 3, 4, 5}; // 초기화 리스트 사용
}

2. 원소 접근

v4[0];       // 인덱스 접근 (범위 체크 X)
v4.at(2);    // 인덱스 접근 (범위 체크 O, 예외 발생 가능)
v4.front();  // 첫 번째 원소
v4.back();   // 마지막 원소

3. 삽입 및 삭제

vector<int> v = {1, 2, 3};

v.push_back(4);        // 맨 뒤에 삽입 -> {1, 2, 3, 4}
v.pop_back();          // 맨 뒤 원소 삭제 -> {1, 2, 3}

v.insert(v.begin(), 0);         // 맨 앞에 삽입 -> {0, 1, 2, 3}
v.insert(v.begin() + 2, 99);    // 인덱스 2에 삽입 -> {0, 1, 99, 2, 3}

v.erase(v.begin());             // 첫 번째 원소 삭제 -> {1, 99, 2, 3}
v.erase(v.begin() + 1, v.end()); // 인덱스 1부터 끝까지 삭제 -> {1}

4. 크기 및 용량

v.size();       // 원소 개수
v.capacity();   // 현재 할당된 공간 크기
v.empty();      // 비었는지 확인
v.resize(10);   // 크기 조정 (부족분은 0으로 채움)
v.reserve(20);  // 용량을 미리 확보 (재할당 줄임)
v.shrink_to_fit(); // capacity를 size에 맞게 줄임

5. 반복자(iterator) 사용

for (auto it = v.begin(); it != v.end(); ++it)
    cout << *it << " ";

for (auto rit = v.rbegin(); rit != v.rend(); ++rit)
    cout << *rit << " "; // 역방향 순회

6. 범위 기반 for문

for (int x : v)
    cout << x << " ";

for (auto &x : v) // 참조로 접근하여 수정 가능
    x *= 2;

7. 기타 유용한 함수

v.clear();            // 모든 원소 제거
v.swap(v2);           // 다른 벡터와 교환
std::sort(v.begin(), v.end()); // 정렬
std::reverse(v.begin(), v.end()); // 뒤집기
std::find(v.begin(), v.end(), 3); // 특정 값 탐색

8. 2차원 벡터

vector<vector<int>> matrix(3, vector<int>(4, 0));
// 3x4 행렬, 모든 값 0으로 초기화

이 문법들은 std::vector의 알고리즘 문제 풀이에서 가장 자주 쓰이는 핵심 사용법입니다.

최근 수정: 25. 11. 6. 오후 12:07
Contributors: kmbzn
Prev
Python 시간 초과 방지를 위한 팁
Next
Vim 사용 매뉴얼

BUILT WITH

CloudflareNode.jsGitHubGitVue.jsJavaScriptVSCodenpm

All trademarks and logos are property of their respective owners.
© 2025 kmbzn · MIT License