Question

In: Computer Science

C++ Write a toVector function that takes in a C-style pointer of any type and a...

C++ Write a toVector function that takes in a C-style pointer of any type and a size (int) and returns a vector of the same size containing a copy of the elements in the input array using a template. You may assume that the array elements have a valid copy constructor. Please explain every line of code to me

Solutions

Expert Solution

Here is the code:

# include <iostream>
# include <vector>
using namespace std;

//define a template function which takes any type T
template <typename T>
//the return value is a vector containing elements of type T
//the argument is a pointer to elements of type T, and n
vector<T> toVector(T* a, int n) {
//define a vector
vector<T> v;
//iterate over the array in the pointer
for (int i=0; i<n; i++) {
//add i to get the pointer to ith element
//then dereference to get the value
v.push_back(*(a+i));
}
//return the vector
return v;
}

int main() {
//this is for testing
//define an int array and char array
int* intArray = new int[3];
//initialize some values
*intArray = 4;
*(intArray+1) = 2;
*(intArray+2) = 0;
char* charArray = new char[3];
*charArray = 'c';
*(charArray+1) = 'a';
*(charArray+2) = 't';
//use the toVector function for both arrays
vector<int> v1 = toVector(intArray, 3);
vector<char> v2 = toVector(charArray, 3);
//now print the two vectors
for (int i=0; i<3; i++) {
cout << v1[i] << " ";
}
cout << endl;
for (int i=0; i<3; i++) {
cout << v2[i] << " ";
}
cout << endl;
return 0;
}

Here is a screenshot of the code:

Here is the output of the code:

Comment in case of any doubts.


Related Solutions

C++ Write a toVector function that takes in a C-style pointer of any type and a...
C++ Write a toVector function that takes in a C-style pointer of any type and a size (int) and returns a vector of the same size containing a copy of the elements in the input array. You may assume that the array elements have a valid copy constructor. Please explain every line of code to me
write a function named as cubeCalculator that takes an integer pointer as function and return its...
write a function named as cubeCalculator that takes an integer pointer as function and return its cube value , you are required to compute the cube of a number using pointer notation , return the result as an integer value , use c++
Create a function output() in C that takes the pointer to the array and its size...
Create a function output() in C that takes the pointer to the array and its size and prints the arrays' contests. (function prototype : void output(int *arrayPtr, int size);
Write a function pretty_print, which takes one parameter that can be any type of namedtuple. It...
Write a function pretty_print, which takes one parameter that can be any type of namedtuple. It "pretty prints" the contents of the namedtuple, including both the names of its fields and their values. This is subject to a few rules. Each field and its value are displayed on one line, with the name of the field appearing first, followed by a colon and a space, followed by the value of the field converted to a string. The fields should be...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
Write a C++ function to print any given std::array of a given type to the standard...
Write a C++ function to print any given std::array of a given type to the standard output in the form of {element 0, element 1, element 2, ...}. For example given the double array, d_array = {2.1, 3.4, 5.6, 2.9}, you'd print {2.1, 3.4, 5.6, 2.9} to the output upon executing std::cout << d_array; line of code. Your function mus overload << operator.
Language C++ Thank You! -Write a function getMeASentence() that takes zero arguments, and its return type...
Language C++ Thank You! -Write a function getMeASentence() that takes zero arguments, and its return type is string. This function will return a randomly generated sentence that abides by certain rules. -The rules of this sentence are that there are 5 randomly generated lowercase letters, a space, a randomly generated inequality symbol, a space, and a randomly generated 1 digit number. -In this context, an inequality symbol is understood to mean the "<", ">", or "=" symbol. -Write a function...
Question: Write a method named reduce that: ● Takes a function of type (A, A) =>...
Question: Write a method named reduce that: ● Takes a function of type (A, A) => A ● Returns A ● Combines all the elements of the list into a single value by applying the provided function to all elements ○ You may assume the function is commutative ● If the list has size 1, return that element without calling the provided function Example: If head stores a reference to the List(4, 6, 2) head.reduce((a: Int, b: Int) => a...
Please write program in c++ with using pointer. create a function that can find the number...
Please write program in c++ with using pointer. create a function that can find the number of even numbers that are between the maximum and minimum elements of an given array.The program have to use pointer. example 8 -1 2 2 1 9 2 4 0 output: 2
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT