Untitled
unknown
c_cpp
3 years ago
816 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
PRINT(result - v.begin());
// 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.
//
// Expressions like `result - 1` and `result + 2` work as you expect too.
PRINT( *(result + 1) );
return 0;
}Editor is loading...