c++使用冒泡排序对向量进行自定义排序
c++ Using bubble sort to custom sort a vector
我一直在尝试对vector<vector<string>> vvs
进行冒泡排序如果运行类似的for循环
for ( auto x : vvs )
它包含一条类似的线路
if ( x.at(pos) == (*need next x position*).at(pos) {
//perform sort and swap if needed
}
是否可以获得基于范围的循环的下一个位置?
for (auto i : o)
std::cout << i << std::endl;
基于范围的for循环仅用于对数组或向量中的每个元素进行排序,用于传统的循环
for (unsigned i = 2; i != 10; ++i)
std::cout << arr[i] << std::endl;
不用矢量,只需使用std::list
,它比手动操作简单快捷得多,
std::list<std::string> vvs;
然后订购列表就像:一样简单
vvs.sort(compare_nocase);
按字母顺序对列表进行排序,不区分大小写;
或者,定义一个类似的基于迭代器的Bubble Sort
template <class Iterator>
inline void BubbleSort(Iterator begin, Iterator end) {
for (Iterator i = begin; i != end; ++i)
for (Iterator j = begin; j < i; ++j)
if (*i < *j)
std::iter_swap(i, j);
}
应用于矢量
for (auto & v : vvs) {
BubbleSort(v.begin(), v.end());
}
相关文章:
- 从具有自定义排序的向量创建集合
- 自定义排序函数中的堆溢出
- 基于3个对象的自定义排序
- 如何将标准::矢量插入具有自定义排序功能的 std::set 中
- 在类中使用自定义排序时编译错误
- 如何自定义排序 VCL 列表框?
- 如何将比较器添加到自定义排序函数
- 自定义排序实现会导致链接错误
- 在hadoop 2.7.4的自定义排序类中调用C函数,给出Java.lang.unsatisfiedlinkerror
- C++中的多键自定义排序
- 对字符串数组进行自定义排序
- 如何使用list::sort()对STL列表对象进行排序,同时将自定义排序函数作为参数传递给list::排序
- 调用自定义排序函数的次数
- 自定义排序和映射时,首选函数指针或函数对象
- 使用std::sort与自定义排序对象和数据类型模板-寻找正确的语法
- STL 集自定义排序函数在 Qunicy 2005 中有效,但在 MS Studio 2010 中不起作用
- STL 设置自定义排序
- 具有自定义排序的C++映射中的唯一索引
- 使用Functor在集合中生成自定义排序方法
- 我的结构的自定义排序