Question

C++ Question! Write a function vector concatenateMyVectors(const vector& v, const vector& w) which returns the concatenation...

C++ Question!

Write a function

vector concatenateMyVectors(const vector& v, const vector& w)

which returns the concatenation of the two vectors.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please note that given method have error as vector required the type of date for creating vector

i have used template that can help to create any type of vector tested with int

save below code as concatvector.cpp or any name required ending with .cpp

i have also added main for testing if not required then skip the main function

#include <iostream>
#include <vector>

using namespace std;

// used template for generic type
template <typename Type>
vector<Type> concatenateMyVectors(const vector<Type> &v, const vector<Type> &w)
{

    vector<Type> out;
    // iterating first vector
     for (auto i = v.begin(); i != v.end(); ++i)
        out.push_back(*i) ;
    // iterating another vector
    for (auto i = w.begin(); i != w.end(); ++i)
        out.push_back(*i) ;
    // returning concanetaed vector
    return out;
}


// main for testing above function > start
int main(int argc, char const *argv[])
{
    vector<int> v, w, concat_v;
    v.push_back(2);
    v.push_back(4);
    
    w.push_back(45);
    cout<<"Concatenating vectors...."<<endl;
    concat_v = concatenateMyVectors(v, w);
    cout<<"Concatenated vectors...."<<endl;
    for (auto i = concat_v.begin(); i != concat_v.end(); ++i)
        cout << *i << " ";
    cout<<endl;
    return 0;
}
// main function end

// OUT

Concatenating vectors....
Concatenated vectors....
2 4 45

Add a comment
Know the answer?
Add Answer to:
C++ Question! Write a function vector concatenateMyVectors(const vector& v, const vector& w) which returns the concatenation...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT