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
Summary

Summary - CS 8251 (101)

Rating
-
Sold
-
Pages
9
Uploaded on
08-03-2024
Written in
2023/2024

This doc lead to sucessfullly coplete our c++ course

Institution
Course

Content preview

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

Written for

Institution
Course

Document information

Uploaded on
March 8, 2024
Number of pages
9
Written in
2023/2024
Type
SUMMARY

Subjects

$11.89
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
kumaravelcivil

Get to know the seller

Seller avatar
kumaravelcivil ANNA
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
3
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 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