// 参数读取类
class ParameterReader
{
public:
ParameterReader( string filename="./parameters.txt" )
{
ifstream fin( filename.c_str() );
if (!fin)
{
cerr<<"parameter file does not exist."<<endl;
return;
}
while(!fin.eof())
{
string str;
getline( fin, str );
if (str[0] == '#')
{
// 以‘#’开头的是注释
continue;
}
int pos = str.find("=");
if (pos == -1)
continue;
string key = str.substr( 0, pos );
string value = str.substr( pos+1, str.length() );
data[key] = value;
if ( !fin.good() )
break;
}
}
string getData( string key )
{
map<string, string>::iterator iter = data.find(key);
if (iter == data.end())
{
cerr<<"Parameter name "<<key<<" not found!"<<endl;
return string("NOT_FOUND");
}
return iter->second;
}
public:
map<string, string> data;
};
知识点 string中的find()输入值为string,输出是查到的下标,为数值类型。如果没查到则返回npos。
//string (1)
size_type find (const basic_string& str, size_type pos = 0) const noexcept;
//c-string (2)
size_type find (const charT* s, size_type pos = 0) const;
//buffer (3)
size_type find (const charT* s, size_type pos, size_type n) const;
//character (4)
size_type find (charT c, size_type pos = 0) const noexcept;
int main() { //测试size_type find (charT c, size_type pos = 0) const noexcept; string st1("babbabab"); cout << st1.find('a') << endl;//1 由原型知,若省略第2个参数,则默认从位置0(即第1个字符)起开始查找 cout << st1.find('a', 0) << endl;//1 cout << st1.find('a', 1) << endl;//1 cout << st1.find('a', 2) << endl;//4 在st1中,从位置2(b,包括位置2)开始,查找字符a,返回首次匹配的位置,若匹配失败,返回npos cout << st1.rfind('a',7) << endl;//6 关于rfind,后面讲述 cout << st1.find('c', 0) << endl;//4294967295 cout << (st1.find('c', 0) == -1) << endl;//1 cout << (st1.find('c', 0) == 4294967295) << endl;//1 两句均输出1,原因是计算机中-1和4294967295都表示为32个1(二进制) cout << st1.find('a', 100) << endl;//4294967295 当查找的起始位置超出字符串长度时,按查找失败处理,返回npos //测试size_type find (const basic_string& str, size_type pos = 0) const noexcept; string st2("aabcbcabcbabcc"); string str1("abc"); cout << st2.find(str1, 2) << endl;//6 从st2的位置2(b)开始匹配,返回第一次成功匹配时匹配的串(abc)的首字符在st2中的位置,失败返回npos //测试size_type find (const charT* s, size_type pos = 0) const; cout << st2.find("abc", 2) << endl; //6 同上,只不过参数不是string而是char* //测试size_type find (const charT* s, size_type pos, size_type n) const; cout << st2.find("abcdefg", 2, 3) << endl;//6 取abcdefg得前3个字符(abc)参与匹配,相当于st2.find("abc", 2) cout << st2.find("abcbc", 0, 5) << endl;//1 相当于st2.find("abcbc", 0) cout << st2.find("abcbc", 0, 6) << endl;//4294967295 第3个参数超出第1个参数的长度时,返回npos return 0; }
FRAME readFrame( int index, ParameterReader& pd )
{
FRAME f;
string rgbDir = pd.getData("rgb_dir");
string depthDir = pd.getData("depth_dir");
string rgbExt = pd.getData("rgb_extension");
string depthExt = pd.getData("depth_extension");
stringstream ss;
ss<<rgbDir<<index<<rgbExt;
string filename;
ss>>filename;
f.rgb = cv::imread( filename );
ss.clear();
filename.clear();
ss<<depthDir<<index<<depthExt;
ss>>filename;
f.depth = cv::imread( filename, -1 );
return f;
}
参数读取和处理
int min_inliers = atoi( pd.getData("min_inliers").c_str() );
double max_norm = atof( pd.getData("max_norm").c_str() );
读取的数据可以转为数据类型为int double
解决的问题是:(1)如何从一个有固定格式的参数文件中读取各个参数的数据,然后使用Map将数据存储起来,并提供一个接口来进行查询操作。
(2)其查询结果为string,需要c_str()来转换成字符,并用atoi或者atof来将查询结果转为需要的类型。
int main() { //测试size_type find (charT c, size_type pos = 0) const noexcept; string st1("babbabab"); cout << st1.find('a') << endl;//1 由原型知,若省略第2个参数,则默认从位置0(即第1个字符)起开始查找 cout << st1.find('a', 0) << endl;//1 cout << st1.find('a', 1) << endl;//1 cout << st1.find('a', 2) << endl;//4 在st1中,从位置2(b,包括位置2)开始,查找字符a,返回首次匹配的位置,若匹配失败,返回npos cout << st1.rfind('a',7) << endl;//6 关于rfind,后面讲述 cout << st1.find('c', 0) << endl;//4294967295 cout << (st1.find('c', 0) == -1) << endl;//1 cout << (st1.find('c', 0) == 4294967295) << endl;//1 两句均输出1,原因是计算机中-1和4294967295都表示为32个1(二进制) cout << st1.find('a', 100) << endl;//4294967295 当查找的起始位置超出字符串长度时,按查找失败处理,返回npos //测试size_type find (const basic_string& str, size_type pos = 0) const noexcept; string st2("aabcbcabcbabcc"); string str1("abc"); cout << st2.find(str1, 2) << endl;//6 从st2的位置2(b)开始匹配,返回第一次成功匹配时匹配的串(abc)的首字符在st2中的位置,失败返回npos //测试size_type find (const charT* s, size_type pos = 0) const; cout << st2.find("abc", 2) << endl; //6 同上,只不过参数不是string而是char* //测试size_type find (const charT* s, size_type pos, size_type n) const; cout << st2.find("abcdefg", 2, 3) << endl;//6 取abcdefg得前3个字符(abc)参与匹配,相当于st2.find("abc", 2) cout << st2.find("abcbc", 0, 5) << endl;//1 相当于st2.find("abcbc", 0) cout << st2.find("abcbc", 0, 6) << endl;//4294967295 第3个参数超出第1个参数的长度时,返回npos return 0; }