본문 바로가기
{Programing}/Data Structure

STL - Vector

by 탱타로케이 2021. 4. 27.

stl 자료구조의 종류 

c++ 11을 기준으로 array를 시작으로 vector, deque, list, stack, queue, priority_queue, set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map, unordered_multimap 이 있다.

 

예전부터 존재하던 vector,stack,queue,priority_queue,deque,list,map,multimap,set,multiset 먼저 정리한 후에 나머지

unordered_~~와 array를 정리하겠다.

 

stl 자료구조의 기본적인 함수 세트

삽입, 삭제, 길이 확인, 반복자용 시작점, 종료점.

이를 기본으로 하고 함수 세트들의 변종들을 자료구조에 맞게 추가해서 만들어 두었다.

 

자세한 변종들은 아래에서 확인가능하다. 하단부 표에 보면 각 자료구조별 멤버변수,함수들이 정리되어있다.

www.cplusplus.com/reference/stl/

 

Containers - C++ Reference

 

www.cplusplus.com

#include <iostream>
#include <vector>
int main () 
{
   // 이것은 int의 벡터입니다.
    std :: vector < int > vecOfInts;
    // 추가하는 동안 자동으로 크기 조정
    for ( int i = 0; i < 10; i ++ )
        vecOfInts. push_back ( i ) ;
    std :: vector < int > :: iterator it = vecOfInts. begin () ;
    while ( it! = vecOfInts. end ())
    {
        std :: cout << * it << "," ;
        it ++;
    }
    std :: cout << std :: endl;
    for ( int i = 0; i < vecOfInts. size () ; i ++ )
        std :: cout << vecOfInts [ i ] << "," ;
    std :: cout << std :: endl;
    return 0;
}

기본적인 컨셉은 배열과 같다.

템플릿 라이브러리 답게 자료형에 구애받지 않고 생성이 가능하다.

배열과 마찬가지로 [] 연산자를 이용해 인덱스를 통한 접근이 가능.

vector의 길이가 n 일때  인덱스는 0~n-1 이고, 

삽입/삭제 연산시 인덱스가 n-1일때 최대 성능을 보이고, 0~n-2일때는 성능이 떨어진다.

반복자를 통해 순회할수 있으며, 인덱스를 통한 순회도 가능하다.

삽입 삭제가 자유로우나 성능을 따지고 싶다면 벡터의 끝에서만 삽입삭제하는 것을 권장한다.

erase 함수는 인수의 개수에 따라 두가지 기능을 가진다.

erase( a ) 인수가 하나일경우 해당 위치의 원소를 지운다.

erase( b, c ) 두개일 경우 앞의 인자가 시작점. 뒤의 인자가 끝점인 범위내의 모든 원소를 삭제한다.

 

 

www.cplusplus.com/reference/vector/vector/

 

vector - C++ Reference

difference_typea signed integral type, identical to: iterator_traits ::difference_type usually the same as ptrdiff_t

www.cplusplus.com

 

 

참조) stl의 각종 팁.

thispointer.com//whats-stdvector-and-why-should-i-use-stdvector/

 

C++ std::vector example and why should I use std::vector? – thispointer.com

Vector is a template based container that behaves just like a Dynamic Array. It can expands its memory at run time and always store elements in contiguous memory location just like Array. We can store any type of element in vector by specifying the type as

thispointer.com

 

'{Programing} > Data Structure' 카테고리의 다른 글

STL - List  (0) 2021.04.29
STL - Array  (0) 2021.04.29
Minimum Spanning Tree, MST - 최소 신장 트리  (0) 2020.03.09
Graph  (0) 2020.03.05
Array  (0) 2020.01.28

댓글