C++11 vector之vector::data

索卓
2023-12-01

// reference: http://www.cplusplus.com/reference/vector/vector/data/
// 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;
}
/*
Output:
myvector contains: 10 20 0 100 0
*/

强烈建议大家写完代码给出输出结果,有些博客就一些代码,什么结果或者能不能跑都不管了。

 类似资料: