To use a vector, the vector library has to be included at the top of the program, with
All vector codes for this article are in the C++ main() function.
Article Content
Push Back
A single element can be pushed at the back of a vector. There are two syntaxes for the push_back() member functions, which are:
void push_back(T&& x)
They both return void, and they are used similarly.
The following code has a vector of flowers in Britain. One more flower is push_back(), i.e., appended, to the vector. The vector is a vector of strings of the string class.
#include <string>
#include <iostream>
using namespace std;
int main(){
vector<string> vtr = {"blue throatwort", "bottlebrush", "brodiaea", "broom", "columbine"};
for (int i=0; i<vtr.size(); i++)
cout<<vtr[i] << ", ";
cout<<endl;
vtr.push_back("cornflower");
for (vector<string>::iterator it=vtr.begin(); it != vtr.end(); it++)
cout<< *it << ", ";
cout<<endl;
return 0;
}
The output is:
blue throatwort, bottlebrush, brodiaea, broom, columbine, cornflower,
The program begins with the necessary inclusion directives. Then there is the main() function with all the vector code. In the main() function, a vector of five strings of flower names is declared. This list is then displayed using a for-loop and indexes. The principal statement in the code is:
This expression appends another single flower name to the vector list. There are now six elements in the vector. The next code segment displays the set of six elements, using a for-loop and iterators.
Push Back to Empty Vector
A vector must not always be created with elements. A vector can be created, empty. The push_back() member function can still be used to feed in elements to an empty vector. The following code illustrates this:
#include <string>
#include <iostream>
using namespace std;
int main(){
vector<string> vtr;
vtr.push_back("blue throatwort");
vtr.push_back("bottlebrush");
vtr.push_back("brodiaea");
for (int i=0; i<vtr.size(); i++)
cout<<vtr[i] << ", ";
cout<<endl;
return 0;
}
The output is:
In the main() function, the first statement declares an empty vector. The next three statements feed the vector with three flower names, using the push_back() function. The code segment that follows displays the three values of the vector.
Inserting
Two simplified functions for inserting to a vector are:
a.insert(p,rv)
where ‘a’ is the name of a vector and p is an iterator pointing to the element in front of which insert will take place. These functions are used in a similar way, as illustrated in the following example:
#include <string>
#include <iostream>
using namespace std;
int main(){
vector<string> vtr = {"blue throatwort", "bottlebrush", "brodiaea", "broom", "columbine"};
vector<string>::iterator p = vtr.end();
vector<string>::iterator pRet = vtr.insert(p, "cornflower");
for (int i=0; i<vtr.size(); i++)
cout<<vtr[i] << ", ";
cout<<endl;
cout<< *pRet<<endl;
return 0;
}
The output is:
cornflower
The two special statements in this code are:
vector<string>::iterator pRet = vtr.insert(p, "cornflower");
The first statement here returns an iterator that points just after the last element of the vector. After insertion, the iterator returned, points to the inserted element. In this case, the iterator returned is pRet. The expression, *pRet in the code, obtains the value pointed to by pRet.
a.insert(p,n,t)
This inserts n of the same t values. In this case, insertion has to take place at the end, as in the following code:
#include <string>
#include <iostream>
using namespace std;
int main(){
vector<string> vtr = {"blue throatwort", "bottlebrush", "brodiaea", "broom", "columbine"};
vector<string>::iterator p = vtr.end();
vector<string>::iterator pRet = vtr.insert(p, 3, "cornflower");
for (int i=0; i<vtr.size(); i++)
cout<<vtr[i] << ", ";
cout<<endl;
cout<<pRet - vtr.begin() <<endl;
return 0;
}
The output is:
5
Two new statements of interest in this program are:
and
The first statement here inserts 3 elements of "cornflower". The second statement calculates and returns the index corresponding to the iterator returned by the insert() function. This iterator points to the first element of the inserted elements.
a.insert(p,i,j)
This inserts a range of elements from a similar vector to the vector of interest. i and j are iterators. The element indicated by j is not inserted. Such a range is denoted by [i, j) . In the situation of appending, the range has to be inserted at the back. The following program illustrates this:
#include <string>
#include <iostream>
using namespace std;
int main()
{
vector<string> vec = {"cornflower", "corsage orchid", "dill", "drumstick", "foxglove"};
vector<string>::iterator itB = vec.begin();
itB++;
vector<string>::iterator itE = vec.end();
itE--; itE--;
vector<string> vtr = {"blue throatwort", "bottlebrush", "brodiaea", "broom", "columbine"};
vector<string>::iterator p = vtr.end();
vector<string>::iterator pRet = vtr.insert(p, itB, itE);
for (int i=0; i<vtr.size(); i++)
cout<<vtr[i] << ", ";
cout<<endl;
cout<<pRet - vtr.begin() <<endl;
return 0;
}
The output is:
5
The second statement in the main() function returns an iterator that points to "cornflower". The third statement makes this iterator point to "corsage orchid". The statement after returns an iterator that points just after "foxglove". The statement following makes this iterator point to "drumstick". So the range is now,
corresponding to [itB, itE) . However, the range inserted is ("corsage orchid", "dill") as explained above.
a.insert(p, il)
A list literal can be inserted at the back of the vector. In this case, the last element of the list is inserted. The following program illustrates this:
#include <string>
#include <iostream>
using namespace std;
int main() {
vector<string> vtr = {"blue throatwort", "bottlebrush", "brodiaea", "broom", "columbine"};
vector<string>::iterator p = vtr.end();
vector<string>::iterator pRet = vtr.insert(p, {"corsage orchid", "dill", "drumstick"});
for (int i=0; i<vtr.size(); i++)
cout<<vtr[i] << ", ";
cout<<endl;
cout<<pRet - vtr.begin() <<endl;
return 0;
}
The output is:
5
The special statement in this program is:
The inserted list is:
The returned iterator points to the first element of the inserted list.
Emplace
Emplace is like an insert. Since this article deals with appending, the emplace has to take place at the back of the vector.
a.emplace(p, args)
This is the simplified member function of emplace(). p is an iterator, pointing to the element, in front of which, the new element is inserted. The function returns an iterator pointing to the element inserted. The following program illustrates this:
#include <string>
#include <iostream>
using namespace std;
int main() {
vector<string> vtr = {"blue throatwort", "bottlebrush", "brodiaea", "broom", "columbine"};
vector<string>::iterator p = vtr.end();
vector<string>::iterator pRet = vtr.emplace(p, "cornflower");
for (int i=0; i<vtr.size(); i++)
cout<<vtr[i] << ", ";
cout<<endl;
cout<< *pRet<<endl;
return 0;
}
The output is:
cornflower
The special statement in this program is:
a.emplace_back(args)
Here, ‘a’ is the name of the vector. emplace_back() is like push_back(). It appends an element to the vector. It does not return an iterator. It returns a reference to the element inserted. The following program illustrates its use:
#include <string>
#include <iostream>
using namespace std;
int main() {
vector<string> vtr = {"blue throatwort", "bottlebrush", "brodiaea", "broom", "columbine"};
for (int i=0; i<vtr.size(); i++)
cout<<vtr[i] << ", ";
cout<<endl;
vtr.emplace_back("cornflower");
for (vector<string>::iterator it=vtr.begin(); it != vtr.end(); it++)
cout<< *it << ", ";
cout<<endl;
return 0;
}
The output is:
blue throatwort, bottlebrush, brodiaea, broom, columbine, cornflower,
The principal statement in the program is:
This expression appends another single flower name to the vector list. There are now six elements in the vector. The next code segment in the program displays the set of six elements, using a for-loop and iterators.
Emplace Back to Empty Vector
A vector must not always be created with elements. A vector can be created, empty. The emplace_back() member function can still be used to feed in elements to an empty vector. The following code illustrates this:
#include <string>
#include <iostream>
using namespace std;
int main() {
vector<string> vtr;
string str1 = vtr.emplace_back("blue throatwort");
string str2 = vtr.emplace_back("bottlebrush");
string str3 = vtr.emplace_back("brodiaea");
cout<< str1 <<endl;
cout<< str2 <<endl;
cout<< str3 <<endl;
return 0;
}
The output is:
bottlebrush
brodiaea
In the main() function, the first statement declares an empty vector. The next three statements feed the vector with three flower names, using the emplace_back() function. The code segment that follows displays the three values of the vector.
Note; a reference returned is received by the type of element.
Conclusion
A single element can be appended to a vector with the push_back() and emplace_back() vector member functions. The insert() member function can also be used in its different overloaded forms. The insert() function works with iterators.