vector轉換爲char*[]

int ReqSubMarketData(std::vector<std::string>& vecInstrucment)
{
	char** destination = new char*[vecInstrucment.size() + 1];
	MakeCString(vecInstrucment, destination);
        // param char* ppD[], int nSize
	SubscribeMarketData(destination, static_cast<int>(vecInstrucment.size()));
	
	// 釋放資源
	for (int n = 0; n < static_cast<int>(vecInstrucment.size()); n++)
	{
		delete[] (destination[n]);
	}

	delete[] destination;
	destination = NULL;

	return ERR_OK;
}

void MakeCString(const std::vector<std::string>& source, char** destination)
{
	// 注意釋放內存
	for (int n = 0; n < static_cast<int>(source.size()); ++n)
	{
		destination[n] = new char[32];
		destination[n][31] = '\0';
		strncpy(destination[n], source[n].c_str(), 31);
	}

	//destination[source.size()] = NULL;
}

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