我有一个vector <tuple<int a, int b, Vec4i c>>
,我已经根据a按顺序对元组进行了排序。结构是这样的。
vect = { 42,324,{}; //[0]
43,231,{};
45,97 ,{};
73,32 ,{}; //[1]
112,87,{};
114,249,{}; //[2]
}
我想比较&#34; a&#34;如果元素之间的差异小于5,则对它们进行分组。if(a[i+1]-a[i] >= 5
在每个组中,找到b的max元素,并且关联的c是push_back到新的向量。
元组通过以下方式实现:
vector<Vec4i> horiz;
vector<int> ly, lx;
using tuple_t = std::tuple<int, int, Vec4i>;
vector <tuple_t> vect;
int n = ly.size();
auto sort_A = [&](tuple_t lhs, tuple_t rhs)
{ return (get<0>(lhs) < get<0>(rhs)); };
for (int i = 0; i < n; i++)
vect.push_back(make_tuple(ly[i],lx[i],horiz[i]));
sort(vect.begin(), vect.end(), sort_A);
答案 0 :(得分:0)
在元组中,copy-list-initialization不起作用,你必须使用make_tuple。您可以使用std :: tie与元组关联。我无法完全理解你的问题,但我给你一个伪代码,用于在'vect'中找到最高元素(根据参数'b'),如下所示:
#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>
using namespace std;
struct Vec4i
{
int i1;
int i2;
int i3;
int i4;
Vec4i() { i1=i2=i3=i4 = 0;}
Vec4i(int i11,int i21,int i31,int i41) { i1=i11; i2=i21; i3=i31; i4 = i41;}
};
int main()
{
typedef tuple<int, int, Vec4i> foo_t;
vector <foo_t> vect;
vect.push_back( make_tuple( 42, 32,Vec4i(1,2,3,4)));
vect.push_back( make_tuple( 43,231,Vec4i(2,3,4,5)));
vect.push_back( make_tuple( 45, 97,Vec4i(3,4,5,6)));
vect.push_back( make_tuple( 73,324,Vec4i(4,5,6,7)));
vect.push_back( make_tuple(112, 87,Vec4i(5,6,7,8)));
vect.push_back( make_tuple(114,249,Vec4i(6,7,8,9)));
auto result = std::max_element(vect.begin(),vect.end(),
[](const foo_t& lhs,const foo_t& rhs)
{
int a1,b1,a2,b2;
Vec4i v1,v2;
tie(a1,b1,v1) = lhs;
tie(a2,b2,v2) = rhs;
return b1<b2;
} );
int a,b;
Vec4i v;
tie(a,b,v) = *result;
cout <<"a ="<< a << " "<<"b ="<< b << " "<<"i1 ="<< v.i1 << " "<<"i2 ="<< v.i2 << " "<<"i3 ="<< v.i3 << " "<<"i4 ="<< v.i4 <<endl;
}
结果是:
a =73 b =324 i1 =4 i2 =5 i3 =6 i4 =7
答案 1 :(得分:0)
您不需要复制到子向量中,只需要记录迭代器的范围。
#include <vector>
#include <tuple>
#include <algorithm>
#include <numeric>
struct Vec4i {};
int main()
{
using tuple_t = std::tuple<int, int, Vec4i>;
std::vector<tuple_t> tuples;
tuples.push_back(std::make_tuple( 42, 32,Vec4i{}));
tuples.push_back(std::make_tuple( 43,231,Vec4i{}));
tuples.push_back(std::make_tuple( 45, 97,Vec4i{}));
tuples.push_back(std::make_tuple( 73,324,Vec4i{}));
tuples.push_back(std::make_tuple(112, 87,Vec4i{}));
tuples.push_back(std::make_tuple(114,249,Vec4i{}));
// Finds the end of a group
auto next_group = [](const tuple_t & lhs, const tuple_t & rhs)
{ return (std::get<0>(rhs) - std::get<0>(lhs)) > 5; };
using iter_t = std::vector<tuple_t>::iterator;
// Collect up the groups as iterators
std::vector<std::pair<iter_t, iter_t>> iters;
for (iter_t it = tuples.begin(), next; it != tuples.end(); it = next)
{
// Original grouping, long runs are one group
next = std::adjacent_find(it, tuples.end(), next_group);
// only advance next if it is not end
next += (next != tuples.end());
iters.emplace_back(it, next);
// Variant grouping, compares to first element of group
next = it;
while(next != tuples.end() && !next_group(*it, *next))
{ ++next; }
iters.emplace_back(it, next);
}
auto compare_b = [](const tuple_t & lhs, const tuple_t & rhs)
{ return std::get<1>(lhs) < std::get<1>(rhs); };
// Operate on a pair of iter_t as a range, finding the maximum b
auto get_max_c = [&](std::pair<iter_t, iter_t> pair)
{ return std::get<2>(*std::max_element(pair.first, pair.second, compare_b)); };
std::vector<Vec4i> results;
std::transform(iters.begin(), iters.end(), std::back_inserter(results), get_max_c);
}