• ¿Documento equivocado? Cámbialo gratis
  • Escrito por estudiantes que aprobaron
  • Inmediatamente disponible después del pago
  • Leer en línea o como PDF
Vender
¿Dónde estudias?
Tu idioma
Document preview thumbnail
Vista previa 2 fuera de 9 páginas
Resumen

Summary - CS 8251 (101)

Document preview thumbnail
Vista previa 2 fuera de 9 páginas

This doc lead to sucessfullly coplete our c++ course

Vista previa del contenido

Vectors in C++ STL | C++ Tutorials for
Beginners


The Standard Template Library (STL) in C++ provides several useful
features for efficient programming. One of the most important
components of STL is the Vector data structure. A Vector is a dynamic
array that can resize itself automatically as elements are added or
removed. Here's a brief overview of how to initialize and manipulate
Vectors in C++.

Initializing Vectors in C++

You can initialize a Vector in C++ in several ways - using default
initialization, initializer list, or by copying an existing Vector. Here's an
example:

#include <vector>
using namespace std;

// Initialize a vector with default values
vector<int> vec1;

// Initialize a vector with initializer list
vector<int> vec2 = {1, 2, 3, 4, 5};

// Initialize a vector by copying another vector
vector<int> vec3 = vec2;
Iterators and Vector Functions

Iterators are a powerful feature of the STL that allow you to traverse
and modify the elements of a Container, including Vectors. You can
use iterators to access, insert, or remove elements from a Vector.
Here are some common Vector functions that use iterators:

vec.begin(); // Returns an iterator pointing to the first
element
vec.end(); // Returns an iterator pointing to the end of
the vector
vec.insert(it, val); // Inserts a value at a specific
iterator
vec.erase(it); // Erases the element at the given
iterator

, vec.push_back(val); // Adds a value to the end of the
vector
vec.pop_back(); // Removes the last element of the
vector
vec.size(); // Returns the number of elements in
the vector
Accessing and Modifying Vectors

You can access and modify the elements of a Vector using the index
operator [], at(), or front() and back() functions. Here are some
examples:

vec[0] = 1; // Access and modify the element at index 0
vec.at(1) = 2; // Access and modify the element at
index 1 using the at() function
vec.front() = 3; // Access and modify the first element
using the front() function
vec.back() = 4; // Access and modify the last element
using the back() function
Arrays vs Vectors in C++

Vectors have several advantages over traditional arrays in C++,
including automatic resizing, dynamic memory management, and
built-in functions like push_back(), pop_back(), and size(). However,
Vectors can be slightly slower than arrays due to their dynamic nature.

Dynamic Arrays: Vectors

Vectors are essentially dynamic arrays that can grow or shrink in size
as necessary. This allows you to write more efficient code that avoids
the limitations of fixed-size arrays.

Bound Checking in Arrays vs Vectors

Vectors provide built-in bound checking, which means that you can't
access an element outside its bounds as you would with a traditional
array. This can help prevent common programming errors and improve
code reliability.

int arr[5];
arr[5] = 10; // This is an error because the array has a
size of 5

vector<int> vec(5);
vec[5] = 10; // This is also an error, and the program
will throw an exception

Información del documento

Subido en
8 de marzo de 2024
Número de páginas
9
Escrito en
2023/2024
Tipo
Resumen
$11.89

¿Documento equivocado? Cámbialo gratis Dentro de los 14 días posteriores a la compra y antes de descargarlo, puedes elegir otro documento. Puedes gastar el importe de nuevo.
Escrito por estudiantes que aprobaron
Inmediatamente disponible después del pago
Leer en línea o como PDF

Vendido
0
Seguidores
0
Artículos
3
Última venta
-



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