1. 程式人生 > >二維vector容器讀取txt坐標

二維vector容器讀取txt坐標

post oca pre positive i++ cati cat cout begin

template <class vector>
struct HeadLocation{
	vector x;
	vector y;
};

vector<HeadLocation<int> > gt_loc_;  //二維vector容器

void ReadLocationFromTextFile(const string filename) {
	cout << "Opening file " << filename << endl;
	ifstream infile(filename.c_str());
	if (!infile){ printf("不存在此文本文件!"); };
	int num_crowd;
	infile >> num_crowd;
	if (num_crowd <= 0){ cout << "Number of crowd must be positive!\n"; };
	gt_loc_.clear(); //size = 0, capicity =?
	gt_loc_.resize(num_crowd); //size = num_crowd(行)

	for (int i = 0; i < num_crowd; i++) {
		/*HeadLocation<float> location_t;
		HeadLocation<int> location(location_t.begin(), location_t.end());*/
		HeadLocation<int> location;
		infile >> location.x >> location.y;

		for (int j = 0; j < 3; ++j) {
			location.x = (location.x - 1) / 2;
			location.y = (location.y - 1) / 2;
		}
		gt_loc_[i] = location;
	}
	infile.close(); // 關閉文件
}

  

二維vector容器讀取txt坐標