C++ 벡터 값 인덱스 역순찾기

1 개요[ | ]

C++ 벡터 값 위치 역순확인
C++ 벡터 값 인덱스 역순찾기
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int findIndexReverse(vector<int> v, int value) {
    return v.rend()-find(v.rbegin(), v.rend(), value)-1;
}

int main() {
    vector<int> v = {1, 2, 3, 2};
    cout << findIndexReverse(v, 1) << endl; // 0
    cout << findIndexReverse(v, 2) << endl; // 3
    cout << findIndexReverse(v, 9) << endl; // -1 (not exists)
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int findIndexReverse(vector<int> v, int value) {
    auto it = find(v.rbegin(), v.rend(), value);
    return v.rend()-it-1;
}

int main() {
    vector<int> v = {1, 2, 3, 2};
    cout << findIndexReverse(v, 1) << endl; // 0
    cout << findIndexReverse(v, 2) << endl; // 3
    cout << findIndexReverse(v, 9) << endl; // -1 (not exists)
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int findIndexReverse(vector<int> v, int value) {
    auto it = find(v.rbegin(), v.rend(), value);
    return distance(it, v.rend())-1;
}

int main() {
    vector<int> v = {1, 2, 3, 2};
    cout << findIndexReverse(v, 1) << endl; // 0
    cout << findIndexReverse(v, 2) << endl; // 3
    cout << findIndexReverse(v, 9) << endl; // -1 (not exists)
}

2 같이 보기[ | ]

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