How to initialize vector in C++ STL (Standard Template Library), STL 벡터 초기화 방법
제일 간단한 3가지만
Simplest way to initialize vector in c++
1 - 벡터를 만들면서 즉시 값을 넣어서 초기화
2 - 벡터를 만들어두고 넣고 싶은 값을 넣어서 초기화
3 - 넣을 배열이 있다면, 벡터를 만들며 배열을 옮겨 넣으며 초기화
//
1 - Initialize when you make vector
2 - Initialize after making vector
3 - Initialize when you make vector using array you made before it
int main(int argc, char*(*argv)) {
//1
vector<int> testVector1 = { 1, 2, 3, 4, 5 };
for (int i = 0; i < testVector1.size(); ++i)
{
cout << testVector1.at(i) << " ";
}
cout << endl;
for (int i = 0; i < testVector1.size(); ++i)
{
cout << testVector1.at(i) << " ";
}
cout << endl;
//2
vector<int> testVector2;
for (int i = 0; i < 5; ++i)
{
testVector2.push_back(i);
}
for (int i = 0; i < testVector2.size(); ++i)
{
cout << testVector2.at(i) << " ";
}
cout << endl;
//3
int intArray[] = { 10,20,30,40,50 };
vector<int> testVector3(intArray, intArray + sizeof(intArray) / sizeof(intArray[0]));
for (int i = 0; i < testVector3.size(); ++i)
{
cout << testVector3.at(i) << " ";
}
cout << endl;
}
Something else you might like...?
2018/11/22 - [Programming/Java] - Java foreach statement (advanced for iteration statement) 자바 향상된 for문 문법
2018/11/22 - [Programming/Image Processing] - OpenCv, How to create/initialize mat in CV for Java, 매트릭스 생성/초기화, 자바, c++, JavaCv
2018/11/22 - [Programming/C++] - Visual Studio console closing problem after running main function 비주얼스튜디오 메인 실행 후 콘솔 꺼지는 문제
2018/11/18 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #30 circleOfNumbers. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/11/17 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #29 chessBoardCellColor. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/11/15 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #28 alphabeticShift. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/11/15 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #27 variableName. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제, example, c++ java c# scalar
2018/11/16 - [Programming/Image Processing] - OpenCV Java/C++ FourierFastTransferShift(fftshift) 푸리에 변환 후 위치 재설정 함수 (Matlab - fftshift) - (2)