我有vector<vector<int> > Y
.我想将Y中的子向量(称为y)合并为一个vector<int>
.但我不想对它们进行排序,即按照它们出现的顺序合并它们.我怎么能有效地做到这一点,也许是通过使用STL算法?该std::merge
方法通过排序合并我不想要的.
编辑:我想要的是:给定{{1,6,5},{5,3-1,77},{0},...}返回{1,6,5,5,3,-1, 77,0,...}
seh*_*ehe 14
这个词是串联或扁平的:
std::vector<int> a { 1,2,3 },
b {9,0,-7};
std::vector<int> c(begin(a), end(a));
c.insert(end(c), begin(b), end(b));
Run Code Online (Sandbox Code Playgroud)
或者,更简单:
auto c = a;
c.insert(end(c), begin(b), end(b));
Run Code Online (Sandbox Code Playgroud)
c
现在包含1,2,3,9,0,-7
您可以将其概括为处理嵌套容器的情况:
template <template<typename...> class R=std::vector,
typename Top,
typename Sub = typename Top::value_type>
R<typename Sub::value_type> flatten(Top const& all)
{
using std::begin;
using std::end;
R<typename Sub::value_type> accum;
for(auto& sub : all)
accum.insert(end(accum), begin(sub), end(sub));
return accum;
}
Run Code Online (Sandbox Code Playgroud)
如果你想从第一容器元素移动到最后(如果元素仅是可移动的,或者复制昂贵)的使用std::move
与std::back_inserter
或适用std::make_move_operator
于每个源迭代器.
在Coliru上看到它
最初,我原本以为你会追求一种可变的解决方案:让我演示一下你如何能使它变得更加通用,所以你可以说:
auto x = to_vector(std::vector<int> { 1,2,3 }, std::list<int> { 9,8,11 }, std::set<int> { 42 });
Run Code Online (Sandbox Code Playgroud)
事实上,我把它变得如此通用,你将异构集合连接成"任意"容器:
// fun with maps:
auto y = concatenate<std::map<long, std::string> >(
std::map<int, const char*> { { 1, "one" }, { 2, "two" } },
std::map<unsigned, std::string> { { 1, "one" }, { 3, "three" } }
);
Run Code Online (Sandbox Code Playgroud)
你(正确地)期望这to_vector
只是一个方便的简写concatenate<std::vector<...>>
.这是完整的monty,看到它住在Coliru和ideone:
#include <vector>
#include <utility>
#include <iterator>
namespace detail
{
template <typename R>
void do_concatenation(R& accum) {}
template <typename R, typename First, typename... More>
void do_concatenation(R& accum, First const& first, More const&... more)
{
using std::begin;
using std::end;
std::copy(begin(first), end(first), std::inserter(accum, end(accum)));
do_concatenation(accum, more...);
}
}
template <typename Result, typename... Containers>
Result concatenate(Containers const&... containers)
{
Result accum;
detail::do_concatenation(accum, containers...);
return accum;
}
template <typename First, typename... More>
std::vector<typename First::value_type> to_vector(First const& first, More const&... containers)
{
return concatenate<std::vector<typename First::value_type>>(first, containers...);
}
/// demo
#include <set>
#include <list>
#include <iostream>
#include <map>
#include <string>
int main()
{
auto x = to_vector(std::vector<int> { 1,2,3 }, std::list<int> { 9,8,11 }, std::set<int> { 42 });
for (auto i : x)
std::cout << i << " ";
std::cout << std::endl;
// fun with maps:
auto y = concatenate<std::map<long, std::string> >(
std::map<int, const char*> { { 1, "one" }, { 2, "two" } },
std::map<unsigned, std::string> { { 1, "one" }, { 3, "three" } }
);
for (auto kvp : y)
std::cout << "(" << kvp.first << ", " << kvp.second << ")";
}
Run Code Online (Sandbox Code Playgroud)
输出:
1 2 3 9 8 11 42
(1, one)(2, two)(3, three)
Run Code Online (Sandbox Code Playgroud)
Seb*_*edl 11
template <typename T>
std::vector<T> flatten(const std::vector<std::vector<T>>& v) {
std::size_t total_size = 0;
for (const auto& sub : v)
total_size += sub.size(); // I wish there was a transform_accumulate
std::vector<T> result;
result.reserve(total_size);
for (const auto& sub : v)
result.insert(result.end(), sub.begin(), sub.end());
return result;
}
Run Code Online (Sandbox Code Playgroud)
与@ not-sehe的解决方案相比,这具有一些性能优势:
在缺点方面,它只适用于矢量,而不是deque或list.