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
  • Python 1 Write a function called sum_odd that takes two parameters, then calculates and returns the...

    Python 1 Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers, if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. 3 To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use...

  • ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns...

    ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns a single, merged sorted list as a result. For example, if the two input lists are [0, 2, 4, 6, 8] and [1, 3, 5, 7, 9], the returned result should be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. Remember that you can add lists in Python using the + operator ([0] + [1,2,3] = [0,1,2,3]) and can take slices of...

  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...

  • Can you please write a program in cpp that implements the function vector<int> sumPairWise(vector<int> &v1, vector<int>...

    Can you please write a program in cpp that implements the function vector<int> sumPairWise(vector<int> &v1, vector<int> &v2) that returns a vector of integers whose elements are the pairwise sum of the elements from the two vectors passed as arguments. If a vector has a smaller size that the other, consider extra entries from the shorter vectors as 0. Example: vector<int> v1{1,2,3}; vector<int> v2{4,5}; sumPairWise(v1, v2); // returns [5, 7, 3]

  • Python Write a function (named largest_of_three) that takes three parameters and returns the largest. You can...

    Python Write a function (named largest_of_three) that takes three parameters and returns the largest. You can assume the parameters are either floats or ints.

  • 1. use python List to Dictionary Write a function that has three parameters: a list of...

    1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a start number, and an end number. This function should return a dictionary with all integers between the start and end number (inclusive) as the keys and their respective indices in the list as the value. If the integer is not in the list, the corresponding value would be None. Example unsorted list: [2,1,10,0,4,3] two numbers: 3, 10 returned...

  • C++, Create a function called larger which takes two integers as its parameters and returns the...

    C++, Create a function called larger which takes two integers as its parameters and returns the larger of the two integers

  • python Write a function called has_odd_even that takes three integers as parameters and that returns True...

    python Write a function called has_odd_even that takes three integers as parameters and that returns True if there is at least one odd and at least one even among the three numbers and that returns False otherwise. Below are some sample calls and the appropriate value to return. Function call Value returned has_odd_even(2, 4, 6) False has_odd_even(2, 3, 4) True has_odd_even(12, 4, 17) True has_odd_even(5, 17, 4) True has_odd_even(14, 7, 5) True has_odd_even(5, 4, 2) True has_odd_even(13, 20, 91) True...

  • Make a public class called ManyExamples and in the class... Write a function named isEven which...

    Make a public class called ManyExamples and in the class... Write a function named isEven which takes in a single int parameter and returns a boolean. This function should return true if the given int parameter is an even number and false if the parameter is odd. Write a function named close10 which takes two int parameters and returns an int. This function should return whichever parameter value is nearest to the value 10. It should return 0 in the...

  • c++ Write a function has Match that takes 3 parameters: 2 arrays of strings and an...

    c++ Write a function has Match that takes 3 parameters: 2 arrays of strings and an int that represents the length of each array The function returns true if at any position, the two arrays have the same string, false if not. For example: {"a", "b", "cat", "d"} and {"1", "2", "cat", "4"} would match

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