I found an example of std::vector in http://ift.tt/1FIXhye.
/ vector::data
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector (5);
int* p = myvector.data();
*p = 10;
++p;
*p = 20;
p[2] = 100;
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector.size(); ++i)
std::cout << ' ' << myvector[i];
std::cout << '\n';
return 0;
}
and the result is
myvector contains: 10 20 0 100 0
My question may be silly but I don't really understand what happened here. We got a direct pointer to the memory of the vector. Then we assign the value 10 for the first element (index 0), move to the second element and assign the value 20 to it (index 1). Finally, we assign the value 100 for the third element (index 2). Should the answer be as follow?
10 20 100
Aucun commentaire:
Enregistrer un commentaire