C++ Basic Skills: 36 – Vectors

By | 2015년 10월 13일

#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

int main()
{
    vector<int>  myVector(5,0); //{0,0,0,0,0}
    myVector.push_back(10);//{0,0,0,0,0,10}
    myVector.pop_back(); //{0,0,0,0,0}

    for(int i=0;i<myVector.size();i++)
    {
        myVector[i] =i;
    }

    cout << "third element:" << myVector[3] << endl;
    cout << "Last element:" << myVector.back() << endl;

   // myVector.erase(myVector.begin() +3,myVector.begin() + 4);
    cout << "-----------" << endl;
   vector<int> newVector; //{}

   for(int i=0;i<32;i++)
{
    newVector.push_back(i);
    cout << "#" << i << " size: " << newVector.size();
    cout << ", capacity: " << newVector.capacity() << endl;
}
    return 0;
}

third element:3
Last element:4
-----------
#0 size: 1, capacity: 1
#1 size: 2, capacity: 2
#2 size: 3, capacity: 4
#3 size: 4, capacity: 4
#4 size: 5, capacity: 8
#5 size: 6, capacity: 8
#6 size: 7, capacity: 8
#7 size: 8, capacity: 8
#8 size: 9, capacity: 16
#9 size: 10, capacity: 16
#10 size: 11, capacity: 16
#11 size: 12, capacity: 16
#12 size: 13, capacity: 16
#13 size: 14, capacity: 16
#14 size: 15, capacity: 16
#15 size: 16, capacity: 16
#16 size: 17, capacity: 32
#17 size: 18, capacity: 32
#18 size: 19, capacity: 32
#19 size: 20, capacity: 32
#20 size: 21, capacity: 32
#21 size: 22, capacity: 32
#22 size: 23, capacity: 32
#23 size: 24, capacity: 32
#24 size: 25, capacity: 32
#25 size: 26, capacity: 32
#26 size: 27, capacity: 32
#27 size: 28, capacity: 32
#28 size: 29, capacity: 32
#29 size: 30, capacity: 32
#30 size: 31, capacity: 32
#31 size: 32, capacity: 32

Process returned 0 (0x0)   execution time : 0.318 s
Press any key to continue.

Print Friendly, PDF & Email
Category: C++