100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 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
Institution
Course








Whoops! We can’t load your doc right now. Try again or contact support.

Connected book

Written for

Institution
Course

Document information

Uploaded on
November 6, 2025
Number of pages
1
Written in
2025/2026
Type
Other
Person
Unknown

Subjects

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;
}
R271,90
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
tinahuynh

Get to know the seller

Seller avatar
tinahuynh California State University, Long Beach
Follow You need to be logged in order to follow users or courses
Sold
New on Stuvia
Member since
1 month
Number of followers
0
Documents
68
Last sold
-

0,0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

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

Didn't get what you expected? Choose another document

No worries! You can immediately select a different document that better matches what you need.

Pay how you prefer, start learning right away

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

Student with book image

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

Alisha Student

Frequently asked questions