1 개요[ | ]
- C++ vector
- C++ 벡터
- C++ 표준 라이브러리에서 제공하는 리스트 자료구조
2 int 벡터[ | ]
C++
Copy
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v = { 3, 1, 4, 8 };
for(int el: v) {
cout << el << ' '; // 3 1 4 8
}
}
Loading
C++
Copy
#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 el: v) {
cout << el << ' '; // 3 1 4 8 25 13
}
}
Loading
3 문자열 벡터[ | ]
C++
Copy
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<string> fruits = { "apple", "banana", "lemon" };
for (string s: fruits) cout << s << ' '; // apple banana lemon
}
Loading
4 원소 접근[ | ]
C++
Copy
#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
// 음수가 들어오면 엉뚱한 값이 나온다.(오류는 발생하지 않는다.)
cout << v[-2] << endl; // 33
cout << v[-1] << endl; // 0
// 범위를 벗어나면 엉뚱한 값이 나온다. (오류는 발생하지 않는다.)
cout << v[4] << endl; // 0
cout << v[5] << endl; // 0
cout << v[6] << endl; // 4113
cout << v[7] << endl; // 0
}
Loading
5 같이 보기[ | ]
6 참고[ | ]
편집자 Jmnote bot Jmnote
로그인하시면 댓글을 쓸 수 있습니다.