c++笔记:vector转数组

二种转换方式,一种是直接获得vector首地址的指针,别一种再copy一份

	vector<char*> vecChar;
	char* p1 = "p12113";
	vecChar.push_back(p1);
	char* p2 = "fdsafdsa";
	vecChar.push_back(p2);
	char* p3 = "sasssssss";
	vecChar.push_back(p3);
	char** a1 = &vecChar[0];
	for (int z = 0; z < vecChar.size(); z++) {
		cout << "pointer: " << *(a1 + z) << " "<< z << " " << vecChar[z] << endl;
	}
	char* arr[vecChar.size()];
	std::copy(vecChar.begin(), vecChar.end(), arr);
	for (int z = 0; z < vecChar.size(); z++) {
		cout << "copy: " << arr[z] << " "<< z << " " << vecChar[z] << endl;
	}

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据