C++ 벡터

Jmnote (토론 | 기여)님의 2019년 3월 17일 (일) 19:39 판 (→‎같이 보기)
  다른 뜻에 대해서는 벡터 문서를 참조하십시오.

1 개요

C++ vector
C++ 벡터
#include <iostream>
#include <vector>
using namespace std;
int main() {
    vector<int> v = { 3, 1, 4, 8 };
    for (int n : v) cout << n << ' ';
}
// 3 1 4 8
#include <iostream>
#include <vector>
using namespace std;
int main() {
    vector<int> v = { 3, 1, 4, 8 };
    v.push_back(25);
    v.push_back(13);
    for (int n : v) cout << n << ' ';
}
// 3 1 4 8 25 13
#include <iostream>
#include <vector>
using namespace std;
int main() {
	vector<int> v = { 3, 1, 4 };

	cout << v[0] << endl; // 3
	cout << v[1] << endl; // 1
	cout << v[2] << endl; // 4

    // out of range ( No error )
	cout << v[-2] << endl; // 33
	cout << v[-1] << endl; // 0

    // out of range ( No error )
	cout << v[4] << endl; // 0
	cout << v[5] << endl; // 0
	cout << v[6] << endl; // 4113
	cout << v[7] << endl; // 0
}

2 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}