sorted vectorに対し、lower_boundで指定値以上、以下のデータを削除するC++のサンプルです。
覚えられないのでメモ。
#include <vector> int main(int argc, const char * argv[]) { auto description = [](std::vector<int> v){ for( auto value : v ){ printf( "%d\n", value ); } }; // 値2未満のデータを全て削除するので、1のデータが削除され2,3のデータが残る。 { printf( "cut off head\n" ); std::vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); auto it = lower_bound( v.begin(), v.end(), 2 ); v.erase( v.begin(), it ); description( v ); } // 値2以上のデータを全て削除するので、2,3のデータが削除され1のデータが残る。 { printf( "cut off tail\n" ); std::vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); auto it = lower_bound( v.begin(), v.end(), 2 ); v.erase( it, v.end() ); description( v ); } return 0; }
cut off head 2 3 cut off tail 1