C++ Vector iterator range constructor
Read the middle batch of an order queue using a range.
A simple example
Vector iterator range constructor
- Start with A, B, C
- Do this Select the first two items
- Result A, B
Syntax
batch(first, last)
Run these examples in C++
Save one example at a time as lesson.cpp. Use C++17 and a Unix-style shell.
c++ -std=c++17 -Wall -Wextra lesson.cpp -o lesson
./lessonSelect two orders for the packing bench
A packing bench takes a small batch from the middle of the day's queue. The supervisor wants orders B and C on this bench while the other orders stay visible in the full queue.
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<std::string> orders{"A", "B", "C", "D"};
std::vector<std::string> batch(orders.begin() + 1, orders.begin() + 3);
std::cout << batch[0] << ", " << batch[1];
return 0;
}
B, C
The requested range contains only orders B and C.
Prepare the next workshop roster
A teacher reads a workshop roster in arrival order. The coordinator prints the first two entries on a separate slip. Which entries appear, in order?
Work from the delivery round
A courier has three labelled stops in route order. The coordinator prints the first two entries on a separate slip. Which entries appear, in order?
What to watch for
The range is half-open and must be valid. This constructor copies the selected elements into a separate vector.