码迷,mamicode.com
首页 > 其他好文 > 详细

字符串搜索 find()

时间:2018-06-24 00:37:32      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:code   mat   line   orb   fstream   class   函数   c_str   位置   

参考 《C++ Primer Plus》中文版 P870

#include <map>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

string getData( string key, map<string, string> & data );

//using namespace std;
int main(int argc, char** argv )
{
    map<string, string> data;
    string filename = "./parameters.txt";
    ifstream fin( filename.c_str() );
/*
c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同. 
这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
*/
    if (!fin)//文件不存在
    {
        cerr<<"parameter file does not exist."<<endl;//报错
                return 0;
    }
    while(!fin.eof()) //文件是否为空
    {
                string str;
                getline( fin, str );//把每一行放到str中,不断读取
                if (str[0] == #)
                {
                    // 以‘#’开头的是注释
                    continue;
                }

                int pos = str.find("=");  // 等于号的位置, P870
                if (pos == -1)
                {
                    continue;
                }
                string key = str.substr( 0, pos );  //从下标0到pos
                string value = str.substr( pos+1, str.length() );
                data[key] = value;

                if ( !fin.good() )
                {
                    break;
                }
     }


        string value = getData("camera.cx",data);
        cout << value << endl;
}
string getData( string key ,map<string, string> & data)//成员函数 { 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; //second值,first键 }

 

读取的文件parameters.txt

# 这是一个参数文件

detector=ORB
descriptor=ORB
good_match_threshold=10

# camera
camera.cx=682.3;
camera.cy=254.9;
camera.fx=979.8;
camera.fy=942.8;
camera.scale=1000.0;

 

技术分享图片

 

字符串搜索 find()

标签:code   mat   line   orb   fstream   class   函数   c_str   位置   

原文地址:https://www.cnblogs.com/112358nizhipeng/p/9219094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!