久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進行排

Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進行排序)
本文介紹了使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進行排序的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想要做什么:我想對 2、3 或 N 個向量進行排序,鎖定在一起,不將它們復制到一個元組中.也就是說,把冗長放在一邊,比如:

What I want to do: I want to sort 2, or 3, or N vectors, locked together, without copying them into a tuple. That is, leaving verbosity aside, something like:

vector<int>    v1 = {  1,   2,   3,   4,   5};
vector<double> v2 = { 11,  22,  33,  44,  55};
vector<long>   v3 = {111, 222, 333, 444, 555};

typedef tuple<int&,double&,long&> tup_t;
sort(zip(v1,v2,v3),[](tup_t t1, tup_t t2){ return t1.get<0>() > t2.get<0>(); });

for(auto& t : zip(v1,v2,v3))
  cout << t.get<0>() << " " << t.get<1>() << " " << t.get<2>() << endl;

這應該輸出:

5 55 555
4 44 444
...
1 11 111

我現在的做法:我已經實現了我自己的快速排序,其中我傳遞的第一個數組用于比較,并且排列應用于所有其他數組.我只是不知道如何重用 std::sort 來解決我的問題(例如提取排列).

How I am doing it right now: I have implemented my own quicksort, where the first array I pass is used for the comparison, and the permutations are applied to all other arrays. I just couldn't figure out how to reuse std::sort for my problem (e.g. extract permutations).

我嘗試過的: boost::zip_iterator 和 boost::zip_range(帶有 boost::combine 范圍),但 std::sort 和 boost::range::algorithm::sort 抱怨迭代器/范圍是只讀的,而不是隨機訪問......

What I've tryed: boost::zip_iterator and boost::zip_range (with boost::combine range), but both std::sort and boost::range::algorithm::sort complain that the iterators/ranges are read only and not random access...

問題: 如何在鎖步(壓縮)中對 N 個向量進行排序?這個問題看起來非常普遍和常見,所以我想通過一個可能非常復雜的庫必須有一個簡單的解決方案,但我找不到它......

Question: How do I sort N vectors in lock step (zipped)? The problem looks pretty generic and common so I guess there must be an easy solution through a probably very complex library but I just can't find it...

備注: 是的,stackoverflow 中也有類似的問題,這個問題以不同的形式被問到很多.但是,它們總是以以下答案之一關閉:

Remarks: yes, there are similar questions in stackoverflow, this question gets asked a lot in different forms. However they are always closed with one of the following answers:

  • 將您的向量復制到一對/元組中并對該元組進行排序...
  • 將您的向量復制到一個結構中,每個向量有一個成員,并對結構向量進行排序...
  • 針對您的特定問題實現您自己的排序功能...
  • 使用輔助索引數組...
  • 使用 boost::zip_iterator 不帶示例或帶有產生不良結果的示例.

提示:

  • 我在 boost 郵件列表 指向 Anthony Williams 的這篇論文.雖然這似乎只適用于成對,但他們也討論了 TupleIteratorType,但我找不到它.
  • user673679 找到了 這篇 帖子包含一個很好的解決方案,適用于兩個容器的情況.它還確定了問題(重點是我的):
  • I've found this thread in the boost mailing list which points to this paper from Anthony Williams. Although this seems to only work for pairs, they also discus a TupleIteratorType but I haven't been able to find it.
  • user673679 found this post containing a nice solution for the two container case. It also nails down the problem (the emphasis is mine):

[...] 根本問題是對"數組引用的行為不像它們應該的那樣 [...] 我只是決定濫用迭代器的符號并編寫一些有效的東西.這實際上涉及編寫一個不符合規范的迭代器,其中值類型的引用與引用類型不同.

[...] the fundamental problem is that "pairs" of array references do not behave like they should [...] I simply decided to abuse the notation of an iterator and write something that works. This involved writing, effectively, a non-conforming iterator where the reference of the value type is not the same as the reference type.

答案:見下面 interjay 的評論(這也部分回答了未來的問題):

#include "tupleit.hh"
#include <vector>
#include <iostream>
#include <boost/range.hpp>
#include <boost/range/algorithm/sort.hpp>
#include <boost/range/algorithm/for_each.hpp>

template <typename... T>
auto zip(T&... containers)
    -> boost::iterator_range<decltype(iterators::makeTupleIterator(std::begin(containers)...))> {
  return boost::make_iterator_range(iterators::makeTupleIterator(std::begin(containers)...),
                                      iterators::makeTupleIterator(std::end(containers)...));
}

int main() {

  typedef boost::tuple<int&,double&,long&> tup_t;

  std::vector<int>    a = {   1,   2,   3,   4 };
  std::vector<double> b = {  11,  22,  33,  44 };
  std::vector<long>   c = { 111, 222, 333, 444 };

  auto print = [](tup_t t){ std::cout << t.get<0>() << " " << t.get<1>() << " " << t.get<2>() << std::endl; };

  boost::for_each( zip(a, b, c), print);

  boost::sort( zip(a, b, c), [](tup_t i, tup_t j){ return i.get<0>() > j.get<0>(); });

  for ( auto tup : zip(a, b, c) ) print(tup);

  return 0;
}

未來問題:上一個答案適用于序列容器.我們能否讓它也適用于 sortable 容器(例如序列和列表)?這將需要 random_access 和雙向 TupleIterator 以及適用于雙向迭代器的排序算法.

Future question: the previous answer works for sequence containers. Could we get it also to work on sortable containers (e.g. sequences and lists)? This would require random_access and bidirectional TupleIterators as well as a sort algorithm that works on bidirectional iterators.

更新:這適用于類似序列的容器的組合.但是,混合列表需要 std::sort 支持雙向迭代器(不支持).

Update: this works for combinations of sequence-like containers. However mixing a list would require that std::sort supported BidirectionalIterators (which does not).

推薦答案

這是一個基于 range-v3 的工作示例 已建議標準化的庫

Here's a working example based on the range-v3 Library that has been proposed for Standardization

#include <range/v3/all.hpp>
#include <iostream>

using namespace ranges;

int main() 
{
    std::vector<int> a1{15, 7, 3,  5};
    std::vector<int> a2{ 1, 2, 6, 21};
    sort(view::zip(a1, a2), std::less<>{}, &std::pair<int, int>::first); 
    std::cout << view::all(a1) << '
';
    std::cout << view::all(a2) << '
';
}

實時示例(需要具有良好 C++14 支持的最新編譯器,不是 VS 2015).

Live Example (requires recent compiler with good C++14 support, not VS 2015).

這篇關于使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進行排序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉置矩陣的最快方法是什么?)
Rotating a point about another point (2D)(圍繞另一個點旋轉一個點 (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識別的算法改進)
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構建 ISO 8601 日期時間?)
Sort list using STL sort function(使用 STL 排序功能對列表進行排序)
Is list::size() really O(n)?(list::size() 真的是 O(n) 嗎?)
主站蜘蛛池模板: 亚洲精品无 | 国产黄色在线观看 | 91免费版在线观看 | 午夜免费网站 | 欧美精品在线播放 | 成年女人免费v片 | 亚洲在线视频 | 亚洲精品久 | 午夜精品久久久 | 久久久高清 | 亚洲欧美日韩精品久久亚洲区 | 日韩精品一区二区三区久久 | 久久久久国产精品 | 超碰网址 | 中文字幕一区二区三区精彩视频 | 欧美成人h版在线观看 | 欧美日韩三级在线观看 | 午夜精品一区二区三区在线视频 | 91伊人网| 美女一级毛片 | 欧美一级视频免费看 | 国产精品久久久久久吹潮 | 日韩午夜在线播放 | av超碰 | japanhd成人 | 在线观看精品视频网站 | 一二三四在线视频观看社区 | 天天插天天干 | 日韩一区二区av | 中文字幕免费视频 | 中文字幕免费观看 | 91精品国产麻豆 | 超碰人人做 | 午夜欧美一区二区三区在线播放 | 99视频网 | 日韩在线欧美 | 亚洲黄色av网站 | 亚洲精品一区国产精品 | 精品视频在线一区 | 99精品国产一区二区三区 | 午夜www |