I need explanation to my homework :/ I need to write a function in C++ which takes 3 arg ( initial and final iterator of a vector slice and int) and return const iterator of first found int(3th arg) in this vector. If this int is not in our vector, we should return const final vector iterator. This function use iterators instead of indexes and use only "vector header". This is example program
int main() {
const std::vector<int> vector {3, -1, 7, 12, -5, 7, 10};
std::cout << find(vector.begin() + 3, vector.end(), 7) - vector.begin() << std::endl; }
and this is my attempt to solve
#include <iostream>
#include <vector>
using namespace std;
vector<int>::const_iterator find(vector<int>::const_iterator, vector<int>::const_iterator, int);
int main() {
const std::vector<int> vector {3, -1, 7, 12, -5, 7, 10};
std::cout << find(vector.begin() + 3, vector.end(), 7) - vector.begin() << std::endl;
}
vector<int>::const_iterator find(vector<int>::const_iterator begin, vector<int>::const_iterator end, int num) {
for(vector<int>::iterator iterator = begin; iterator != end;) {
if (*iterator == num){
return iterator;
} else {
return end;
}
}
}
Aucun commentaire:
Enregistrer un commentaire