-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
  • 20,509
  • 2
  • 42
  • 59
domlao
  • 15,145
  • 32
  • 90
  • 128

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
  • 91,854
  • 11
  • 113
  • 173
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
  • 13,890
  • 5
  • 38
  • 65