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

1번째 줄: 1번째 줄:
{{다른뜻|벡터}}
==개요==
==개요==
;C++ vector
;C++ vector
6번째 줄: 7번째 줄:
#include <iostream>
#include <iostream>
#include <vector>
#include <vector>
using namespace std;
using namespace std;
int main() {
int main() {
vector<int> v = { 3, 1, 4, 8 };
    vector<int> v = { 3, 1, 4, 8 };
for (int n : v) {
    for (int n : v) cout << n << ' ';
    std::cout << n << '\n';
}
}
}
// 3
// 3 1 4 8
// 1
// 4
// 8
</source>
</source>
<source lang='cpp'>
<source lang='cpp'>
#include <iostream>
#include <iostream>
#include <vector>
#include <vector>
using namespace std;
using namespace std;
int main() {
int main() {
vector<int> v = { 3, 1, 4, 8 };
    vector<int> v = { 3, 1, 4, 8 };
v.push_back(5);
    v.push_back(25);
v.push_back(6);
    v.push_back(13);
for (int n : v) {
    for (int n : v) cout << n << ' ';
    std::cout << n << '\n';
}
}
}
// 3
// 3 1 4 8 25 13 
// 1
// 4
// 8
// 5
// 6
</source>
</source>
<source lang='cpp'>
<source lang='cpp'>
#include <iostream>
#include <iostream>
#include <vector>
#include <vector>
using namespace std;
using namespace std;
int main() {
int main() {
vector<int> v = { 3, 1, 4 };
vector<int> v = { 3, 1, 4 };

2019년 3월 16일 (토) 13:37 판

  다른 뜻에 대해서는 벡터 문서를 참조하십시오.

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 }}