"C++ 벡터"의 두 판 사이의 차이

40번째 줄: 40번째 줄:
// 5
// 5
// 6
// 6
</source>
<source lang='cpp'>
#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
}
</source>
</source>



2018년 4월 14일 (토) 17:32 판

1 개요

C++ vector
C++ 벡터
#include <iostream>
#include <vector>

using namespace std;

int main() {
	vector<int> v = { 3, 1, 4, 8 };
	for (int n : v) {
	    std::cout << n << '\n';
	}
}
// 3
// 1
// 4
// 8
#include <iostream>
#include <vector>

using namespace std;

int main() {
	vector<int> v = { 3, 1, 4, 8 };
	v.push_back(5);
	v.push_back(6);
	for (int n : v) {
	    std::cout << n << '\n';
	}
}
// 3
// 1
// 4
// 8
// 5
// 6
#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 }}