100% de satisfacción garantizada Inmediatamente disponible después del pago Tanto en línea como en PDF No estas atado a nada 4.2 TrustPilot
logo-home
Otro

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

Puntuación
-
Vendido
-
Páginas
1
Subido en
06-11-2025
Escrito en
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++.

Mostrar más Leer menos
Institución
Grado








Ups! No podemos cargar tu documento ahora. Inténtalo de nuevo o contacta con soporte.

Libro relacionado

Escuela, estudio y materia

Institución
Grado

Información del documento

Subido en
6 de noviembre de 2025
Número de páginas
1
Escrito en
2025/2026
Tipo
Otro
Personaje
Desconocido

Temas

Vista previa del contenido

#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;
}
$15.49
Accede al documento completo:

100% de satisfacción garantizada
Inmediatamente disponible después del pago
Tanto en línea como en PDF
No estas atado a nada

Conoce al vendedor
Seller avatar
tinahuynh

Conoce al vendedor

Seller avatar
tinahuynh California State University, Long Beach
Seguir Necesitas iniciar sesión para seguir a otros usuarios o asignaturas
Vendido
Nuevo en Stuvia
Miembro desde
1 mes
Número de seguidores
0
Documentos
68
Última venta
-

0.0

0 reseñas

5
0
4
0
3
0
2
0
1
0

Recientemente visto por ti

Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes