카테고리 없음

[언어 공부] <c++> vector

Lord DEVader 2023. 1. 19. 14:08
  • 개요
    • std::vector is a sequence container that encapsulates dynamic size arrays.
  • 특징
    • 배열처럼 연속적으로 저장됨.
    • 그 크기는 자동으로 관리됨.(확장 가능)
  • 배열(array)과 비교
    • vector는 크기를 자유롭게 늘릴수 있어, 배열에 비해 유연
  • 사용
    • 라이브러리
      • #include <vector>
    • 선언
      • vector <int> n_a_m_e(n)
        • n : 벡터 공간의 크기
  • 예제
#include <iostream>
#include <vector>

using namespace std;

int main()
{
	vector<int> list(6);	//벡터 선언. 파라미터는 공간 크기.
	vector<int> horse(6);

	horse[0] = 1;
	horse[1] = 1;
	horse[2] = 2;
	horse[3] = 2;
	horse[4] = 2;
	horse[5] = 8;

	for (int i=0; i<6; i++)
		cin >> list[i];

	for (int i=0; i<6; i++)
	{
		cout << horse[i] - list[i] << " ";
	}
}
// BoJ3003