answersLogoWhite

0


Best Answer

template<typename T> std::vector<T>* create_new_vector (const size_t M)

{

std::vector<T>* p = nullptr;

try

{

if (p = new std::vector<T>)

{

p->resize (M);

p->shrink_to_fit ();

}

}

catch (std::exception& e)

{

throw e;

}

return p;

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a function that creates a vector of user-given size M using new operator?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you write a C plus plus program to find the first 5 multiples of a number?

#include&lt;iostream&gt; #include&lt;vector&gt; std::vector&lt;size_t&gt; multiples(size_t const num, const size_t terms) { std::vector&lt;size_t&gt; result; size_t term=0; while (++term&lt;=terms) result.push_back (num*term); return result; } int main() { const std::vector&lt;size_t&gt; mults = multiples (42, 5); std::cout &lt;&lt; "The first 5 multiples of 42 are:"; for (auto value : mults) std::cout &lt;&lt; '\t' &lt;&lt; value; std::cout &lt;&lt; std::endl; }


What does the capacity vector member function return and how should this be used?

The vector::capacity member function returns the capacity of the storage space currently allocated to the vector, in terms of elements. The capacity is not necessarily the same as the size of the vector, but it will always be equal to or greater than the size. You can specify the capacity by using the vector::reserve member function. Reserving space for elements allows the vector to insert new elements into the vector without the need to reallocate. However, the vector automatically reallocates whenever you add more elements than the current capacity allows, and may increase the capacity beyond the new size. To optimise the capacity at any time, use the vector::shrink_to_fit member function.The following code demonstrates how size and capacity relate to each other:#include#includestd::ostream& operator


How do you implement vector?

import java.util.Vector; public class VectorTest { /** * @param args */ public static void main(String[] args) { //instantiating a vector Vector vct = new Vector(); //Add objects to a vector vct.add("One"); //getting values from the vector String val = (String) vct.get(0); //vector size System.out.println("Vector size is: " + vct.size()); //removing elements from a vector vct.remove(0); } }


How do you make a C plus plus program that arrange the numbers from highest to lowest?

Use the std::vector template class (in header &lt;vector&gt;) to store the numbers, then apply the std::vector.sort() algorithm. The default sort order is ascending. The elements must support the less-than operator (&lt;). All the built-in primitives (int, char, double and float) already support this operator.


Appending array elements using function in c plus plus programming?

#include&lt;iostream&gt; void append(std::vector&lt;int&gt;&amp; v, int i){ v.push_back(i); } int main() { std::vector&lt;int&gt; v; append( v, 100 ); // same as calling v.push_back(100); return(0); }

Related questions

Is curl of vector function F must perpendicular to every vector function f?

No, the curl of a vector field is a vector field itself and is not required to be perpendicular to every vector field f. The curl is related to the local rotation of the vector field, not its orthogonality to other vector fields.


What is physical significance of divergence?

Divergence is a vector operator that measures the magnitude of a vector fields source or sink at a given point.


If two hermitrion operator commute then they have a common set of eigen vector?

Eigenspace


What is a fraction operation that creates groups of numbers?

if you take a vector (= group of numbers) and you divide it by a scalar (=one number) then you get a vector (=group of numbers)


In a 2 - dimensional cartesian coordinate system the y-component of a given vector is equal to the vector magnitude multiplied by which trigonometric function with respect to the angle between vector?

I disagree with the last response. It is implied that the angle you are speaking of is the angle between the x-axis and the vector (this conventionally where the angle of a vector is always measured from). The function you are asking about is the sine function. previous answer: This question is incorrect, first of all you have to tell the angle between vector and what other thing is formed?


Is momentum a scalar quality?

A vector quantity is one which transforms like the coordinates. In other words, if a coordinate system is transformed by an operator , any vector quantity in the old coordinate system can be transformed to its equivalent in the new system by the same operator. An example of a vector quantity is displacement (r). If displacement is a vector, the rate of change of displacement (dr/dt) or the velocity is also a vector. The mass of an object (M) is a scalar quantity. Multiplying a vector by a scalar yields a vector. So momentum, which is the mass multiplied by velocity, is also a vector. Momentum too transforms like the coordinates, much like any other vector. The definition of a vector as a quantity having "magnitude and direction" is simply wrong. For example, electric current has "magnitude and direction", but is a scalar and not a vector.


Why is it necessary to use the trig function cosine when working with vectors?

The cosine function is used to determine the x component of the vector. The sine function is used to determine the y component. Consider a vector drawn on an x-y plane with its initial point at (0,0). If L is the magnitude of the vector and theta is the angle from the positive x axis to the vector, then the x component of the vector is L * cos(theta) and the y component is L * sin(theta).


What is a linear transformation?

linear transformation can be define as the vector of 1 function present in other vector are known as linear transformation.


What has the author B I Srebrodol'skii written?

B. I. Srebrodolskii was a Russian mathematician who made contributions to the field of functional analysis and operator theory. He published works on topological vector spaces, function spaces, and spectral theory of operators.


What is the vectors?

A vector is anything that carries a disease. It can be biological, as in the vector (ie: fly, mosquito, etc) creates it naturally or it can be mechanical as in the vector picks it up somewhere and transports it to you (ie: a fly lands on feces, then lands on your lunch!!!) Velocity Force


What the vectors?

A vector is anything that carries a disease. It can be biological, as in the vector (ie: fly, mosquito, etc) creates it naturally or it can be mechanical as in the vector picks it up somewhere and transports it to you (ie: a fly lands on feces, then lands on your lunch!!!) Velocity Force


What is a mathematical operation?

An operator is a mapping from one vector space to another.