c++ stl 已排序区间算法 合并连贯之排序区间inplace_merge使用方法
程序员文章站
2024-03-12 23:51:50
...
template<typename T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
for (int i = first; i <= last; ++i)
{
coll.insert(coll.end(), i);
}
}
template<typename T>
inline void PRINT_ELEMENTS(const T & coll, const string& optcstr = "")
{
cout << optcstr;
for (auto elem : coll)
{
cout << elem << ' ';
}
cout << endl;
}
int main()
{
list<int>a;
INSERT_ELEMENTS(a, 1, 7);
INSERT_ELEMENTS(a, 1, 8);
PRINT_ELEMENTS(a, "a: ");
auto pos = find(a.begin(), a.end(), 7);
++pos;
inplace_merge(a.begin(), pos, a.end());
PRINT_ELEMENTS(a, "a: ");
}