C++ は、ファイル名から指定された場所にある情報を抽出する実装を行います。

ファイル名の例が次のとおりであるとします。

7_1_1221636_I_87_67_223_223_996.05_N-1.jpg

このうち、87、67、223、223 がそれぞれ xywh です。

xywh 情報は、次のコードを使用して抽出できます。

#include <iostream>
#include <regex>
#include <string>


std::string filename = "7_1_1221636_I_87_67_223_223_996.05_N-1.jpg";

int x = 0;
int y = 0;
int w = 0;
int h = 0;

// 创建一个正则表达式模式,用于匹配连续的四个数字,由下划线分隔
std::regex pattern(R"(\d+_\d+_\d+_\d+)");
// 创建一个std::smatch对象matches,用于存储匹配结果
std::smatch matches;

// 使用std::regex_search函数在字符串filename中查找匹配的子字符串,并将匹配结果存储在matches中
if (std::regex_search(filename, matches, pattern)) 
{
	std::string xywh = matches[0];
    // 创建一个std::istringstream对象iss,并将xywh作为其构造函数的参数,用于将xywh字符串转换为输入流
	std::istringstream iss(xywh);
	std::string xStr, yStr, wStr, hStr;
    // 使用std::getline函数从输入流iss中按下划线分隔提取子字符串,并分别存储到对应的字符串变量中
	std::getline(iss, xStr, '_');
	std::getline(iss, yStr, '_');
	std::getline(iss, wStr, '_');
	std::getline(iss, hStr, '_');
    // 使用std::stoi函数将提取的子字符串转换为整数类型,并将结果存储到对应的变量中
	x = std::stoi(xStr);
	y = std::stoi(yStr);
	w = std::stoi(wStr);
	h = std::stoi(hStr);
}

おすすめ

転載: blog.csdn.net/qq_38964360/article/details/135261520