c++ vector指针访问vector元素的两种方法

 方法一:使用指针

#include 
#include 
using namespace std;

int main()
{
	vector* str;
	vector s;
	str = &s;
	str->push_back("123");
	str->push_back("456");
	cout << str->back() << endl;
	str->pop_back();
	size_t size = str->size();
	for (size_t i = 0; i < size; i++)
	{
		cout << str->at(i) << endl;
	}
}

方法二:使用迭代器 

#include 
#include 
using namespace std;

int main()
{
	vector s;
	vector *p = &s;
	s.push_back(1);
 
	for (vector::iterator it = p->begin(); it != p->end(); it++)
		cout << *it<

你可能感兴趣的:(C++)