answersLogoWhite

0


Best Answer

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

#include

std::ostream& operator<< (std::ostream& os, const std::vector& v)

{

os<<"Size:\t\t"<

"\nCapacity:\t"<

std::endl;

return(os);

}

int main()

{

std::cout<<"Initialising\n"<

std::vector v;

std::cout<

std::cout<<"Insert 1 element\n"<

v.push_back(0);

std::cout<

std::cout<<"Insert 99 elements\n"<

for(size_t i=1; i<100; ++i)

v.push_back(i);

std::cout<

std::cout<<"Reserve space for 200 elements\n"<

v.reserve(200);

std::cout<

std::cout<<"Extract last 50 elements\n"<

for(size_t i=0; i<50; ++i)

v.pop_back();

std::cout<

std::cout<<"Resize to 25 elements\n"<

v.resize(25);

std::cout<

std::cout<<"Optimise\n"<

v.shrink_to_fit();

std::cout<

}

Output

Initialising

Size: 0

Capacity: 0

Insert 1 element

Size: 1

Capacity: 1

Insert 99 elements

Size: 100

Capacity: 141

Reserve space for 200 elements

Size: 100

Capacity: 200

Extract last 50 elements

Size: 50

Capacity: 200

Resize to 25 elements

Size: 25

Capacity: 200

Optimise

Size: 25

Capacity: 25

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What does the capacity vector member function return and how should this be used?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How does the compiler differentiate the statement and function in C programming?

statement should not return a value but function returns a value


What is the difference between using a return value and a pass-by-reference parameter?

The main difference is that return values can be used in compound statements (such as assignments), whereas output parameters cannot. An output parameter is a non-const parameter that is passed by reference or as a pointer variable.As a general rule, every function should return something through the return value, even if only an error level where zero would typically indicate success (but not always). A function that does not return anything (returns void) is not strictly a function, it is better described as a procedure. However, if a function can be guaranteed never to fail (provides trivial functionality) and has no need to return any other value to the caller, then returning void is a logical option. Just because a function should return a value doesn't mean that it must return a value.However, the return value is limited by the fact that it only permits one value to be returned by a function. Output parameters allow a function to return several values at once and can be used in conjunction with the return value. If there is no need for the return value, it should be used to indicate the error level of the function wherever it would be appropriate. However it is not unusual for a function to accept a reference as an output parameter and to also return that same reference via the return value.Note that although you could return multiple values via a struct or class type, this should only be considered if you have several functions that can make use of the struct or class type. However, in most cases it would be simpler to make those functions members of the struct or class itself.


When a class uses dynamic memory what member function should be provided by class?

the copy constructor


Difference in using a method that return value and method that does not return value in object orinted programming?

A method that return a value should have a return statement. The method signature should indicate the type of return value. While in the case of a method that does not return a value should not have a return statement and in the signature, the return type is void. When using a method that doesn't return a value, a programmer can not get a value from that function, but instead, it can only change variable values and run other methods.


What data type does the main function return in c?

The main function must return the int data type. A program that terminates normally should return the value zero to indicate no error. Not all execution environments make use of the return value (Windows in particular), however a command script or batch file can examine the ERRORLEVEL if required.

Related questions

Why function should return a value?

Not all functions return values. If you take a function which is of type void, you get a function which is does not return anything. The only functions which should return values are those which are used as a right side of expressions (so called rvalues).


What function should be used to add a column of numbers and return a single value?

The "SUM" function.


How does the compiler differentiate the statement and function in C programming?

statement should not return a value but function returns a value


Why it display function should return a value in c programme?

It is up to you to decide. You may go for 'void' return type, which means no return value.


What is the difference between using a return value and a pass-by-reference parameter?

The main difference is that return values can be used in compound statements (such as assignments), whereas output parameters cannot. An output parameter is a non-const parameter that is passed by reference or as a pointer variable.As a general rule, every function should return something through the return value, even if only an error level where zero would typically indicate success (but not always). A function that does not return anything (returns void) is not strictly a function, it is better described as a procedure. However, if a function can be guaranteed never to fail (provides trivial functionality) and has no need to return any other value to the caller, then returning void is a logical option. Just because a function should return a value doesn't mean that it must return a value.However, the return value is limited by the fact that it only permits one value to be returned by a function. Output parameters allow a function to return several values at once and can be used in conjunction with the return value. If there is no need for the return value, it should be used to indicate the error level of the function wherever it would be appropriate. However it is not unusual for a function to accept a reference as an output parameter and to also return that same reference via the return value.Note that although you could return multiple values via a struct or class type, this should only be considered if you have several functions that can make use of the struct or class type. However, in most cases it would be simpler to make those functions members of the struct or class itself.


When a class uses dynamic memory what member function should be provided by class?

the copy constructor


How is heat capacity a path function?

Heat capacity is NOT a path function. It is a STATE function. It depends on the phase of the material, the temperature and the pressure. Usually heat capacity is known at some particular condition and then a calculation is required to estimate it at the condition of interest. Performing these calculations should always result in the same final value no matter the path you took to get to the value - hence it is a STATE function rather than PATH. Path functions would be things like WORK and HEAT (for which the state function "heat capacity" might be used in the calculations)


Can you use the float as a return type of the main function in 'C' language?

No, it should be int type or void.


Difference in using a method that return value and method that does not return value in object orinted programming?

A method that return a value should have a return statement. The method signature should indicate the type of return value. While in the case of a method that does not return a value should not have a return statement and in the signature, the return type is void. When using a method that doesn't return a value, a programmer can not get a value from that function, but instead, it can only change variable values and run other methods.


What is the mental capacity of someone with an IQ of 77?

An IQ of 77 typically indicates cognitive abilities below average, but mental capacity can vary greatly among individuals. People with this IQ level may face challenges with more complex cognitive tasks and may require additional support in certain areas such as learning new skills or problem-solving. It's important to consider each person's unique strengths and abilities when discussing mental capacity.


What is the need for return statements in c?

It's not so much a need but a requirement. Every C function must contain at least one return statement, typically at the end of the function body (before the closing brace). However, a C function may have more than one return path, and each requires its own return statement. Note that it does not matter whether the function returns a value or not; even a function that returns void must have a return statement. In C++ the rules regarding return statements are more relaxed. Functions that return void do not require a return statement at all; when execution reaches the closing brace of a void function, a return statement is implied. All functions that return a value of any type other than void must have a return statement. The one exception to this rule is the global main function which must always return an int. If the global main function has no return statement then the value 0 is implicitly returned to the calling environment (the value 0 is typically used to indicate no error). However, if we need to return other values (including 0), then we must include a return statement. Functions that have multiple return paths are considered poor style and should be avoided. Functions are generally much easier to read and maintain when there is only one return path which should logically terminate at the very end of the function. However, eliminating multiple return paths can also produce more efficient machine code, particularly in functions with highly complex return paths. Eliminating multiple return paths needn't be difficult, we simply need to refactor the function such that each unique return path is representing by some function which returns the appropriate value. The calling function simply stores that value and returns it at the end of the function, thus simplifying the overall complexity of the calling function. Refactoring complex functions into smaller, simpler function calls is good style in and of itself; well-named, descriptive function calls result in code that is largely self-documenting and thus more abstract. Although function calls are themselves expensive, small and simple functions can be easily inline-expanded by the compiler's optimisers, so it's a win-win.


How heat is a path function?

Heat capacity is NOT a path function. It is a STATE function. It depends on the phase of the material, the temperature and the pressure. Usually heat capacity is known at some particular condition and then a calculation is required to estimate it at the condition of interest. Performing these calculations should always result in the same final value no matter the path you took to get to the value - hence it is a STATE function rather than PATH. Path functions would be things like WORK and HEAT (for which the state function "heat capacity" might be used in the calculations)