Question

Can you write a function that takes 2 vectors as parameters and returns a larger vector...

Can you write a function that takes 2 vectors as parameters and returns a larger vector containing the two vectors

Example
Vector a: 1 2 3 4 5
Vector b: 6 7 8 9 10
Concatenated vector: 1 2 3 4 5 6 7 8 9 10

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

In C++

#include <vector>
#include <iostream>
#include <iterator>

// this is the function which concats two vector and prints result data
void concat(std::vector<int>& a, const std::vector<int>& b) {
a.insert(a.end(), b.begin(), b.end());
std::copy(
a.begin(),
a.end(),
std::ostream_iterator<int>(std::cout, " ")
);
}

int main(int argc, char** argv) {
std::vector<int> a{1,2,3,4,5};
std::vector<int> b{6,7,8,9,10};
concat(dest, src);

return 0;
}

In Java:

import java.util.Vector;

public class MergeVector {

static void concat(Vector<Integer> Va, Vector<Integer> Vb) {

Vector<Integer> merge = new Vector<Integer>();

merge.addAll(Va);

merge.addAll(Vb);

System.out.print("Concatenated Vector: ");

for(Integer in: merge) {

System.out.print(in +" ");

}

}

public static void main(String[] args) {

Vector<Integer> Va = new Vector<Integer>();

Vector<Integer> Vb = new Vector<Integer>();

Va.add(1);

Va.add(2);

Va.add(3);

Va.add(4);

Va.add(5);

Vb.add(6);

Vb.add(7);

Vb.add(8);

Vb.add(9);

Vb.add(10);

concat(Va, Vb);

}

}

Output:

Add a comment
Know the answer?
Add Answer to:
Can you write a function that takes 2 vectors as parameters and returns a larger vector...
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