-4

I have this vector,

std::vector <std::vector<std::string> > gname;

gname.push_back(std::vector<std::string>({"John","jake"}));
gname.push_back(std::vector<std::string>({"May", "harold}));

and I want to put all the values from gname to,

std:vector<std::string> cname;

It this possible using c++11?

donkopotamus
  • 22,114
  • 2
  • 48
  • 60
domlao
  • 15,663
  • 34
  • 95
  • 134

2 Answers2

3

Here's a one liner. Nothing fancy, but it's readable...

for (auto& vec : gname) { cname.insert(cname.end(), vec.begin(), vec.end()); }
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
0

To complement Karolys fine one-liner. Here is one using <algorithm>

std::for_each(gname.begin(), gname.end(), 
        [&cname](const std::vector<std::string>& stuff){
    cname.insert(cname.end(), stuff.begin(), stuff.end());});

Debatable if it qualifies as a one-liner though.

Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67