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

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


<source lang='cpp'>
==int 벡터==
<syntaxhighlight lang='cpp' run>
#include <iostream>
#include <iostream>
#include <vector>
#include <vector>
10번째 줄: 11번째 줄:
int main() {
int main() {
     vector<int> v = { 3, 1, 4, 8 };
     vector<int> v = { 3, 1, 4, 8 };
     for (int n : v) cout << n << ' ';
     for(int el: v) {
        cout << el << ' '; // 3 1 4 8
    }
}
}
// 3 1 4 8
</syntaxhighlight>
</source>
<syntaxhighlight lang='cpp' run>
<source lang='cpp'>
#include <iostream>
#include <iostream>
#include <vector>
#include <vector>
22번째 줄: 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>
</source>
 
<source lang='cpp'>
==문자열 벡터==
<syntaxhighlight lang='cpp' run>
#include <iostream>
#include <vector>
using namespace std;
int main() {
    vector<string> fruits = { "apple", "banana", "lemon" };
    for (string s: fruits) cout << s << ' '; // apple banana lemon
}
</syntaxhighlight>
 
==원소 접근==
<syntaxhighlight lang='cpp' run>
#include <iostream>
#include <iostream>
#include <vector>
#include <vector>
37번째 줄: 53번째 줄:
cout << v[2] << endl; // 4
cout << v[2] << endl; // 4


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


     // out of range ( No error )
     // 범위를 벗어나면 엉뚱한 값이 나온다. (오류는 발생하지 않는다.)
cout << v[4] << endl; // 0
cout << v[4] << endl; // 0
cout << v[5] << endl; // 0
cout << v[5] << endl; // 0
47번째 줄: 63번째 줄:
cout << v[7] << endl; // 0
cout << v[7] << endl; // 0
}
}
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
* [[C++ 벡터 정렬]]
{{z컬럼3|
* [[C++ ]]
* [[C++ 배열]]
* [[C++ 배열]]
* [[C++ 리스트]]
* [[C++ 리스트]]
* [[C++ 페어 벡터]]
* [[C++ 벡터 정렬]]
* [[C++ 집합을 벡터로 변환]]
* [[C++ 벡터를 집합으로 변환]]
}}
==참고==
* https://en.cppreference.com/w/cpp/container/vector


[[분류: 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 }}