The Standard Template Library Common Classes
Vector
Example 1:
Create a vector that holds string values. Print the size and first, second, third and last elements. Finally using a for each loop print all the elements to the console screen.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> strVector = { "john", "Paul", "Jesse", "Patty" };
cout << "Size of the Vector is " << strVector.size() << endl;
cout << "The first item in the vector
is " << strVector.front() << endl;
cout << "The second item in the
vector is " << strVector[1] << endl; //using substring
cout << "The second item in the
vector is " << strVector.at(2) << endl; //using at()
cout << "The last item in the vector
is " << strVector.back() << endl;
for (auto&&
s : strVector)
{
cout << s <<endl;
}
system("pause");
return 0;
}
Comments
Post a Comment