Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Other

Working with capacity(), push_back(), and size()

Rating
-
Sold
-
Pages
1
Uploaded on
06-11-2025
Written in
2025/2026

This file is a C++ program that demonstrates the use of the standard library's `vector` container. Here’s a breakdown of what it does: 1. **Includes Necessary Libraries**: It begins by including the `iostream` and `vector` libraries, which are essential for input/output operations and the use of dynamic arrays, respectively. 2. **Using Namespace**: The `using namespace std;` line allows the program to use standard library names without needing to prefix them with `std::`. 3. **Main Function**: The program starts execution in the `main()` function. 4. **Vector Declaration**: It declares a vector of integers named `numbers` and an iterator `it` for traversing the vector. 5. **User Input Loop**: The program enters an infinite loop where it prompts the user to enter positive integers. The loop continues until the user inputs `-1`, which serves as a signal to exit. 6. **Dynamic Size Management**: After each valid number is input, the program uses `push_back()` to add the number to the `numbers` vector. It then outputs the current size and capacity of the vector. The `capacity()` function returns the size of the allocated storage used for the vector, which may be greater than or equal to its current size. 7. **Accessing Vector Element**: After the loop, the program accesses and prints the third element (index 2) of the vector. 8. **Modifying Vector Element**: The program then modifies the third element of the vector (index 2) to a value of `99` and outputs the changed value. Overall, this program demonstrates basic C++ programming concepts, including loops, user input handling, the dynamic array capabilities of `vector`, and element accessing/modification. It's a good example for beginners to understand how vectors work in C++.

Show more Read less

Content preview

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

int main()
{
vector<int> numbers;
vector<int>::iterator it;
int temp;

// capacity() returns the size of the array
cout << "Capacity: " << numbers.capacity() << endl << endl;

while (true)
{
cout << "Enter a positive number (-1 to quit): ";
cin >> temp;
if (temp == -1)
break;
numbers.push_back(temp);
cout << "Size: " << numbers.size() << endl;
cout << "Capacity: " << numbers.capacity() << endl;
}

cout << numbers[2] << endl << endl;

numbers[2] = 99;
cout << "After the change, " << numbers[2] << endl << endl;
}

Written for

Document information

Uploaded on
November 6, 2025
Number of pages
1
Written in
2025/2026
Type
OTHER
Person
Unknown
$15.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
tinahuynh

Get to know the seller

Seller avatar
tinahuynh California State University, Long Beach
View profile
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
8 months
Number of followers
0
Documents
68
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions