Untitled
unknown
c_cpp
3 years ago
877 B
3
Indexable
#include <iostream>
#include <vector>
#include <algorithm>
using std::cout;
using std::endl;
using std::find;
using std::vector;
#define PRINT(X) cout << (#X) << " = " << X << endl
int main() {
vector<int> v { 11, 22, 33, 44, 55, 66 };
// `result` is an "iterator" which is sort of like a pointer.
// v.begin() and v.end() return iterators too.
auto result = find(v.begin(), v.end(), 33);
// You can calculate the index from an iterator
int index = result - v.begin();
PRINT(index);
// You can also do more.
// `result + 1` gives you an iterator pointed to the next element
// We then derefence the iterator to get its pointed-to value.
PRINT( *(result + 1) );
// This does what you'd expect.
PRINT( *(result - 1) );
return 0;
}Editor is loading...