answersLogoWhite

0

The following function will print the contents of any vector of any type T which overloads the std::ostream::operator<< operator function.

template

std::ostream& print_elements (std::ostream& os, std::vector& vec) {

os << "{";

for (auto val : vec) os << val << ", ";

return os << "/b/b} ";

}

for (auto val : vec) is known as a range-for statement and can be interpreted as meaning "for each val in vec".

Note that /b is the backspace character. We use it here to backspace over the final comma and space before overwriting with a closing brace and a space.

Example usage:

std::vector vi {1, 3, 5, 7, 9};

std::vector vs {"the", "quick", "brown", "fox"};

print_elements (std::cout, vi);

print_elements (std::cout, vs);

Output:

{1, 3, 5, 7, 9}

{"the", "quick", "brown", "fox"}

User Avatar

Wiki User

7y ago

What else can I help you with?

Related Questions

What is zero vector?

A zero vector is a vector whose value in every dimension is zero.


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.


Is it necessary that every vector has to obey the law of vector addition?

Yes.


What is the difference between finger prints?

Nobody's finger prints are the same. Every person in the world has different finger prints. Even identical twins have different prints.


Does every one have same tongue prints?

no no


A vedtor which is perpendicular to every vector?

The zero vector is not perpendicular to all vectors, but it is orthogonal to all vectors.


What is a zero vector?

A zero vector is a vector whose value in every dimension is zero.


Every vector can be represented as the sum of its?

Every vector can be represented as the sum of its orthogonal components. For example, in a 2D space, any vector can be expressed as the sum of two orthogonal vectors along the x and y axes. In a 3D space, any vector can be represented as the sum of three orthogonal vectors along the x, y, and z axes.


What is the common content that prints on every page of a worksheet?

The web adress


What does the nucleus of every element contain?

Every nucleus of every element contains at least 1 proton. Every element except hydrogen also contains neutrons.


Is every irrotational vector field conservative?

Yes, every irrotational vector field is conservative because a vector field being irrotational implies that its curl is zero, which, by one of the fundamental theorems of vector calculus, implies that the vector field is conservative.


Is the empty set an element of every set?

No. An empty set is a subset of every set but it is not an element of every set.