vector 轉換爲 int*

// libvtftp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <vector>

void ShowIntArray(int* p, int nSize)
{
	if (NULL == p || nSize <= 0)
	{
		return;
	}

	TCHAR szBuff[36];
	::memset(szBuff, 0, sizeof(szBuff));

	for (int i = 0; i < nSize; i++)
	{
		int j = *(p + i);
		std::cout <<j;

		memset(szBuff, 0, sizeof(szBuff));

		std::cout << std::endl;
	}

}

int _tmain(int argc, _TCHAR* argv[])
{
	typedef std::vector<int> vtInt;

	vtInt a;
	for (int i = 0; i < 10; i++)
	{
		a.push_back(i);
	}

	// 假設不知道vector容器大小
	int*p = NULL;
	p = new int[a.size()];
	
	for (int i = 0; i < a.size(); i++)
	{
		p[i] = a[i];
	}

	// 調用C的庫函數
	ShowIntArray(p, a.size());

	delete []p;
	p = NULL;

	system("pause");

	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章