【C++】项目记录...

代码片段

  • C++项目总结
    • 以字符串中间的空格分割控制台输入
    • 分割文件路径
    • 读取文件内容并据此创建新的文件夹
    • 从文件中读取像素坐标,并排序

C++项目总结

以字符串中间的空格分割控制台输入

以字符串中间的空格分割控制台输入

	std::cout << "\nInput a new image save path : ";
	string new_path;
	getline(cin, new_path);
	std::cout << "New path : " << new_path << "\\left (\\right)" << endl;

	vector<string> image_wh_split;
	image_wh_split = split(img_wh);
	image_width = stoi(image_wh_split[0]); // string -> int
	image_height = stoi(image_wh_split[1]);
	std::cout << "Image size : (" << image_width << " * " << image_height << ")" << endl;

	vector<string> split(string str) {  // 分割控制台输入 以字符串中间的空格
		int size = str.size();
		vector<string> ans;
		int j = 0;
		for (int i = 0; i < size; i++) {
			if (str[i] == ' ') {
				ans.push_back(str.substr(j, i - j));
				j = i + 1;
			}
		}
		ans.push_back(str.substr(j, size - j));	
		return ans;
	}

分割文件路径

分割文件路径

	// 输出文件夹处理	
	vector<string> pathset = GetDirectoryName(folder); //磁盘名 路径名 文件名 后缀名
	string szDrive, szDir, szFname, szExt;
	szDrive = pathset[0];
	szDir = pathset[1];
	szFname = pathset[2];
	szExt = pathset[3];
	
	vector<string> GetDirectoryName(string fileAbsolutePath)
	{
		char szDrive[_MAX_DRIVE] = { 0 };   //磁盘名
		char szDir[_MAX_DIR] = { 0 };       //路径名
		char szFname[_MAX_FNAME] = { 0 };   //文件名
		char szExt[_MAX_EXT] = { 0 };       //后缀名
		_splitpath(fileAbsolutePath.c_str(), szDrive, szDir, szFname, szExt); //分解路径
		/*std::cout << szDrive << std::endl;
		std::cout << szDir << std::endl;
		std::cout << szFname << std::endl;
		std::cout << szExt << std::endl;*/
		vector<string> set;//szDrive, szDir, szFname, szExt;	
		set.push_back(szDrive);
		set.push_back(szDir);
		set.push_back(szFname);
		set.push_back(szExt);
		return set;
	}

读取文件内容并据此创建新的文件夹

读取文件内容并据此创建新的文件夹

	ifstream myfile("x.txt");
	string temp;
	vector<vector<string>> result;
	vector<string> res;
	while (getline(myfile, temp))
	{
		stringstream input(temp);
		string out;
		while (input >> out) {
			res.push_back(out);
		}
		result.push_back(res);
		res.clear();
	}
	
	for (int i = 0; i < result.size(); i++) {
		for (int j = 0; j < result[i].size(); j++) {
			vector<string> imgpathset = GetDirectoryName(result[i][j]);
			vector<string> imgpath_existpaths;
			
			string sigal = "";
			for (int sizei = szDir.size(); sizei < imgpathset[1].size(); sizei++) {				

				if (imgpathset[1][sizei] == '\\') {
					imgpath_existpaths.push_back(sigal);					
					sigal = "";
					continue;
				}
				else
				{
					string exist(1, imgpathset[1][sizei]);
					sigal = sigal + exist;
				}
			}			
			std::string file_pathleft, file_pathright;
			file_pathleft = new_path + "/left_rectify/";
			file_pathright = new_path + "/right_rectify/";
			string left_save, right_save;
			//if (0 != _access(new_path.c_str(), 0) || 0 != _access(file_pathleft.c_str(), 0) || 0 != _access(file_pathright.c_str(), 0))
			//{
			_mkdir(new_path.c_str());
			// if this folder not exist, create a new one.
			_mkdir(file_pathleft.c_str());// 返回 0 表示创建成功,-1 表示失败		
			_mkdir(file_pathright.c_str());
			for (int i = 0; i < imgpath_existpaths.size(); i++) {
				file_pathleft += imgpath_existpaths[i];
				file_pathright += imgpath_existpaths[i];
				file_pathleft += "\\";
				file_pathright += "\\";
				_mkdir(file_pathleft.c_str());
				_mkdir(file_pathright.c_str());
			}
			std::cout << "\n输出文件夹创建成功!" << endl;

从文件中读取像素坐标,并排序

从文件中读取像素坐标,并排序

	// An highlighted block
	vector<pair<int, int>> points; // 存入103个特征坐标点
	struct stat buffer;  // 判断 是否存在 pnt文件
	//return (stat(pnt_path.c_str(), &buffer) == 0);
	if (stat(pnt_path.c_str(), &buffer) != 0) {
		std::cout << "\npnt_path : " << pnt_path << " 不存在,next" << endl;
		return points; // 返回空
	}

	int temp1 = 0, temp2 = 0;
	ifstream circle;
	circle.open(pnt_path);

	//!circle.eof() 用来控制什么时候读取终止
	for (int i = 0; !circle.eof(); i++)
	{
		if (i == 0) {
			int temp103;
			circle >> temp103;
			//std::cout << "\n读取 : " << temp103 << endl;
			continue;
		}
		//读取txt文件的坐标值
		circle >> temp1 >> temp2;
		//std::cout << "\n读取 : " << temp1 << ", " << temp2 << endl;
		points.emplace_back(temp1, temp2);
	}
	points.pop_back(); // 多出最后一个
	// find min max x y
	vector<int> x_point, y_point;
	for (int size = 0; size < points.size(); size++) {
		x_point.emplace_back(points[size].first);
		y_point.emplace_back(points[size].second);
	}
	sort(x_point.begin(), x_point.end());
	sort(y_point.begin(), y_point.end());

你可能感兴趣的:(C++,c++,开发语言,算法,1024程序员节)