SlideShare a Scribd company logo
4
Most read
5
Most read
10
Most read
Vector
Type Vector arrays for undefined length of arrays
HTTPS://WWW.SCRIBD.COM/DOCUMENT/2539561/LINUX-C-PROGRAMMING-HOWTO
Page 34: Onward
Why to use vector
 A vector is an array like container that improves on the C++ array types. In
particular it is not necessary to know how big you want the vector to be when you
declare it, you can add new elements to the end of a vector using the push_back
function. (In fact the insert function allows you insert new elements at any position
of the vector, but this is a very inefficient operation −− if you need to do this often
consider using a list instead).
Constructing a vector
You can construct any type of vector. It can hold defined data types as well as class
objects
1. vector<int> v1;
2. vector<string> v2;
3. vector<ClassObject> v3;
Declaration and initialization
You can give an initial size to a vector by using a declaration like
vector<char> v4(26);
which says that v4 is to be vector of characters that initially has room for 26 characters.
There is also a way to initialise a vector's elements. The declaration
vector<float> v5(100,1.0);
says that v5 is a vector of 100 floating point numbers each of which has been
initialised to 1.0.
Checking Program for vector return
#include <iostream.h>
#include <vector.h>
void main()
{
vector<int> v1;
vector<int> v2(10);
vector<int> v3(10,7);
cout << "v1.size() returns " << v1.size() << endl;
cout << "v2.size() returns " << v2.size() << endl;
cout << "v3.size() returns " << v3.size() << endl;
}
Checking whether vector is empty or not
 VectorName.empty();
 Returns a Boolean value;
Accessing Elements of a Vector
You can access a vector's elements using operator[]. Thus, if you wanted to print out
all the elements in a vector you could use code like
vector<int> v;
// show loop and assign loop
for (int i=0; i<v.size(); i++)
cout << v[i];
for (int i=0; i<v.size(); i++)
v[i] = 2*i;
Back() and front() functions
 You can ACCESS the first and last member of the vector by calling them in dot (.)
notation like class.
 char ch = v.front(); //let v be a character vector
 v.back()=‘0’;
Inserting and Erasing Vector Elements
Along with operator[] as described above there are a number of other ways to
change or access the elementsin a vector.
1. push_back will add a new element to the end of a vector.
2. pop_back will remove the last element of a vector.
3. insert will insert one or more new elements, at a designated position, in the
vector
4. erase will remove one or more elements from a vector between designated
positions
Insert and Erase ++ Pop_back() and
Push_back(T) – an example
#include <iostream.h>
#include <vector.h>
void main()
{
vector<int> v;
for (int i=0; i<10; i++) v.push_back(i);
cout << "Vector initialised to:" << endl;
for (int i=0; i<10; i++) cout << v[i] << ' ' ;
cout << endl;
for (int i=0; i<3; i++) v.pop_back();
cout << "Vector length now: " << v.size() << endl;
cout << "It contains:" << endl;
//continued
for (int i=0; i<v.size(); i++) cout << v[i] << ' ';
cout << endl;
int a1[5];
for (int i=0; i<5; i++) a1[i] = 100;
v.insert(& v[3], & a1[0],& a1[3]);
cout << "Vector now contains:" << endl;
for (int i=0; i<v.size(); i++) cout << v[i] << '
';cout << endl;
v.erase(& v[4],& v[7]);
cout << "Vector now contains:" << endl;
for (int i=0; i<v.size(); i++) cout << v[i] << ' ';
cout << endl;
}
Vector iteration
For a vector containing elements of type T:
vector<T> v;
an iterator is declared as follows:
vector<T>::iterator i; //iteration is defined in vector class already
Begin() and end()
Such iterators are constructed and returned by the functions begin() and end().
You can compare two iterators(of the same type) using == and !=, increment using
++ and dereference using *.
DO REMEMBER
For iteration itr1
1. itr1 will return the position
2. *itr1 will return value at that position (dereferencing)
Iteration – an example
//continued
{
*i = j;
j++;
i++;
}
// Square each element of v.
for (i=v.begin(); i!=v.end(); i++) *i = (*i) * (*i);
// Print out the vector v.
cout << "The vector v contains: ";
for (i=v.begin(); i!=v.end(); i++) cout << *i << ' ';
cout << endl;
}
#include <iostream.h>
#include <vector.h>
void main()
{
vector<int> v(10);
first is ``less'' than the second
int j = 1;
vector<int>::iterator i;
// Fill the vector v with integers 1 to 10.
i = v.begin();
while (i != v.end())
Comparison of vectors
You can compare two vectors using == and <. == will return true only if both vectors
have the same number of elements and all elements are equal.
• (1,2,3,4) < (5,6,7,8,9,10) is false
• (1,2,3) < (1,2,3,4) is true
• (1,2,3,4) < (1,2,3) is false
• (0,1,2,3) < (1,2,3) is true
A good Example:
The < functions performs a lexicographic comparison of
the two vectors. This works by comparing the vectors
element by element. Suppose we are comparing v1 and
v2 (that is v1 < v2?). Set i=0. If v1[i] < v2[i] then return
true, if v1[i] > v2[i] then return false, otherwise increment
i(that is move on to the next element).
If the end of v1 is reached before v2 return “true”,
otherwise returns “false”. Lexicographic order is also
known as dictionary order.
Vector - Code
#include <vector.h>
#include <iostream.h>
void main()
{
vector<int> v1;
vector<int> v2;
for (int i=0; i<4; i++) v1.push_back(i+1);
for (int i=0; i<3; i++) v2.push_back(i+1);
cout << "v1: ";
for (int i=0; i<v1.size(); i++) cout << v1[i] << ' ';
cout << endl;
cout << "v2: ";
for (int i=0; i<v2.size(); i++) cout << v2[i] << ' ';
cout << endl;
cout << "v1 < v2 is: " << (v1<v2 ? "true" : "false") << endl;
}
END UP

More Related Content

PPTX
Standard Template Library
PDF
Arrays in C++
PDF
Data Structures Notes 2021
PPSX
Files in c++
PDF
PPT
DATA STRUCTURE AND ALGORITHMS
PPTX
Introduction to data structure and algorithms
PPTX
Constructors and destructors
Standard Template Library
Arrays in C++
Data Structures Notes 2021
Files in c++
DATA STRUCTURE AND ALGORITHMS
Introduction to data structure and algorithms
Constructors and destructors

What's hot (20)

PDF
PPTX
Binary Heap Tree, Data Structure
PDF
Arrays in python
PDF
List,tuple,dictionary
PPTX
Arrays In C++
PPTX
Python Datatypes by SujithKumar
PPT
File handling in c
PPTX
Linked List
PPT
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
PDF
Python list
PPTX
C++ decision making
PDF
Data Structures
PPTX
Python Functions
PDF
Python exception handling
PPTX
Exception handling c++
PDF
Arrays In Python | Python Array Operations | Edureka
PPT
Input and output in C++
PDF
Python lists &amp; sets
PPTX
classes and objects in C++
PPTX
Unit I - Evaluation of expression
Binary Heap Tree, Data Structure
Arrays in python
List,tuple,dictionary
Arrays In C++
Python Datatypes by SujithKumar
File handling in c
Linked List
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Python list
C++ decision making
Data Structures
Python Functions
Python exception handling
Exception handling c++
Arrays In Python | Python Array Operations | Edureka
Input and output in C++
Python lists &amp; sets
classes and objects in C++
Unit I - Evaluation of expression
Ad

Viewers also liked (11)

PPTX
Enhancing growth curve approach using cgpann for predicting
PPTX
Computer science curriculum based on Program learning outcomes and objectives
PDF
What's Inside the Growth Curve?
PPTX
Skills of a computer network student
PPTX
Computer engineering and its applications
PPTX
Complexity analysis - The Big O Notation
PPTX
Stacks in c++
PPTX
Application of cgpann in solar irradiance
PPTX
Queues in C++
PPTX
Wind power forecasting an application of machine
PPTX
Inheritance, friend function, virtual function, polymorphism
Enhancing growth curve approach using cgpann for predicting
Computer science curriculum based on Program learning outcomes and objectives
What's Inside the Growth Curve?
Skills of a computer network student
Computer engineering and its applications
Complexity analysis - The Big O Notation
Stacks in c++
Application of cgpann in solar irradiance
Queues in C++
Wind power forecasting an application of machine
Inheritance, friend function, virtual function, polymorphism
Ad

Similar to Vector class in C++ (20)

PPTX
Object Oriented Design and Programming Unit-05
PPT
Vector3
PPTX
C++ STL (quickest way to learn, even for absolute beginners).pptx
PPTX
C++ STL (quickest way to learn, even for absolute beginners).pptx
PPT
cpp programing language exercise Vector.ppt
PPTX
Intro To C++ - Class #18: Vectors & Arrays
DOC
Oops lab manual2
PPTX
14. containers, vector, list
PDF
Sppu University|BE|Computer Engineering|OOPs|unit 6 notes|ppt
PDF
Resource wrappers in C++
PPTX
How to choose best containers in STL (C++)
PPT
Standard Template Library
PPTX
Vectors in Java
PPT
Standard Template Library (STL) in Object Oriented Programming
PPT
standard template library(STL) in C++
PDF
STL in C++
DOCX
I am trying to fill out a program where the method definitions will b.docx
PDF
C++ Lesson #6 - Data Structures vector queue stack.pdf
PDF
DS & Algo 1 - C++ and STL Introduction
PPTX
Time and Space Complexity Analysis.pptx
Object Oriented Design and Programming Unit-05
Vector3
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
cpp programing language exercise Vector.ppt
Intro To C++ - Class #18: Vectors & Arrays
Oops lab manual2
14. containers, vector, list
Sppu University|BE|Computer Engineering|OOPs|unit 6 notes|ppt
Resource wrappers in C++
How to choose best containers in STL (C++)
Standard Template Library
Vectors in Java
Standard Template Library (STL) in Object Oriented Programming
standard template library(STL) in C++
STL in C++
I am trying to fill out a program where the method definitions will b.docx
C++ Lesson #6 - Data Structures vector queue stack.pdf
DS & Algo 1 - C++ and STL Introduction
Time and Space Complexity Analysis.pptx

More from Jawad Khan (20)

PPTX
2.1 input and output in c
PPTX
2.2 variable arithmetics and logics
PPTX
1.2 programming fundamentals
PPTX
1.1 programming fundamentals
PPTX
7 8. emi - analog instruments and digital instruments
PPTX
6. emi instrument transformers (with marking)
PPTX
5 emi ac bridges (with marking)
PPTX
4. emi potentiometer and ac bridges
PPTX
3 .emi wattmeter and energy meter
PPTX
2. emi analog electromechanical instruments
PPTX
1. emi concept of measurement system
PPTX
Varibale frequency response lecturer 2 - audio+
PPTX
Variable frequency response lecture 3 - audio
PPTX
Varibale frequency response lecturer 1 - audio
PPTX
Two port network - part 3
PPTX
Two port network - part 2
PPTX
Two port network - part 1
PPTX
4. ideal transformer and load conversion
PPTX
3. magnetic coupled circuits examples
PPTX
2. magnetic coupled circuits
2.1 input and output in c
2.2 variable arithmetics and logics
1.2 programming fundamentals
1.1 programming fundamentals
7 8. emi - analog instruments and digital instruments
6. emi instrument transformers (with marking)
5 emi ac bridges (with marking)
4. emi potentiometer and ac bridges
3 .emi wattmeter and energy meter
2. emi analog electromechanical instruments
1. emi concept of measurement system
Varibale frequency response lecturer 2 - audio+
Variable frequency response lecture 3 - audio
Varibale frequency response lecturer 1 - audio
Two port network - part 3
Two port network - part 2
Two port network - part 1
4. ideal transformer and load conversion
3. magnetic coupled circuits examples
2. magnetic coupled circuits

Recently uploaded (20)

PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Cell Structure & Organelles in detailed.
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
Classroom Observation Tools for Teachers
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Lesson notes of climatology university.
PDF
RMMM.pdf make it easy to upload and study
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
Pre independence Education in Inndia.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Supply Chain Operations Speaking Notes -ICLT Program
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Cell Structure & Organelles in detailed.
TR - Agricultural Crops Production NC III.pdf
Basic Mud Logging Guide for educational purpose
Classroom Observation Tools for Teachers
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Microbial diseases, their pathogenesis and prophylaxis
102 student loan defaulters named and shamed – Is someone you know on the list?
Sports Quiz easy sports quiz sports quiz
Lesson notes of climatology university.
RMMM.pdf make it easy to upload and study
VCE English Exam - Section C Student Revision Booklet
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Microbial disease of the cardiovascular and lymphatic systems
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Cell Types and Its function , kingdom of life
Pre independence Education in Inndia.pdf

Vector class in C++

  • 1. Vector Type Vector arrays for undefined length of arrays HTTPS://WWW.SCRIBD.COM/DOCUMENT/2539561/LINUX-C-PROGRAMMING-HOWTO Page 34: Onward
  • 2. Why to use vector  A vector is an array like container that improves on the C++ array types. In particular it is not necessary to know how big you want the vector to be when you declare it, you can add new elements to the end of a vector using the push_back function. (In fact the insert function allows you insert new elements at any position of the vector, but this is a very inefficient operation −− if you need to do this often consider using a list instead).
  • 3. Constructing a vector You can construct any type of vector. It can hold defined data types as well as class objects 1. vector<int> v1; 2. vector<string> v2; 3. vector<ClassObject> v3;
  • 4. Declaration and initialization You can give an initial size to a vector by using a declaration like vector<char> v4(26); which says that v4 is to be vector of characters that initially has room for 26 characters. There is also a way to initialise a vector's elements. The declaration vector<float> v5(100,1.0); says that v5 is a vector of 100 floating point numbers each of which has been initialised to 1.0.
  • 5. Checking Program for vector return #include <iostream.h> #include <vector.h> void main() { vector<int> v1; vector<int> v2(10); vector<int> v3(10,7); cout << "v1.size() returns " << v1.size() << endl; cout << "v2.size() returns " << v2.size() << endl; cout << "v3.size() returns " << v3.size() << endl; }
  • 6. Checking whether vector is empty or not  VectorName.empty();  Returns a Boolean value;
  • 7. Accessing Elements of a Vector You can access a vector's elements using operator[]. Thus, if you wanted to print out all the elements in a vector you could use code like vector<int> v; // show loop and assign loop for (int i=0; i<v.size(); i++) cout << v[i]; for (int i=0; i<v.size(); i++) v[i] = 2*i;
  • 8. Back() and front() functions  You can ACCESS the first and last member of the vector by calling them in dot (.) notation like class.  char ch = v.front(); //let v be a character vector  v.back()=‘0’;
  • 9. Inserting and Erasing Vector Elements Along with operator[] as described above there are a number of other ways to change or access the elementsin a vector. 1. push_back will add a new element to the end of a vector. 2. pop_back will remove the last element of a vector. 3. insert will insert one or more new elements, at a designated position, in the vector 4. erase will remove one or more elements from a vector between designated positions
  • 10. Insert and Erase ++ Pop_back() and Push_back(T) – an example #include <iostream.h> #include <vector.h> void main() { vector<int> v; for (int i=0; i<10; i++) v.push_back(i); cout << "Vector initialised to:" << endl; for (int i=0; i<10; i++) cout << v[i] << ' ' ; cout << endl; for (int i=0; i<3; i++) v.pop_back(); cout << "Vector length now: " << v.size() << endl; cout << "It contains:" << endl; //continued for (int i=0; i<v.size(); i++) cout << v[i] << ' '; cout << endl; int a1[5]; for (int i=0; i<5; i++) a1[i] = 100; v.insert(& v[3], & a1[0],& a1[3]); cout << "Vector now contains:" << endl; for (int i=0; i<v.size(); i++) cout << v[i] << ' ';cout << endl; v.erase(& v[4],& v[7]); cout << "Vector now contains:" << endl; for (int i=0; i<v.size(); i++) cout << v[i] << ' '; cout << endl; }
  • 11. Vector iteration For a vector containing elements of type T: vector<T> v; an iterator is declared as follows: vector<T>::iterator i; //iteration is defined in vector class already
  • 12. Begin() and end() Such iterators are constructed and returned by the functions begin() and end(). You can compare two iterators(of the same type) using == and !=, increment using ++ and dereference using *. DO REMEMBER For iteration itr1 1. itr1 will return the position 2. *itr1 will return value at that position (dereferencing)
  • 13. Iteration – an example //continued { *i = j; j++; i++; } // Square each element of v. for (i=v.begin(); i!=v.end(); i++) *i = (*i) * (*i); // Print out the vector v. cout << "The vector v contains: "; for (i=v.begin(); i!=v.end(); i++) cout << *i << ' '; cout << endl; } #include <iostream.h> #include <vector.h> void main() { vector<int> v(10); first is ``less'' than the second int j = 1; vector<int>::iterator i; // Fill the vector v with integers 1 to 10. i = v.begin(); while (i != v.end())
  • 14. Comparison of vectors You can compare two vectors using == and <. == will return true only if both vectors have the same number of elements and all elements are equal. • (1,2,3,4) < (5,6,7,8,9,10) is false • (1,2,3) < (1,2,3,4) is true • (1,2,3,4) < (1,2,3) is false • (0,1,2,3) < (1,2,3) is true A good Example: The < functions performs a lexicographic comparison of the two vectors. This works by comparing the vectors element by element. Suppose we are comparing v1 and v2 (that is v1 < v2?). Set i=0. If v1[i] < v2[i] then return true, if v1[i] > v2[i] then return false, otherwise increment i(that is move on to the next element). If the end of v1 is reached before v2 return “true”, otherwise returns “false”. Lexicographic order is also known as dictionary order.
  • 15. Vector - Code #include <vector.h> #include <iostream.h> void main() { vector<int> v1; vector<int> v2; for (int i=0; i<4; i++) v1.push_back(i+1); for (int i=0; i<3; i++) v2.push_back(i+1); cout << "v1: "; for (int i=0; i<v1.size(); i++) cout << v1[i] << ' '; cout << endl; cout << "v2: "; for (int i=0; i<v2.size(); i++) cout << v2[i] << ' '; cout << endl; cout << "v1 < v2 is: " << (v1<v2 ? "true" : "false") << endl; }