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

(새 문서: ==개요== ;C++ 벡터 find() <syntaxhighlight lang='cpp' run> #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v = {3, 1...)
 
 
(같은 사용자의 중간 판 9개는 보이지 않습니다)
2번째 줄: 2번째 줄:
;C++ 벡터 find()
;C++ 벡터 find()


==값 위치 확인==
{{참고|C++ 벡터 값 위치 확인}}
<syntaxhighlight lang='cpp' run>
#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)
}
</syntaxhighlight>
==값 있는지 확인==
{{참고|C++ 벡터 값 있는지 확인}}
<syntaxhighlight lang='cpp' run>
<syntaxhighlight lang='cpp' run>
#include <iostream>
#include <iostream>
9번째 줄: 31번째 줄:


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

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