"C++ 벡터 find()"의 두 판 사이의 차이

 
(같은 사용자의 중간 판 하나는 보이지 않습니다)
3번째 줄: 3번째 줄:


==값 위치 확인==
==값 위치 확인==
{{참고|C++ 벡터 값 위치 확인}}
<syntaxhighlight lang='cpp' run>
<syntaxhighlight lang='cpp' run>
#include <iostream>
#include <iostream>
22번째 줄: 23번째 줄:


==값 있는지 확인==
==값 있는지 확인==
{{참고|C++ 벡터 값 있는지 확인}}
<syntaxhighlight lang='cpp' run>
<syntaxhighlight lang='cpp' run>
#include <iostream>
#include <iostream>

2023년 9월 17일 (일) 14:55 기준 최신판

1 개요[ | ]

C++ 벡터 find()

2 값 위치 확인[ | ]

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int getIndex(vector<int> v, int value) {
    auto it = find(v.begin(), v.end(), value);
    return it==v.end() ? -1 : it-v.begin();
}

int main() {
    vector<int> v = {11, 1, 12, 2};
    cout << getIndex(v, 2) << endl; // 3
    cout << getIndex(v, 3) << endl; // -1 (not exists)
}

3 값 있는지 확인[ | ]

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<int> v = {11, 1, 12, 2};
    cout << (find(v.begin(), v.end(), 2) != v.end()) << endl; // 1 (exists)
    cout << (find(v.begin(), v.end(), 3) != v.end()) << endl; // 0 (not exists)
}

4 같이 보기[ | ]

5 참고[ | ]

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