insert()
This method allows inserting any value wherever you please.
This has 3 overloaded versions. The first one accepts only 2 arguments. One is the iterator which indicates where the value is to be inserted and the other argument is the value itself.
Here is a code that uses this version of insert() method.
#include <iostream>
#include <vector>
using namespace std;
voidDisplay(vector<int> m)
{
for(int i=0;i<m.capacity();i++)
cout<<m.at(i)<<endl;
}
int main()
{
vector<int> nums;
for(int i=1;i<11;i++)
nums.push_back(i);
cout<<"Before inserting the 100 at 2nd place the vector was :"<<endl;
Display(nums);
nums.insert(nums.begin()+1,100);
cout<<"After inserting the 100 at 2nd place the vector is :"<<endl;
Display(nums);
return 0;
}
Here is the output of the above program
Before inserting the 100 at 2nd place the vector was :
1
2
3
4
5
6
7
8
9
10
After inserting the 100 at 2nd place the vector was :
1
100
2
3
4
5
6
7
8
9
10
There are two more overloaded versions for this method insert() Now we will discuss about the second one. It takes 3 arguments. This is used for copying part or full of another collection. (May be a vector, a plain C++ array or some other collections that is accessed using pointers/iterators) Say we have a plain C++ array like this
And we want to insert 5,6,3 in our vector defined in the last program. We want to insert these values right there where we inserted 100 in the last program. Here is the code.
#include <iostream>
#include <vector>
using namespace std;
voidDisplay(vector<int> m)
{
for(int i=0;i<m.capacity();i++)
cout<<m.at(i)<<endl;
}
int main()
{
int array[]={5,6,3,4};
vector<int> nums;
for(int i=1;i<11;i++)
nums.push_back(i);
cout<<"Before inserting the array elements at 2nd place the vector was :"<<endl;
Display(nums);
nums.insert(nums.begin()+1,array,array+3);
cout<<"After inserting the array elements at 2nd place the vector is :"<<endl;
Display(nums);
return 0;
}
Here is the output of the code
Before inserting the arry elements at 2nd place the vector was :
1
2
3
4
5
6
7
8
9
10
After inserting the array elements at 2nd place the vector is :
1
5
6
3
2
3
4
5
6
7
8
9
10
As you can see first 3 array elements are inserted in between 1 and 2 of the original vector.
The last overloaded version of insert allows you to insert one constant value , M number of times in any place within the original vector. Here is the code for inserting three 99 in between 1 and 2.
#include <iostream>
#include <vector>
using namespace std;
voidDisplay(vector<int> m)
{
for(int i=0;i<m.capacity();i++)
cout<<m.at(i)<<endl;
}
int main()
{
int array[]={5,6,3,4};
vector<int> nums;
for(int i=1;i<5;i++)
nums.push_back(i);
cout<<"Before inserting the constant elements at 2nd place the vector was :"<<endl;
Display(nums);
nums.insert(nums.begin()+1,3,99);
cout<<"After inserting the constant elements at 2nd place the vector is :"<<endl;
Display(nums);
return 0;
}
Here is the output
Before inserting the constant elements at 2nd place the vector was :
1
2
3
4
After inserting the constant elements at 2nd place the vector is :
1
99
99
99
2
3
4
Applications
Suppose we need to put the co-ordinates of a moving point. We can create a structure that holds two values representing the co-ordinates at any moment and then we can create a vector of these points.
Here is the code.
#include <iostream>
#include <vector>
#include <conio.h>//for getch()
using namespace std;
typedef struct Point
{
float x;
float y;
float z;
}Point;//This will represent a point at any point of time.
int main()
{
//No need to tell the compiler how many elements
vector<Point> MovingPoints;
float x,y,z;
Point TemporaryPoint;
for(int i=0;i<4;i++)
{
cout<<"Enter the co-ordinates of the points :";
cin>>x>>y>>z;
TemporaryPoint.x=x;
TemporaryPoint.y=y;
TemporaryPoint.z=z;
MovingPoints.push_back(TemporaryPoint);
}
vector<Point>::iterator k = MovingPoints.begin();
cout<<"The moving point's coordinates were "<<endl;
for(;k<MovingPoints.end();k++)
cout<<"("<<k->x<<","<<k->y<<","<<k->z<<")"<<endl;
getch();
return 0;
}
Here is the output of the code
Enter the co-ordinates of the points :1 2 3
Enter the co-ordinates of the points :4 5 6
Enter the co-ordinates of the points :6 7 3
Enter the co-ordinates of the points :4 6 7
The moving point's coordinates were
(1,2,3)
(4,5,6)
(6,7,3)
(4,6,7)