C++ STL容器使用迭代器/指針的初始化,C++數組複製

容器使用迭代器/指針的初始化

C++ STL是可以根據指針範圍或迭代器範圍進行初始化的,常用用法如下:

	vector<int> v1{ 1,2,3 };
	set<int> set1{ 1,2,3 };
	int a[3] = { 1,2,3 };
	double b[3] = { 1.1,2.1,3.1 };
	vector<int> v2(v1.begin(), v1.end()); //內容爲1 2 3
	vector<int> v3(set1.begin(), set1.end());//內容爲1 2 3
	vector<int> v4(a, a + 3);//內容爲1 2 3
	vector<int> v5(b, b + 3);//內容爲1 2 3

數組的複製

(1)按照上述方法,利用數組指針初始化一個vector即可。
(2)memcpy函數

void * memcpy ( void * destination, const void * source, size_t num );
destination是目標數組首地址,source是資源數組首地址,num是需要copy的字節數

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