Introduction
- In this article you will get the knowledge of vectors in c++.
- The points that are covered in this article are mention below.
- What is a vector and why we use in C++?
- Important function used in vector?
- How do I get the size of a vector in C++?
- How do you add an element to a vector in C++?
- How do you find the last element of a vector?
- Sorting a vector in descending order in C++?
- Can you have a vector of vectors C++?
- vector c++ examples.?
- Vector pointer in c++.?
- vector constructor in c++?
- some problems in vector ?
Vectors in c++
Vectors in c++
What is vector and why we use in c++
- vector is a dynamic contiguous array and it is a sequence container(linear fashion).
- In vector we insert as many elements we want to add but in a array of 10 integer to add the 11th element we need to make an array again.
- it is likely to array but array has static data type and vector has dynamic datatype.
- vector is declared in the header file (#include<vector>).
- we use vector in c++ because it is dynamic and it automatically adjust the size when an element is inserted or deleted from it.
Important functions used mostly in vector
How do I get values in vector in C++
syntax -
vec1.push_back(39);
How do I delete last element in vector in C++
2. pop_back (delete the last element)
syntax -
vec1.pop_back( )
How do I erase a vector in C++
3. erase (This is used to erase the elements in the vector)
syntax-
vec1.erase(vec1.begin()+3)This will erase 4th element from first
How do I begin a vector in C++
4.begin() (This is used to begin from the first element)
syntax-
vec1.begin()
How do I get the size of a vector in C++
5. size() (This function help us to get the size of the vector)
syntax-
vec1.size()
How do I insert an element in vector in C++
6. insert() (This function is used to insert value in vector)
syntax-
vector<int>::iterator it;
vec1.insert(it,5);
How do I access the front element in vector in C++
7. front() (This function help us to access the first element of vector)
syntax-
vec1.front()
How do I access the back element in vector in C++
7. back() (This function help us to access the first element of vector)
syntax-
vec1.back()
How do I copy of element in vector in C++
8. insert() (This function is used to insert value in vector)
syntax-
vector<int>::iterator it;
vec1.insert(it+1,5); //number of element you its copy will print
How do I sort an element in vector in C++
9. sort() (This function is used to sort value in vector)
syntax-
sort()
sort(vec1.begin(),vec1.end())
for descending order
sort(vec1.rbegin(),vec1.rend())
For more functions click here
Coding
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
- This is the required header files here we will use vectors so we include here <vector>
- header file and for using some sorting and some data structure functions we use here
- <algorithm> header file.
int main()
{
//ways to create vector
vector<int> vec1; //zero length integer vector
int element, size ;
cout<<"Enter the size of your vector"<<endl;
cin>>size;
for (int i = 0; i < size; i++)
{
cout<<"Enter an element to add to this vector";
cin>>element;
vec1.push_back(element);
}
- This is the way to create a vector and add value to insert it .first create a
- zero length vector ,then declare two variables of element and size for storing
- the size of vector and elements in vector and use vec1.push_back for storing our elements in vector.
void display(vector<T> &v)
{
cout<<"Displaying this vector";
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
cout << v.at(i) << " "; //at operator help us to know where is the value
}
cout << endl;
}
This is the way to create a function of name display to display the elements in vector.
vec1.pop_back();
display(vec1);
This is the way to remove value from last using pop_back function and display this using the display function.
vector<int>::iterator it;
vec1.insert(it,5);
This is the way to insert element into vector using iterator and display it through the function display().
erasing element from the vector
vec1.erase(vec1.begin()+3);//this will erase the 4th element
vec1.erase(vec1.begin(), vec1.begin()+3);//This will erase the 1st 3 element
This is the way to erase elements in the vector
sort(vec1.begin(), vec1.end()); //for sorting the vectors
cout << "The elements after sorting in the vector is::" << endl;
for (int i = 0; i < vec1.size(); i++)
{
cout << vec1[i] << " \n";
}
This is the way to sort the elements in the vector
for (int i = 0; i < vec1.size(); i++)
{
cout << vec1[i] << " \n";
}
return 0;
}
Lastly display the size of the vector and display it.
Coding Execution in IDE
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> vec1;
int element, size;
cout << "Enter the size of the vector:" << endl;
cin >> size;
for (int i = 0; i < size; i++)
{
cout << "Enter the element To add In vector:" << endl;
cin >> element;
vec1.push_back(element);
}
//using itarator insert the element This is always tuse an iterator
// vector<int>::iterator it;
// vec1.insert(it,5);
//erasing element from the vector
// vec1.erase(vec1.begin()+3);//this will erase the 4th element
// vec1.erase(vec1.begin(), vec1.begin()+3);//This will erase the 1st 3 element
// sort(vec1.begin(), vec1.end()); //for sorting the vectors
// cout << "The elements after sorting in the vector is::" << endl;
// for (int i = 0; i < vec1.size(); i++)
// {
// cout << vec1[i] << " \n";
// }
// vec1.push_back(78);
// vec1.push_back(16);
// // now front equals 78, and back 16
// vec1.front() -= vec1.back();
//if try to remove the element
// vec1.pop_back();
// cout << "After pop back the elements are:"<<endl;
for (int i = 0; i < vec1.size(); i++)
{
cout << vec1[i] << " \n";
}
return 0;
}
vector pointer in c++
#include <iostream>
#include <vector>
using namespace std;
void foo(vector<int>* a) ///else we can use here reference &a
{
cout << a->at(0) << a->at(1) << a->at(2) << endl;
// expected result is "123"
}
int main()
{
vector<int> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
foo(&a);
}
This is an example of pointer using in vector .
vector constructor in c++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> first; //empty vector of ints
vector<int> second(4, 100); //4 ints with value 100
vector<int> third(second.begin(), second.end()); //itarating through the second vector
vector<int> fourth(third); //a copy of third
// the iterator constructor can also be used to construct from arrays:
int myint[] = {4, 5, 6, 9};
vector<int> fifth(myint, myint + sizeof(myint) / sizeof(int));
cout << "The contents of fifth are:";
for (std::vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
cout << ' ' << *it;
cout << '\n';
}
This is an example of constructor using in vector .
Some problems in vector
Q-1)
Prints three highest numbers from a list of numbers in descending order
Also check Out
Also check Out Projects for Resume
Feedback
Hope you understand all the things that are we discussed till now if still you have any doubts regarding Vectors in C++ please comment below and we will try to solve that
THANKYOU!!
Post a Comment
Post a Comment
Please do not spam in comment