由于BOOST_FOREACH
适用于范围,因此您可以将向量拆分为您想要正常使用的范围(在BOOST_FOREACH
循环中)以及您想要特别对待的范围:
#include <stdio.h>
#include <vector>
#include "boost/foreach.hpp"
#include "boost/range.hpp"
using namespace std;
int main () {
vector<int> foo;
foo.push_back(1);
foo.push_back(2);
foo.push_back(3);
foo.push_back(4);
vector<int>::iterator special_item(foo.end() - 1);
boost::sub_range< vector<int> > normal_items(foo.begin(), special_item);
BOOST_FOREACH( int i, normal_items) {
printf( "%d ", i);
}
printf( "\nspecial item: %d\n", *special_item);
return 0;
}