SlideShare a Scribd company logo
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

What's hot

Set methods in python
Set methods in pythonSet methods in python
Set methods in python
deepalishinkar1
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
Python Modules
Python ModulesPython Modules
Python Modules
Nitin Reddy Katkam
 
This pointer
This pointerThis pointer
This pointer
Kamal Acharya
 
javascript objects
javascript objectsjavascript objects
javascript objects
Vijay Kalyan
 
Sets in python
Sets in pythonSets in python
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
Pooja B S
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
moazamali28
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
JavaScript Control Statements I
JavaScript Control Statements IJavaScript Control Statements I
JavaScript Control Statements I
Reem Alattas
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
Binay Kumar Ray
 
C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And References
verisan
 
Strings in python
Strings in pythonStrings in python
Strings in python
Prabhakaran V M
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
hydpy
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
eShikshak
 
Wrapper class
Wrapper classWrapper class
Wrapper class
kamal kotecha
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
Abhilash Nair
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
RaginiJain21
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
Mauryasuraj98
 
Strings
StringsStrings
Strings
Nilesh Dalvi
 

What's hot (20)

Set methods in python
Set methods in pythonSet methods in python
Set methods in python
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
 
Python Modules
Python ModulesPython Modules
Python Modules
 
This pointer
This pointerThis pointer
This pointer
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
Sets in python
Sets in pythonSets in python
Sets in python
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
JavaScript Control Statements I
JavaScript Control Statements IJavaScript Control Statements I
JavaScript Control Statements I
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
 
C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And References
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Strings
StringsStrings
Strings
 

Viewers also liked

Enhancing growth curve approach using cgpann for predicting
Enhancing growth curve approach using cgpann for predictingEnhancing growth curve approach using cgpann for predicting
Enhancing growth curve approach using cgpann for predicting
Jawad Khan
 
Computer science curriculum based on Program learning outcomes and objectives
Computer science curriculum based on Program learning outcomes and objectivesComputer science curriculum based on Program learning outcomes and objectives
Computer science curriculum based on Program learning outcomes and objectives
Jawad Khan
 
What's Inside the Growth Curve?
What's Inside the Growth Curve?What's Inside the Growth Curve?
What's Inside the Growth Curve?
Shridhar Lolla
 
Skills of a computer network student
Skills of a computer network studentSkills of a computer network student
Skills of a computer network student
Jawad Khan
 
Computer engineering and its applications
Computer engineering and its applicationsComputer engineering and its applications
Computer engineering and its applications
Jawad Khan
 
Complexity analysis - The Big O Notation
Complexity analysis - The Big O NotationComplexity analysis - The Big O Notation
Complexity analysis - The Big O Notation
Jawad Khan
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
Application of cgpann in solar irradiance
Application of cgpann in solar irradianceApplication of cgpann in solar irradiance
Application of cgpann in solar irradiance
Jawad Khan
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
Vineeta Garg
 
Wind power forecasting an application of machine
Wind power forecasting   an application of machineWind power forecasting   an application of machine
Wind power forecasting an application of machine
Jawad Khan
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
Jawad Khan
 

Viewers also liked (11)

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

Similar to Vector class in C++

cpp programing language exercise Vector.ppt
cpp programing language exercise Vector.pptcpp programing language exercise Vector.ppt
cpp programing language exercise Vector.ppt
ssuser8ac8e7
 
Intro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & ArraysIntro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & Arrays
Blue Elephant Consulting
 
Vector3
Vector3Vector3
Vector3
Rajendran
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++
Ilio Catallo
 
Oops lab manual2
Oops lab manual2Oops lab manual2
Oops lab manual2
Mouna Guru
 
Vectors data frames
Vectors data framesVectors data frames
Vectors data frames
FAO
 
6. R data structures
6. R data structures6. R data structures
6. R data structures
ExternalEvents
 
Matlab vectors
Matlab vectorsMatlab vectors
Matlab vectors
pramodkumar1804
 
8. Vectors data frames
8. Vectors data frames8. Vectors data frames
8. Vectors data frames
ExternalEvents
 
Lecture one
Lecture oneLecture one
Lecture one
Mahmoud Hussein
 
I am trying to fill out a program where the method definitions will b.docx
I am trying  to fill out a program where the method definitions will b.docxI am trying  to fill out a program where the method definitions will b.docx
I am trying to fill out a program where the method definitions will b.docx
Phil4IDBrownh
 
vectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptx
vectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptxvectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptx
vectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptx
VivekSharma34623
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structure
lodhran-hayat
 
Implement a function in c++ which takes in a vector of integers and .pdf
Implement a function in c++ which takes in a vector of integers and .pdfImplement a function in c++ which takes in a vector of integers and .pdf
Implement a function in c++ which takes in a vector of integers and .pdf
feelingspaldi
 
Mat 104 practical use of vector differentiation
Mat 104 practical use of vector differentiationMat 104 practical use of vector differentiation
Mat 104 practical use of vector differentiation
Md. Asad Chowdhury Dipu
 
The Java Fx Platform – A Java Developer’S Guide
The Java Fx Platform – A Java Developer’S GuideThe Java Fx Platform – A Java Developer’S Guide
The Java Fx Platform – A Java Developer’S Guide
Stephen Chin
 
The deadline of submission is February 15th. Acceptable file format f.pdf
The deadline of submission is February 15th. Acceptable file format f.pdfThe deadline of submission is February 15th. Acceptable file format f.pdf
The deadline of submission is February 15th. Acceptable file format f.pdf
fashionfootwear1
 
03. Week 03.pptx
03. Week 03.pptx03. Week 03.pptx
03. Week 03.pptx
Vinc2ntCabrera
 
Practical use of vector differentiation
Practical use of vector differentiationPractical use of vector differentiation
Practical use of vector differentiation
Md. Asad Chowdhury Dipu
 
Java script ppt from students in internet technology
Java script ppt from students in internet technologyJava script ppt from students in internet technology
Java script ppt from students in internet technology
SherinRappai
 

Similar to Vector class in C++ (20)

cpp programing language exercise Vector.ppt
cpp programing language exercise Vector.pptcpp programing language exercise Vector.ppt
cpp programing language exercise Vector.ppt
 
Intro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & ArraysIntro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & Arrays
 
Vector3
Vector3Vector3
Vector3
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++
 
Oops lab manual2
Oops lab manual2Oops lab manual2
Oops lab manual2
 
Vectors data frames
Vectors data framesVectors data frames
Vectors data frames
 
6. R data structures
6. R data structures6. R data structures
6. R data structures
 
Matlab vectors
Matlab vectorsMatlab vectors
Matlab vectors
 
8. Vectors data frames
8. Vectors data frames8. Vectors data frames
8. Vectors data frames
 
Lecture one
Lecture oneLecture one
Lecture one
 
I am trying to fill out a program where the method definitions will b.docx
I am trying  to fill out a program where the method definitions will b.docxI am trying  to fill out a program where the method definitions will b.docx
I am trying to fill out a program where the method definitions will b.docx
 
vectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptx
vectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptxvectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptx
vectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptx
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structure
 
Implement a function in c++ which takes in a vector of integers and .pdf
Implement a function in c++ which takes in a vector of integers and .pdfImplement a function in c++ which takes in a vector of integers and .pdf
Implement a function in c++ which takes in a vector of integers and .pdf
 
Mat 104 practical use of vector differentiation
Mat 104 practical use of vector differentiationMat 104 practical use of vector differentiation
Mat 104 practical use of vector differentiation
 
The Java Fx Platform – A Java Developer’S Guide
The Java Fx Platform – A Java Developer’S GuideThe Java Fx Platform – A Java Developer’S Guide
The Java Fx Platform – A Java Developer’S Guide
 
The deadline of submission is February 15th. Acceptable file format f.pdf
The deadline of submission is February 15th. Acceptable file format f.pdfThe deadline of submission is February 15th. Acceptable file format f.pdf
The deadline of submission is February 15th. Acceptable file format f.pdf
 
03. Week 03.pptx
03. Week 03.pptx03. Week 03.pptx
03. Week 03.pptx
 
Practical use of vector differentiation
Practical use of vector differentiationPractical use of vector differentiation
Practical use of vector differentiation
 
Java script ppt from students in internet technology
Java script ppt from students in internet technologyJava script ppt from students in internet technology
Java script ppt from students in internet technology
 

More from Jawad Khan

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

More from Jawad Khan (20)

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

Recently uploaded

বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 

Recently uploaded (20)

বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 

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; }