정의 |
- 벡터란 배열과 유사한 자료구조
- 동적 배열구조 클래스개념
- 마지막에 데이터 추가 및 삭제가 쉬움
- 배열과 같이 위치값(index)로 접근이 가능
벡터 사용법 |
#include <vector>
선언 후
vector <자료형> 변수이름;
- 10개의 숫자를 입력 받아서 출력하는 프로그램 예
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
int main()
{
vector <int> v;
int num;
for(int i=0;i<10;i++)
{
cin >> num;
v.push_back(num);
}
for(int i=0;i<v.size();i++)
{
cout << v[i] << " ";
}
return 0;
}
|
cs |
- 10개의 구조체 데이터를 입력 받아서 정렬하여 출력하는 프로그램 예
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
struct Point{
int x,y;
};
bool compare(Point l, Point r)
{
if(l.x<r.x) return true;
if(l.x==r.x) {
if(l.y < r.y) return true;
}
return false;
}
int main()
{
vector <Point> v;
int x,y;
for(int i=0;i<10;i++)
{
cin >> x >> y;
v.push_back( {x,y});
}
sort(v.begin(),v.end(),compare);
for(int i=0;i<v.size();i++)
{
cout << "(" << v[i].x << "," << v[i].y << ") ";
}
return 0;
}
|
cs |
- 반복자(iterator)를 사용한 출력
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
struct Point{
int x,y;
};
bool compare(Point l, Point r)
{
if(l.x<r.x) return true;
if(l.x==r.x) {
if(l.y < r.y) return true;
}
return false;
}
int main()
{
vector <Point> v;
int x,y;
for(int i=0;i<10;i++)
{
cin >> x >> y;
v.push_back( {x,y});
}
sort(v.begin(),v.end(),compare);
for(vector <Point>::iterator iter = v.begin();iter != v.end();iter++)
{
cout << "(" << (*iter).x << "," << (*iter).y << ") ";
}
return 0;
}
|
cs |
벡터 관련한 유형 문제 풀어 보러가기
http://jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=4058&sca=99&sfl=wr_subject&stx=vector
http://jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=4059&sca=99&sfl=wr_subject&stx=vector
https://www.acmicpc.net/problem/10817 ( 이 문제를 벡터에 세 수를 넣고 정렬하여 두번째 수 출력하는 형식으로 문제를 풀어 볼것)
https://www.acmicpc.net/problem/10814 ( 이 문제를 구조체 벡터를 이용하여 정렬하는 방식으로 풀어 볼것)
https://www.acmicpc.net/problem/2751 (이 문제를 벡터를 이용하여 풀어 볼것)
https://www.acmicpc.net/problem/11650 (벡터를 이용하여 풀어 볼것)
https://www.acmicpc.net/problem/11651 (벡터를 이용하여 풀어 볼것)
https://www.acmicpc.net/problem/10989(벡터를 이용하여 풀어 볼것)
'정보 > 자료구조' 카테고리의 다른 글
[자료구조] 셋(Set) (0) | 2021.03.07 |
---|---|
[자료구조] 덱큐(Dequeue) (0) | 2021.03.07 |
[자료구조] 큐(Queue) (0) | 2021.03.07 |
[자료구조] 스택(Stack) (0) | 2021.03.07 |
[자료구조] 링크드리스트(Linked List) (0) | 2021.03.07 |