C++之vector標準庫

C++之vector標準庫

#include <iostream>
#include <vector>

/*
	vector: 表示一組“類型相同”的對象的“集合”,集合中每個對象都有“索引”與之對應,
	        vector常被稱作“容器(container)”
*/

using namespace std;

void testVector() {
	cout << "test vector:" << endl;
	vector<int> ivec{1, 2, 32};
	for (int i = 0; i < 3; i++) {
		cout << ivec[i] << " ";
	}
	cout << endl;

	string str;
	vector<string> vecStr;
	int i = 0;
	while (cin >> str)
	{
		vecStr.push_back(str);
		i++;
		if (i >= 5) break;
	}
	for (auto str : vecStr) {
		// str 是每個元素
		cout << str << " ";
	}
	cout << endl;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章