Iterator and Algorithm Patterns
Iterator Positions
Iterators can locate a value and turn that position back into an index for reporting.
iterator
An iterator points at a position inside a container.
distance
Subtracting random-access iterators reports how far apart two positions are.
Iterator Positions
iterator_positions.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> scores{72, 85, 91, 64};
int target = 85;
auto found = std::find(scores.begin(), scores.end(), target);
int position = found == scores.end()
? -1
: static_cast<int>(std::distance(scores.begin(), found));
std::cout << "target=" << target << std::endl;
std::cout << "position=" << position << std::endl;
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> scores{72, 85, 91, 64};
int target = 64;
auto found = std::find(scores.begin(), scores.end(), target);
int position = found == scores.end()
? -1
: static_cast<int>(std::distance(scores.begin(), found));
std::cout << "target=" << target << std::endl;
std::cout << "position=" << position << std::endl;
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> scores{72, 85, 91, 64};
int target = 100;
auto found = std::find(scores.begin(), scores.end(), target);
int position = found == scores.end()
? -1
: static_cast<int>(std::distance(scores.begin(), found));
std::cout << "target=" << target << std::endl;
std::cout << "position=" << position << std::endl;
return 0;
}
scores ← [72, 85, 91, 64], target ← 85, found ← (empty), position ← 1
5int main() {6 std::vector<int> scores→ [72, 85, 91, 64]{72, 85, 91, 64};7 int target→ 85 = 85; //@target=64, 10089 auto found→ (empty) = std::find(scores[72, 85, 91, 64].begin(), scores.end(), target85);10 int position→ 1 = found(empty) == scores[72, 85, 91, 64].end()11 ? -112 : static_cast<int>(std::distance(scores[72, 85, 91, 64].begin(), found(empty)));1314 std::cout << "target=" << target85 << std::endl;15 std::cout << "position=" << position1 << std::endl;16 return 0;17}outputtarget=85 position=1
scores ← [72, 85, 91, 64], target ← 64, found ← (empty), position ← 3
5int main() {6 std::vector<int> scores→ [72, 85, 91, 64]{72, 85, 91, 64};7 int target→ 64 = 64;89 auto found→ (empty) = std::find(scores[72, 85, 91, 64].begin(), scores.end(), target64);10 int position→ 3 = found(empty) == scores[72, 85, 91, 64].end()11 ? -112 : static_cast<int>(std::distance(scores[72, 85, 91, 64].begin(), found(empty)));1314 std::cout << "target=" << target64 << std::endl;15 std::cout << "position=" << position3 << std::endl;16 return 0;17}outputtarget=64 position=3
scores ← [72, 85, 91, 64], target ← 100, found ← (empty), position ← -1
5int main() {6 std::vector<int> scores→ [72, 85, 91, 64]{72, 85, 91, 64};7 int target→ 100 = 100;89 auto found→ (empty) = std::find(scores[72, 85, 91, 64].begin(), scores.end(), target100);10 int position→ -1 = found(empty) == scores[72, 85, 91, 64].end()11 ? -112 : static_cast<int>(std::distance(scores[72, 85, 91, 64].begin(), found(empty)));1314 std::cout << "target=" << target100 << std::endl;15 std::cout << "position=" << position-1 << std::endl;16 return 0;17}outputtarget=100 position=-1