Vector Creation and Element Access : vector elements « vector « C++ Tutorial






#include <iostream>
 #include <string>
 #include <vector>
 using namespace std;

 class Employee
 {
 public:
        Employee();
        Employee(const string& name, const int age);
        Employee(const Employee& rhs);
        ~Employee();
  
        void    SetName(const string& name);
        string  GetName()      const;
        void    SetAge(const int age);
        int     GetAge()       const;

        Employee& operator=(const Employee& rhs);
 
private:
        string itsName;
        int itsAge;
};

Employee::Employee()
: itsName("New Employee"), itsAge(16)
{}

Employee::Employee(const string& name, const int age)
: itsName(name), itsAge(age)
{}

Employee::Employee(const Employee& rhs)
: itsName(rhs.GetName()), itsAge(rhs.GetAge())
{}

Employee::~Employee()
{}
 
void Employee::SetName(const string& name)
{
        itsName = name;
}

string Employee::GetName() const
{
        return itsName;
}

void Employee::SetAge(const int age)
{
        itsAge = age;
}

int Employee::GetAge() const
{
        return itsAge;
}

Employee& Employee::operator=(const Employee& rhs)
{
        itsName = rhs.GetName();
        itsAge = rhs.GetAge();
        return *this;
}

ostream& operator<<(ostream& os, const Employee& rhs)
{
        os << rhs.GetName() << " is " << rhs.GetAge()  << " years old";
        return os;
}

template<class T>
void ShowVector(const vector<T>& v);    // display vector properties

typedef vector<Employee>        SchoolClass;

int main()
{
        Employee Harry;
        Employee Sally("Sally", 15);
        Employee Bill("Bill", 17);
        Employee Peter("Peter", 16);

        SchoolClass    EmptyClass;
        cout << "EmptyClass:\n";
        ShowVector(EmptyClass);

        SchoolClass GrowingClass(3);
        cout << "GrowingClass(3):\n";
        ShowVector(GrowingClass);

        GrowingClass[0] = Harry;
        GrowingClass[1] = Sally;
        GrowingClass[2] = Bill;
        cout << "GrowingClass(3) after assigning students:\n";
        ShowVector(GrowingClass);

        GrowingClass.push_back(Peter);
        cout << "GrowingClass() after added 4th student:\n";
        ShowVector(GrowingClass);
 
        GrowingClass[0].SetName("Harry");
        GrowingClass[0].SetAge(18);
        cout << "GrowingClass() after Set\n:";
        ShowVector(GrowingClass);
         
        return 0;
}

template<class T>
void ShowVector(const vector<T>& v)
{
        cout << "max_size() = " << v.max_size();
        cout << "\tsize() = " << v.size();
        cout << "\tcapacity() = " << v.capacity();
        cout << "\t" << (v.empty()? "empty": "not empty");
        cout << "\n";
 
        for (int i = 0; i < v.size(); ++i)
                cout << v[i] << "\n";
 
        cout << endl;
 }








16.26.vector elements
16.26.1.Vector Creation and Element Access
16.26.2.Change element with pointer
16.26.3.Change element with const iterator
16.26.4.Accessing Elements in a Vector Using Array Semantics
16.26.5.Accessing Elements in a Vector Using Pointer Semantics (Iterators)
16.26.6.Set Element Values in a vector Using Array Semantics
16.26.7.Instantiate a vector with 10 elements (it can grow larger)
16.26.8.Instantiate a vector with 10 elements, each initialized to 90
16.26.9.Instantiate a vector to 5 elements taken from another
16.26.10.The first and last element in vector as pointed to by begin() and end() - 1
16.26.11.The first and last element in vector as pointed to by rbegin() and rend() - 1
16.26.12.Add elements to the end of vector with push_back
16.26.13.Constructing a Container That Has Identical Elements
16.26.14.Constructing a Container That Has Specified Values
16.26.15.Elements Being Created, Assigned, and Destroyed
16.26.16.Add all the elements from vector Two to the end of vector One
16.26.17.append one more element. This causes reallocation
16.26.18.Instantiate a vector with 4 elements, each initialized to 90
16.26.19.Access the elements of a vector through an iterator.