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

 
(같은 사용자의 중간 판 7개는 보이지 않습니다)
1번째 줄: 1번째 줄:
{{다른뜻|벡터}}
==개요==
==개요==
;C++ vector
;C++ vector
;C++ 벡터
;C++ 벡터
* C++ 표준 라이브러리에서 제공하는 리스트 자료구조


{{소스헤더|int 벡터}}
==int 벡터==
<syntaxhighlight lang='cpp'>
<syntaxhighlight lang='cpp' run>
#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
    }
}
</syntaxhighlight>
<syntaxhighlight lang='cpp' run>
#include <iostream>
#include <iostream>
#include <vector>
#include <vector>
13번째 줄: 24번째 줄:
     v.push_back(25);
     v.push_back(25);
     v.push_back(13);
     v.push_back(13);
     for (int n : v) cout << n << ' ';
     for(int el: v) {
        cout << el << ' '; // 3 1 4 8 25 13
    }
}
}
// 3 1 4 8 25 13 
</syntaxhighlight>
</syntaxhighlight>
{{소스헤더|문자열 벡터}}
 
<syntaxhighlight lang='cpp'>
==문자열 벡터==
<syntaxhighlight lang='cpp' run>
#include <iostream>
#include <iostream>
#include <vector>
#include <vector>
24번째 줄: 37번째 줄:
int main() {
int main() {
     vector<string> fruits = { "apple", "banana", "lemon" };
     vector<string> fruits = { "apple", "banana", "lemon" };
     for (string s: fruits) cout << s << ' ';
     for (string s: fruits) cout << s << ' '; // apple banana lemon
}
}
// apple banana lemon
</syntaxhighlight>
</syntaxhighlight>


{{소스헤더|원소 접근}}
==원소 접근==
<syntaxhighlight lang='cpp'>
<syntaxhighlight lang='cpp' run>
#include <iostream>
#include <iostream>
#include <vector>
#include <vector>
41번째 줄: 53번째 줄:
cout << v[2] << endl; // 4
cout << v[2] << endl; // 4


     //  음수가 들어오면 엉뚱한 값이 나옴 (오류는 발생하지 않음)
     //  음수가 들어오면 엉뚱한 값이 나온다.(오류는 발생하지 않는다.)
cout << v[-2] << endl; // 33
cout << v[-2] << endl; // 33
cout << v[-1] << endl; // 0
cout << v[-1] << endl; // 0


     //  범위를 벗어나면 엉뚱한 값이 나옴 (오류는 발생하지 않음)
     //  범위를 벗어나면 엉뚱한 값이 나온다. (오류는 발생하지 않는다.)
cout << v[4] << endl; // 0
cout << v[4] << endl; // 0
cout << v[5] << endl; // 0
cout << v[5] << endl; // 0
54번째 줄: 66번째 줄:


==같이 보기==
==같이 보기==
{{z컬럼3|
* [[C++ 맵]]
* [[C++ 맵]]
* [[C++ 배열]]
* [[C++ 배열]]
* [[C++ 리스트]]
* [[C++ 리스트]]
* [[C++ 페어 벡터]]
* [[C++ 벡터 정렬]]
* [[C++ 벡터 정렬]]
* [[C++ 집합을 벡터로 변환]]
* [[C++ 벡터를 집합으로 변환]]
}}


==참고==
==참고==

2023년 12월 20일 (수) 20:16 기준 최신판

1 개요[ | ]

C++ vector
C++ 벡터
  • C++ 표준 라이브러리에서 제공하는 리스트 자료구조

2 int 벡터[ | ]

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

3 문자열 벡터[ | ]

#include <iostream>
#include <vector>
using namespace std;
int main() {
    vector<string> fruits = { "apple", "banana", "lemon" };
    for (string s: fruits) cout << s << ' '; // apple banana lemon 
}

4 원소 접근[ | ]

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

5 같이 보기[ | ]

6 참고[ | ]

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