Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge vector and initializer_list in initialization of vector<vector<T>>?

Say I am initialization a vector<vector<string>> like so:

vector<vector<string>> v;
v = {{
    {"a", "b", "c"},
    {"aa", "bb"},
    {"xyz", "yzx", "zxy"},
    {}
}};

Now suppose I want to append an already existing vector<string> to some of v's elements. Like so:

vector<string> suffix {{"1", "2", "3"}};
vector<vector<string>> v;
v = {{
    {"a", "b", "c"} + suffix,
    {"aa", "bb"},
    {"xyz", "yzx", "zxy"} + suffix,
    {}
}};

That syntax obviously doesn't work because operator+ is not defined in such a way.

I understand that it's possible to construct v the first way and then write

vector<int> indices = {0, 2};
for(int i: indices)
   v[i].insert(v[i].end(), suffix.begin(), suffix.end());

But this is not convenient because I may have several suffix vectors that are attached to arbitrary v[i]. I want the suffix to be together with the initialization of the v[i] so it makes sense and I don't have to shift indices if I add/remove elements from v's initialization.

like image 753
jcai Avatar asked Jun 08 '14 02:06

jcai


People also ask

How to initialize a vector in C++ class?

Initialize Vector in ConstructorPass a vector object v as a parameter to the constructor. Initialize vec = v. Declare a function show() to display the values of vector. print the all values of variable i.

What is std :: Initializer_list?

An object of type std::initializer_list<T> is a lightweight proxy object that provides access to an array of objects of type const T .

Are vectors initialized to zero C++?

The default value of a vector is 0. Syntax: // For declaring vector v1(size); // For Vector with default value 0 vector v1(5);


1 Answers

One possible solution is to use a helper function that does the appending.

vector<string> appendStrings(vector<string>&& s1, vector<string> const& s2)
{
   s1.insert(s1.end(), s2.begin(), s2.end());
   return s1;
}

And use it to initialize the variable.

vector<string> suffix {{"1", "2", "3"}};
vector<vector<string>> v = {{
    appendStrings({"a", "b", "c"}, suffix),
    {"aa", "bb"},
    appendStrings({"xyz", "yzx", "zxy"}, suffix),
    {}
}};

Update

A more efficient implementation of appendStrings (Thanks to @Yakk):

vector<string> appendStrings(initializer_list<char const*>&& s1,
                             vector<string> const& s2)
{
   vector<string> ret.
   ret.reserve(s1.size() + s2.size());
   for (auto item : s1 ) {
      ret.emplace_back(item);
   }
   ret.insert(ret.end(), s2.begin(), s2.end() );
   return ret;
}
like image 96
R Sahu Avatar answered Sep 29 '22 10:09

R Sahu