100% tevredenheidsgarantie Direct beschikbaar na je betaling Lees online óf als PDF Geen vaste maandelijkse kosten 4.2 TrustPilot
logo-home
Overig

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

Beoordeling
-
Verkocht
-
Pagina's
1
Geüpload op
06-11-2025
Geschreven 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++.

Meer zien Lees minder
Instelling
Vak








Oeps! We kunnen je document nu niet laden. Probeer het nog eens of neem contact op met support.

Gekoppeld boek

Geschreven voor

Instelling
Vak

Documentinformatie

Geüpload op
6 november 2025
Aantal pagina's
1
Geschreven in
2025/2026
Type
Overig
Persoon
Onbekend

Onderwerpen

Voorbeeld van de inhoud

#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;
}
€13,72
Krijg toegang tot het volledige document:

100% tevredenheidsgarantie
Direct beschikbaar na je betaling
Lees online óf als PDF
Geen vaste maandelijkse kosten

Maak kennis met de verkoper
Seller avatar
tinahuynh

Maak kennis met de verkoper

Seller avatar
tinahuynh California State University, Long Beach
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
Nieuw op Stuvia
Lid sinds
1 maand
Aantal volgers
0
Documenten
68
Laatst verkocht
-

0,0

0 beoordelingen

5
0
4
0
3
0
2
0
1
0

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo makkelijk kan het dus zijn.”

Alisha Student

Veelgestelde vragen