먼저 Vector와 Iterator 를 사용하는 방법은 하기와 같다.
#if 1
#include <iostream>
#include <vector>
using namespace std;
int main(void){
vector <int> viTest(5, 0); // vector 5개 공간 선언 및 0으로 초기화 진행.
vector<int>::iterator viIter = viTest.begin();
for (viIter; viIter != viTest.end(); viIter++){
cout << *viIter << endl;
}
}
#endif
실행결과 :
0
0
0
0
0
Vector의 2차원 설정방법은 하기와 같다.
#if 0
#include <iostream>
#include <vector>
using namespace std;
int main(void){
// vector<vector <int> > vi; // 이런식으로 해도되지만
// 안쪽 괄호의 경우 >>형태로 입력되면 연산자로 인식이 되는 경우 && 가독성이 떨어지므로, 띄어쓰기를 쓰는것이 좋다.
vector < vector<int> > m_vi(5, vector <int>(5,0)); //이렇게 선언과 동시에 초기화를 진행하는 것을 추천한다.
// 위의 코드는 m_vi[5][5] = {0,}; 과 같은 코드이다.
int n, m = 0;
cin >> n >> m;
vector < vector<int> > inputArrayTest(n, vector<int>(m, 0)); // 이 코드는 inputArrayTest[n][m] = {0,} 과 같은 코드이다.
for (int a = 0; a < n; a++)
{
for (int b = 0; b < m; b++)
{
cin >> inputArrayTest[a][b];
}
}
// 위와 같은 방법으로 접근이 가능하다.
for (int a = 0; a < n; a++){
for (int b = 0; b < m; b++){
cout << inputArrayTest[a][b] << " ";
}
cout << endl;
}
}
#endif