我需要连接 的多个实例boost::iterator_range。我最初的想法是使用boost::join,但看起来只需要两个范围作为参数。在另一个问题上,我发现投票最多的答案是“好吧,再次调用加入”,但这对我来说不起作用。我意识到原因可能是我在编译时不知道要加入的范围的确切数量,导致boost::join不知道其返回类型。为了更清楚地说明,我需要在范围 for 循环中使用它,如下所示:
SomeRangeType result;
for ( const auto& a_range : some_list_of_ranges )
{
result = boost::join( result, a_range );
}
return result;
Run Code Online (Sandbox Code Playgroud)
是否存在另一种类似于我需要的类似连接的操作boost?
range-v3有concat和join视图,看来你想在这里加入视图:
std::vector<std::vector<int>> v{{1, 2, 3}, {4}, {5, 6}};
for (auto e : v | ranges::view::join) {
std::cout << e << " "; // 1 2 3 4 5 6
}
Run Code Online (Sandbox Code Playgroud)
C++ does not have run-time dynamic templates, so the idea does not work with boost::range.
We can have 2 ranges r1 and r2 with types R1 and R2 and when we join those then we get result (lets name it r12) of type boost::joined_range<R1,R2>.
We can have third range (say r3 of type R3) and joining it with r12 we would get result of type boost::joined_range<boost::joined_range<R1,R2>,R3>.
The ranges r1, r2, r3 etc. can be passed to variadic template function as arguments and from list of arguments we can make compiler to figure what is the resulting type. Same is when we have the ranges in fixed length collection (like std::tuple or std::array), we can make compiler to figure what is the resulting type of joining those.
However if we don't have fixed amount of ranges but run-time dynamic amount (like from std::vector) then we don't know what will be the type of joining these and also compiler can't figure it out compile time. Every type however must be known compile time in C++.
因此,您要么找到一种方法来修复加入编译时的范围数量,要么必须使用动态的、未加入的vector范围集合(例如 )作为数据类型,或者必须使用第三个东西,例如 range-v3 。