How to initialize and return vector of vectors more compactly? The code below seems bloated and repetitious for the two cases when K==1
and K==N
.
std::vector< std::vector<unsigned int> > Foobar(const unsigned int K, const unsigned int N)
{
std::vector< std::vector<unsigned int> > res;
if (K == 1)
{
std::vector<unsigned int> r(1,N); //One N.
res.emplace_back(r);
return res;
}
if (K == N)
{
std::vector<unsigned int> r(N,1); //N ones.
res.emplace_back(r);
return res;
}
PopulateVectors(res, K+1, N+1);
PopulateVectors(res, K, N);
return res;
}
The function PopulateVectors()
accepts res
as a reference and inserts multiple vectors into it. The way it works internally is immaterial to this question.
if (K == 1)
{
return {{N}};
}
if (K == N)
{
return {std::vector<unsigned int>(N,1)};
}
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.