I have a rather plain/obvious iterator solution going, but thought I'd tap SO to see if someone comes up with one of the occasional amazing recipes that crop up in answers around here :)
The situation is the following:
vector<vector<int>>
, can't work around this due to a mix of API reqs and number of vects being only known at run-time As I'm currently bound between two APIs with strict binary requirements this is GCC 4.1.x limited, so an absolute and maddening lack of any C++0x, Boost 1.44 available though.
At present time these vects all contain unique indices, but in the future producing a single filtered array with duplicates removed (future uses might involve feeds with overlapping indices) might also become a requirement, so bonus points if that's handled.
C++11 solutions or anything related still welcome. I'm not looking for someone to do my homework, I have a clunky but working piece out anyway, I'm more after enlightenment and cookbook inspiration than anything.
Thanks in advance
It's not 100% clear what you want, but making the most of what you've said, I would do something like this:
std::vector<std::vector<int>> v; // input, assume it's already filled
std::size_t size = 0;
for (std::vector<std::vector<int>>::const_iterator i = v.begin(); i != v.end(); ++i)
{
size += i->size();
}
boost::scoped_array<int> array;
if (size != 0)
{
array.reset(new int[size]);
std::size_t offset = 0;
for (std::vector<std::vector<int>>::const_iterator i = v.begin(); i != v.end(); ++i)
{
std::copy_n(&(*i)[0], i->size(), array + offset);
offset += i->size();
}
}
// ...use array...
If you want to make array
unique, you could do:
std::sort(array.get(), array.get() + size);
std::size_t newSize = std::unique(array.get(), array.get() + size) - array.get();
// Now array is unique, assuming you only use elements [0, newSize)
There are probably more efficient ways to sort and make unique (perhaps sort each sub-vector, and then do a merge-sort style operation when copying them to the new array (and simply not copy existing items)), but I'm aiming for simple+correct in my answer. Optimizations can come later once you have a known correct solution.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.