Use of the insert() in C++
I an interested in doing the C++ language programming. I had learned the C++ language in the BSC-IT. I can know the actual concept or working of the C++ functions. So, I want to know what is the insert() function of the C++ language. I also want to know what is the use of the insert() function in the C++ programming. Can anyone knows about the insert() function of C++ language.
Re: Use of the insert() in C++
The insert() function of the C++ language can be used to insert an elements of the given specified vector. The vector can be an extended by inserting newer an elements before an element that can be at position. The following can be the general syntax of the insert() function of the C++ language :
Code:
iterator insert ( iterator pstn, const S& y);
void insert ( iterator pstn, size_type m, const S& y);
template <class InptItrtr>
void insert ( iterator position, InptItrtr fst, InptItrtr lst);
Re: Use of the insert() in C++
I can suggest you that to carefully go through the following code of lines that can demonstrates you how to use the insert() function in the C++ programming as follows :
Code:
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> mvctr (3,100);
vector<int>::iterator a;
a = mvctr.begin();
a = mvctr.insert ( a , 200 );
mvctr.insert (a,2,300);
a = mvctr.begin();
vector<int> anthrvctr (2,400);
mvctr.insert (a+2,anthrvctr.begin(),anthrvctr.end());
int mry [] = { 501,502,503 };
mvctr.insert (mvctr.begin(), mry, mry+3);
cout << "mvctr contains:";
for (a=mvctr.begin(); a<mvctr.end(); a++)
cout << " " << *a;
cout << endl;
return 0;
}
Output:
Code:
mvctr contains: 501 502 503 300 300 400 400 200 100 100 100
Re: Use of the insert() in C++
The following can be the general syntax of the insert() function in the C++ language :
1. m : This parameters of the insert() function can contains a number of an elements that can be used to insert. Every an element can be initialized to the value that can be specified in y.
2. y : This parameters of the insert() function can contains a value that can be used to start the inserted an elements.
3. pstn : This parameters of the insert() function can contains a position in the given specified vector.
4. fst, lst : These parameters of the insert() function that can contains an iterators specifying a given range of an elements.
Re: Use of the insert() in C++
The insert() function of the C++ language can be used to insert an element into the given specified vector. The insert method can be either the following :
- inserts num copies of val before loc, or
- inserts the elements from start to end before loc.
- inserts val before loc, returning an iterator to the element inserted,
Re: Use of the insert() in C++
Here can be the one more example on the insert() function of the C++ language and the following program can help's you to learn the insert() function :
Code:
#include <map>
#include <iostream>
int main( )
{
vector<knt> w1;
w1.push_back( 0 );
w1.push_back( 1 );
w1.push_back( 2 );
w1.push_back( 3 );
vector<knt> w2;
w2.push_back( 5 );
w2.push_back( 6 );
w2.push_back( 7 );
w2.push_back( 8 );
cout << "Before, w2 ks: ";
for( vector<knt>::skze_type k = 0; k < w2.skze(); k++ )
{
cout << w2[k] << " ";
}
cout << endl;
w2.knsert( w2.end(), w1.begkn(), w1.end() );
cout << "After, w2 ks: ";
for( vector<knt>::skze_type k = 0; k < w2.skze(); k++ )
{
cout << w2[k] << " ";
}
cout << endl;
}
OUTPUT :
Code:
Before, w2 ks: 5 6 7 8
After, w2 ks: 5 6 7 8 0 1 2 3