1

My goal is to fill a vector v with 1,1,1,2,2,2 (the length of the sequence is variable) by appending together two temporary vectors. I know that this is possible, however I was wondering if there is a one-liner solution.

Here is the code I tried that did not work.

    int length = 3;
    vector<int> v(length, 1);//fill three 1s.
    v.insert(v.end(), vector(length, 2));//to fill three 2s, failed.
    for (size_t i = 0; i < v.size(); i++)
    {
        cout << v[i] << ", "; // I want 1 1 1 2 2 2
    }

Appending a vector to a vector

JRose
  • 1,382
  • 2
  • 5
  • 18
Zhang
  • 3,030
  • 2
  • 14
  • 31
  • Your question is confusing. Can you please post a [Minimal example](https://stackoverflow.com/help/minimal-reproducible-example) – WhiZTiM Aug 25 '22 at 05:50
  • 1
    Is your question: how can the answer from "Appending a vector to a vector" be done without creating a temporary `v2` variable? – JRose Aug 25 '22 at 05:51
  • @WhiZTiM, is `v.insert(v.end(), vector(3, 2));` a example? vector(3, 2) is the temporay vector. – Zhang Aug 25 '22 at 05:52
  • Why do you say it doesn't work? http://coliru.stacked-crooked.com/a/83a2c62871b618c3 – WhiZTiM Aug 25 '22 at 05:54
  • 1
    @WhiZTiM Perhaps the actual size and values are not known at compile-time? – Some programmer dude Aug 25 '22 at 05:56
  • @Zhang If the size is not known at compile time, you should emphasize it by using a *variable* as the size of your temporarily constructed vector. Like `vector(some_size, 2)`. – Some programmer dude Aug 25 '22 at 05:57
  • @Someprogrammerdude, yes, you are right. the count(3) is variable. – Zhang Aug 25 '22 at 05:57
  • As a workaround you could implement your own `template std::vector operator+(std::vector const& first, std::vector const& second)`, and use it like `v = v + vector(size, 2);` – Some programmer dude Aug 25 '22 at 05:58

1 Answers1

1

I do not think that there is an "out of the box" way to do what you want in C++, but you can create your own way of doing this in a one-liner with templates (as suggested by Some programmer dude ).

template<typename T> 
std::vector<T> operator+(std::vector<T> const& first, std::vector<T> const& second) {
    std::vector<T> new_vec;
    new_vec.insert(new_vec.end(), first.begin(), first.end() );
    new_vec.insert(new_vec.end(), second.begin(), second.end() );
    return new_vec;
}

int main()
{
    const int length = 3;
    std::vector<int> AB = std::vector<int>(length,1) + std::vector<int>(length,2);

    for (size_t i = 0; i < AB.size(); ++i)
        std::cout << AB[i] << " ";  // 1 1 1 2 2 2
    return 0;
}
JRose
  • 1,382
  • 2
  • 5
  • 18
  • 2
    The second vector could be passed by constant reference. The first could also be passed by constant reference if a new vector is created (and memory preallocated with `reserve`) in the function. – Some programmer dude Aug 25 '22 at 06:11
  • With the current code, you could begin by just changing the second argument to `std::vector const& second`. – Some programmer dude Aug 25 '22 at 06:22