Trending October 2023 # How To Reserve() Function Work In C ++ With Sample Code # Suggested November 2023 # Top 10 Popular | Cersearch.com

Trending October 2023 # How To Reserve() Function Work In C ++ With Sample Code # Suggested November 2023 # Top 10 Popular

You are reading the article How To Reserve() Function Work In C ++ With Sample Code updated in October 2023 on the website Cersearch.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How To Reserve() Function Work In C ++ With Sample Code

Introduction to C++ reserve()

Web development, programming languages, Software testing & others

Syntax

void reserve (size_type n)

Here n signifies the number of elements that will be stored in the vector. It will not return any value, but it will reserve the space in the memory. The resulting capacity can be equal to or greater than n. size_type is a type that is an unsigned integral type. This can also be referred to as size_t.

How reserve() function work in C ++?

Let us check the working of reserve() function in C ++.

int main () { szc = example.capacity(); example.reserve(100); std::cout << " Let us change the size of sample:n:n"; for (int i=0; i<100; ++i) { example.push_back(i); if (szc!=example.capacity()) { szc = example.capacity(); std::cout << "Capacity of example is changed to: " << szc << 'n'; } } }

The reserve function here is reserving 100 bytes. This is allocated in the system once the reserve function is called. The capacity of this variable changes internally. You can then keep assigning values to this variable until this size is full. This function will allocate the said memory beforehand. We will check the working of this function with more examples as below.

Examples

Here are the following examples mentioned below.

Example #1

Code:

using namespace std; int main(void) { ssize_t size; size = vec1.capacity(); for (int i = 0; i < 25; ++i) { vec1.push_back(i); if (size != vec1.capacity()) { size = vec1.capacity(); cout << “Increasing the size limit of vector 1 so that it holds” << size << ” elements” << endl; } } cout << endl << endl; vec2.reserve(35); for (int i = 0; i < 25; ++i) { vec2.push_back(i); if (size != vec2.capacity()) { size = vec2.capacity(); cout << “Increasing the size limit of vector 2 so that it holds ” << size << ” elements” << endl; } } return 0; }

In the first case, we have used a for loop, which allocates the capacity until it reaches 35. We are using post increment in the loop. As we have not used a reserve() function in the first case, the capacity function can allocate space greater than 35.

While in the second case, we are again using for loop until it reaches capacity. The difference that you observe here is we have made use of the vector function reserve(). In this case, will you see that as space is already reserved hence, it will not allocate the space again and again, unlike the first case? As space is already allocated, there will not be multiple allocations. Observe the output so that you understand this functionality better.

Output:

You will see that space increases in the first case. In the second case, space is allocated at one go.

Example #2

// minimal C++11 allocator with debug output struct LetAlloc { typedef spc value_type; LetAlloc() = default; spc* allocate(std::size_t n) { n *= sizeof(spc); std::cout << “Let us allocate space ” << n << ” bytesn”; } void deallocate(spc* p, std::size_t n) { std::cout << “Let us deallocate space ” << n*sizeof*p << ” bytesn”; ::operator delete(p); } }; int main() { int spsz = 100; std::cout << “We are reserving space here by using reserve function: n”; { vec1.reserve(spsz); for(int n = 0; n < spsz; ++n) vec1.push_back(n); } std::cout << “Here we are using usual space allocation: n”; { for(int n = 0; n < spsz; ++n) vec1.push_back(n); } }

In the above function, we have created a structure which is allocating and deallocates space using the structure and template format. Similarly, we are also deallocating the space by using the delete function. Secondly, we have used the reserve function, which very easily allocates the space as specified. We have defined a variable known as spsz with the size allocated as 100. Observe the below output to understand better.

Output:

Conclusion Recommended Articles

This is a guide to the C++ reserve(). Here we discuss how to reserve() function work in C ++ with respective examples for better understanding. You may also have a look at the following articles to learn more –

You're reading How To Reserve() Function Work In C ++ With Sample Code

Update the detailed information about How To Reserve() Function Work In C ++ With Sample Code on the Cersearch.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!