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

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

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

問(wèn)題描述

我想要做什么:我想對(duì) 2、3 或 N 個(gè)向量進(jìn)行排序,鎖定在一起,不將它們復(fù)制到一個(gè)元組中.也就是說(shuō),把冗長(zhǎng)放在一邊,比如:

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;

這應(yīng)該輸出:

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

我現(xiàn)在的做法:我已經(jīng)實(shí)現(xiàn)了我自己的快速排序,其中我傳遞的第一個(gè)數(shù)組用于比較,并且排列應(yīng)用于所有其他數(shù)組.我只是不知道如何重用 std::sort 來(lái)解決我的問(wèn)題(例如提取排列).

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).

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

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...

問(wèn)題: 如何在鎖步(壓縮)中對(duì) N 個(gè)向量進(jìn)行排序?這個(gè)問(wèn)題看起來(lái)非常普遍和常見(jiàn),所以我想通過(guò)一個(gè)可能非常復(fù)雜的庫(kù)必須有一個(gè)簡(jiǎ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 中也有類似的問(wèn)題,這個(gè)問(wèn)題以不同的形式被問(wèn)到很多.但是,它們總是以以下答案之一關(guān)閉:

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:

  • 將您的向量復(fù)制到一對(duì)/元組中并對(duì)該元組進(jìn)行排序...
  • 將您的向量復(fù)制到一個(gè)結(jié)構(gòu)中,每個(gè)向量有一個(gè)成員,并對(duì)結(jié)構(gòu)向量進(jìn)行排序...
  • 針對(duì)您的特定問(wèn)題實(shí)現(xiàn)您自己的排序功能...
  • 使用輔助索引數(shù)組...
  • 使用 boost::zip_iterator 不帶示例或帶有產(chǎn)生不良結(jié)果的示例.

提示:

  • 我在 boost 郵件列表 指向 Anthony Williams 的這篇論文.雖然這似乎只適用于成對(duì),但他們也討論了 TupleIteratorType,但我找不到它.
  • user673679 找到了 這篇 帖子包含一個(gè)很好的解決方案,適用于兩個(gè)容器的情況.它還確定了問(wèn)題(重點(diǎn)是我的):
  • 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):

[...] 根本問(wèn)題是對(duì)"數(shù)組引用的行為不像它們應(yīng)該的那樣 [...] 我只是決定濫用迭代器的符號(hào)并編寫(xiě)一些有效的東西.這實(shí)際上涉及編寫(xiě)一個(gè)不符合規(guī)范的迭代器,其中值類型的引用與引用類型不同.

[...] 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.

答案:見(jiàn)下面 interjay 的評(píng)論(這也部分回答了未來(lái)的問(wèn)題):

#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;
}

未來(lái)問(wèn)題:上一個(gè)答案適用于序列容器.我們能否讓它也適用于 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).

推薦答案

這是一個(gè)基于 range-v3 的工作示例 已建議標(biāo)準(zhǔn)化的庫(kù)

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) << '
';
}

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

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

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

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

相關(guān)文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉(zhuǎn)置矩陣的最快方法是什么?)
Rotating a point about another point (2D)(圍繞另一個(gè)點(diǎn)旋轉(zhuǎn)一個(gè)點(diǎn) (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識(shí)別的算法改進(jìn))
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構(gòu)建 ISO 8601 日期時(shí)間?)
Sort list using STL sort function(使用 STL 排序功能對(duì)列表進(jìn)行排序)
Is list::size() really O(n)?(list::size() 真的是 O(n) 嗎?)
主站蜘蛛池模板: 日日爱夜夜操 | 成人免费精品视频 | 欧美激情五月 | 精品成人av| 黄色91在线 | 亚洲精品久久久久久久久久久 | 成人在线h | 久久久国产精品 | 亚洲高清在线观看 | 精品一区二区三区在线视频 | 91九色视频在线 | 精品欧美乱码久久久久久 | 精品久久一区二区三区 | 欧美性video| 欧美高清性xxxxhdvideosex | 精品国产乱码久久久久久老虎 | 日韩日韩日韩日韩日韩日韩日韩 | 欧美成人a∨高清免费观看 老司机午夜性大片 | 日本精品视频在线观看 | 久久日韩精品 | 黄色网页在线观看 | 一级做a爰片性色毛片16 | 在线观看黄色电影 | 日本视频在线播放 | 国产美女黄色 | 91一区二区在线观看 | 黄色在线播放视频 | 欧美激情一区二区三区 | 亚洲综合视频 | 久久久精品一区 | 韩日在线 | 一区二区三区在线免费观看视频 | 久久伊人久久 | 久久在线看 | 亚洲男人的天堂网站 | 超碰8| 精品国产不卡一区二区三区 | 国产高清在线精品一区二区三区 | 欧美精品1区 | 国产精品伦一区二区三级视频 | 精品国产一区二区久久 |