-
vector issue
Code:
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> intVector;
int main()
{
intVector vInt1(5);
vInt1[0]=1;
cout<<vInt1.size()<<endl;
cout<<vInt1.capacity();
return 0;
}
Output is:
5
5
which is supprising (at least for me)
I was expecting 1 5
Druring compiling I get two following warnings:
conversion from 'size_t' to 'unsigned int', possible loss of data
both for cout
I can get rid of them by casting to unsigned int
If someone can explain output.
whati is the difference between size() and capacity()
-
size() should give the number of elements, and capacity() the max number before more space must be allocated.
Maybe this would be clearer?
Code:
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> intVector;
int main()
{
intVector vInt1;
vInt1.reserve(5);
vInt1.push_back(1);
vInt1.push_back(0);
cout<<vInt1.size()<<endl;
cout<<vInt1.capacity();
return 0;
}
I suspect that the reason for the warning is that the constructor doesnt accept a single integer as an argument.
-