See the below code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
// Debug Output
void printVV(std::vector<std::vector<int>>& vv)
{
std::for_each(vv.begin(), vv.end(),
[](std::vector<int>& v) {
std::copy(v.begin(), v.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n'; });
std::cout << "\n\n";
}
int main()
{
// Initialized with Initializer List
std::vector<int> v00{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::vector<int> v10{ 10,11,12,13,14,15,16,17,18,19 };
std::vector<int> v20{ 20,21,22,23,24,25,26,27,28,29 };
std::vector<int> v30{ 30,31,32,33,34,35,36,37,38,39 };
std::vector<int> v40{ 40,41,42,43,44,45,46,47,48,49 };
// Fill vector
std::vector<std::vector<int>> v1{ 5,std::vector<int>(v00.begin() + 2,v00.begin() + 5) };
printVV(v1);
// Initializer List
std::vector<std::vector<int>> v2{ v00, v10,v20,v30, v40 };
printVV(v2);
// Range
std::vector<std::vector<int>> v3{ v2.begin() + 1, v2.begin() + 3 };
printVV(v3);
// Subrange init??? ????
//std::vector<std::vector<int>> v4{ }; ????
//printVV(v4);
return 0;
}
So, I can initialize a vector of vectors in various ways. All compliant to the given constructor signatures.
The question is now:
Can we also somehow use the range constructor for initialising the sub vectors also?
From the signature of the range constructor, it does not seem to be possible, because it expects 2 iterators. So I can initialize only the "outer" vector. What if I want to initialize also the inner vector.
Like for example: Build a vector of vector of int and initialize it with v00.begin()+2, v00.begin()+ 4, then the next row with v10.begin(), v10.begin()+ 7 and so on.
Is there any syntax that I am missing or does it simply not work, what I expect?
Aucun commentaire:
Enregistrer un commentaire