[英]c++ Using bubble sort to custom sort a vector
我陷入嘗試冒泡排序vector<vector<string>> vvs
如果運行像
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循環的自定義循環
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());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.