Demonstrating the four ways that vectors can be created.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<char> a; // empty
vector<char> b(5, 'X'); // size and initialize
vector<char> c(b); // initialize with another vector
int i;
for(i = 0; i <5; i++)
a.push_back('A'+i);
// create a vector from a range
vector<char> d(a.begin()+1, a.end()-1);
for(i = 0; i <a.size(); i++)
cout << "a[" << i << "]: " << a[i] << " ";
cout << "\n\n";
for(i = 0; i <b.size(); i++)
cout << "b[" << i << "]: " << b[i] << " ";
cout << "\n\n";
for(i = 0; i <c.size(); i++)
cout << "c[" << i << "]: " << c[i] << " ";
cout << "\n\n";
for(i = 0; i <d.size(); i++)
cout << "d[" << i << "]: " << d[i] << " ";
return 0;
}
Related examples in the same category