[C++][STL]벡터(vector) 구현

jh Seo·2022년 6월 12일
1

자료구조 구현

목록 보기
1/6

개요

자료구조를 공부하면서
제일 좋은 방법은 직접 구현해보는 것이라고
생각이 들어 벡터를 구현해봤습니다.

늘 push_back(), pop_back(), front(), back(), size()
정도만 알고 썼었지만, 메소드들을 구현하면서
자세히 몰랐던 STL 벡터의 내장함수들을
알게 되었습니다.

특히, resize와 reserve에 대해 공부하게 되었던 좋은 경험이였습니다.

알게 된 것들

  • resize(size_t n,value_type val=value_type())
    벡터의 size가 n보다 크면 size를 n으로 변환하고 벡터의 인덱스가 size를 넘어가는 원소들은 다 없앤다.
    만약 벡터의 size가 n보다 작다면 capacity를 n으로 변환 후 val값으로 모든 원소를 초기화 한다.

  • reserve(size_t n)
    벡터의 size가 n보다 크다면 아무 일도 안 일어나고
    벡터의 size가 n보다 작다면 capacity를 n으로 재조정한다.
    resize와 다르게 벡터의 size나 원소를 건드리지 않는다.

코드

#include<iostream>

using namespace std;

template <typename T>

class vector {											//벡터 구현
private:
	T * vec;
	int v_size;
	int v_capacity;
public:
	vector(const int& temp=10) {
		vec = new T[temp];
		v_size = 0;
		v_capacity = temp;
	}
	~vector() {
		delete[] vec;
	}
	vector& operator=(const vector& other) {			//=연산자일때 처리
		if (this != other) {							//이 벡터가 다른 벡터가 아닐 때
			if (v_capacity < other.v_capacity) {			//그리고 캐패시티도 다를 때
				delete[] vec;
				vec(other.v_capacity);
			}
		}
		v_size = other.v_size;
		for (int i = 0; i < other.v_size; i++) {
			vec[i] = other[i];
		}
	}
	T& operator[](int idx) {						 // []연산자일때 처리
		return vec[idx];
	}
	bool operator==(vector& other) {				//비교연산자 == 처리
		if (capacity != other.capacity)
			return false;
		for (int i = 0; i < v_size; i++) {
			if (vec[i] != other[i])
				return false;
		}
		return true;
	}
	bool operator!=(vector& other) {				//비교연산자 !=처리
		return (!(*this == other));
	}
	int size() {										//vector.size()
		return v_size;
	}
	int capacity() {									//vector.capacty()
		return v_capacity;
	}
	T& front() {									//vector.front() 맨 앞 원소 위치
		return vec[0];
	}
	T& back() {										//vector.back()
		return vec[v_size-1];
	}
	T* begin() {									//vector.begin()
		return vec;
	}
	T* end() {										//vector.end()
		return vec + v_size;
	}
	void push_back(const T& element) {				//vector.push_back()
		if (v_capacity <= v_size) {						//capacity가 size보다 작거나 클때 작업
			v_capacity *= 2;							//그냥 일단 2배곱해주는거로 설정
			T* tempArr = new T[v_capacity];
			for (int i = 0; i < v_size; i++) {
				tempArr[i] = vec[i];
			}
			delete[] vec;
			vec = tempArr;

		}
		vec[v_size++] = element;
		
	}
	void pop_back() {
		v_size = v_size > 0 ? v_size-1 : 0;				//사이즈만 줄이면 어차피 push때 vec[size]값에 넣으므로 
	}
	void resize(int n,T val=0){						//vector.resize()
		T* arr = new T[n];
		v_size = v_size > n ? n : v_size;
		v_capacity = n;
		for (int i = 0; i < v_size; i++) {			//arr배열에 vec배열값 복사해주고
			arr[i] = vec[i];
		}
		for (int i = v_size; i < v_capacity; i++) {		//만약 capacity가 size보다 더 커졌다면 커진만큼 추가된 원소들을 val값으로 초기화 시켜준다.
			arr[i] = val;
		}
		delete[] vec;
		vec = arr;
		v_size = n;
	}
	void reserve(int n) {
		if (n < v_capacity) return;
		T* arr = new T[n];
		for (int i = 0; i < v_size; i++) {
			arr[i] = vec[i];
		}
		delete[] vec;
		vec = arr;
		v_capacity = n;
	}
    void swap(vector& other) {
		swap(vec, other.vec);
		swap(v_size, other.v_size);
		swap(v_capacity,other.v_capacity);
	}

	
};
profile
코딩 창고!

1개의 댓글

comment-user-thumbnail
2022년 6월 20일

코창이 화이팅>_<
코딍끝나구 맨끝에 ; 땀표시를 잊지마!!!!!
땀표시 없으면 다 틀린대!!! 아주짧은지식으로 나도 알구있다. 키키
흐유.. 코딩하는사람들은 먼가 좀 띠껍게(?) 글을 마무리하는 습관이 있나봐.
끝날때마다 땀표실 쓰니. 흐유.. 😨

답글 달기