参考 《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 ;
}
while(!fin.eof()) //文件是否为空
{
string str;
getline( fin, str );//把每一行放到str中,不断读取
if (str[] == '#')
{
// 以‘#’开头的是注释
continue;
} int pos = str.find("="); // 等于号的位置, P870
if (pos == -)
{
continue;
}
string key = str.substr( , pos ); //从下标0到pos
string value = str.substr( pos+, str.length() );
data[key] = value; if ( !fin.good() )
{
break;
}
}
float value1 = atof(getData("camera.cx",data).c_str());//上下的区别
     string value2 = getData("camera.cx",data);
        
     cout << value1 << endl;
     cout << value2 << 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键
}

str为string类型对象,str.find()返回位置int类型。

atof 是ascII to float的缩写,它将ascII字符串转换为相应的单精度浮点数,比如传入"1.234",经过处理后就返回float类型的数1.234 。类似的还有atoi 、atol、itoa、ftoa等等。

读取的文件parameters.txt

# 这是一个参数文件

detector=ORB
descriptor=ORB
good_match_threshold= # camera
camera.cx=682.3;
camera.cy=254.9;
camera.fx=979.8;
camera.fy=942.8;
camera.scale=1000.0;

float value1 = atof(getData("camera.cx",data).c_str());
  string value2 = getData("camera.cx",data);

可以在终端的输出中看到上面两句程序的区别。

字符串搜索 find()的更多相关文章

  1. Linux常用命令学习2---(文件搜索命令locate find、命令搜索命令whereis which、字符串搜索命令grep、帮助命令man)

     1.文件搜索命令:locate [文件名]    在后台数据库中按文件名搜索,搜索速度比find快,耗费资源更少    例子:locate test.txt,就会显示文件名包含 test.txt的所 ...

  2. 【ToolGood.Words】之【StringSearch】字符串搜索——基于BFS算法

    字符串搜索中,BFS算法很巧妙,个人认为BFS算法效率是最高的. [StringSearch]就是根据BFS算法并优化. 使用方法: string s = "中国|国人|zg人|fuck|a ...

  3. C#下利用正则表达式实现字符串搜索功能的方法(转)

    关键字:正则表达式.元字符.字符串.匹配: 1.正则表达式简介:正则表达式提供了功能强大.灵活而又高效的方法来处:.NET框架正则表达式并入了其他正则表达式实现的: 2.字符串搜索:正则表达式语言由两 ...

  4. Windows和linux环境下按文件名和字符串搜索命令

    Windows 1.遍历C盘下所有txt 命令:for /r c:\ %i in (*.txt) do @echo %i 注释:for 循环的意思 /r 按照路径搜索 c:\ 路径 %i in   ( ...

  5. (九)Centos之搜索命令whereis、which和字符串搜索命令grep

    一.Centos之命令搜索命令whereis与which 1.1 whereis 命令名(搜索命令所在路径及帮助文档所在位置) 选项: -b :只查找可执行文件位置 -m:只查找帮助文件 1.2 wh ...

  6. Trie——解决字符串搜索、异或最值问题

    Trie--解决字符串搜索.异或最值问题 在说到Trie之前,我们设想如下问题: 给我们1e5个由小写字母构成的不重复的字符串,每个字符串长度不超过6,之后是1e5次查询操作,每次给我们一个字符串,要 ...

  7. linq字符串搜索条件,排序条件-linq动态查询语句 Dynamic LINQ

    在做搜索和排序的时候,往往是前台传过来的字符串做条件,参数的数量还不定,这就需要用拼sql语句一样拼linq语句.而linq语句又是强类型的,不能用字符串拼出来. 现在好了,有个开源的linq扩展方法 ...

  8. Substrings 子字符串-----搜索

    Description You are given a number of case-sensitive strings of alphabetic characters, find the larg ...

  9. ACM字符串处理算法经典:字符串搜索

    语法:result=strfind(char str[],char key[]); 参数: str[]:在这个源字符串查找操作 key[]:搜索字符串.不能为空字符串 回报值:     假设查找成功. ...

  10. Python: 字符串搜索和匹配,re.compile() 编译正则表达式字符串,然后使用match() , findall() 或者finditer() 等方法

    1. 使用find()方法 >>> text = 'yeah, but no, but yeah, but no, but yeah' >>> text.find( ...

随机推荐

  1. How to use jQuery countdown plugin

    Install We provide two installation methods: Bower bower install jquery.countdown Manual Download th ...

  2. URL编码转换函数:escape()、encodeURI()、encodeURIComponent()

          函数出现时间:                      escape()                                javascript 1.0           ...

  3. 跳出思维定势,改变交谈习惯zz

    一直以来我都是一个不折不扣的作者所划分的内向者,羞于在公众场合说话,也不愿意与陌生人交谈,甚至是与认识的人聊天,有时候也是一种痛苦,看着在办公室里夸夸其谈的同事们,我总是感觉格格不入.严格说来,我算是 ...

  4. 2018.10.19 NOIP训练 桌子(快速幂优化dp)

    传送门 勉强算一道dp好题. 显然第kkk列和第k+nk+nk+n列放的棋子数是相同的. 因此只需要统计出前nnn列的选法数. 对于前mmm%nnn列,一共有(m−1)/n+1(m-1)/n+1(m− ...

  5. 2018.09.23 codeforces 1053B. Vasya and Good Sequences(前缀和)

    传送门 考试的时候卡了一会儿. 显然这个答案只跟二进制位为1的数量有关. 还有一个显然的结论. 对于一个区间[l,r][l,r][l,r],如果其中单个数二进制位为1的数量最大值不到区间所有数二进制位 ...

  6. UVa 210 Concurrency Simulator (双端队列+模拟)

    题意:给定n个程序,每种程序有五种操作,分别为 var = constant(赋值),print var (打印), lock, unlock,end. 变量用小写字母表示,初始化为0,为程序所公有( ...

  7. MySQL终端下常用命令

    一:控制类命令 1.show variables like "%datadir%";显示注册在variables中(一个注册表key-value的格式存储数据)key能匹配%dat ...

  8. x+y+z=n的正整数解

    题:x+y+z=n,其中(n>=3),求x,y,z的正整数解的个数根据图象法:x>=1,y>=1,x+y<=n-1

  9. 基于NLP和GAN的小说影视化

    轮廓: https://tieba.baidu.com/p/1271120336 着色:

  10. (网络流 模板)A Plug for UNIX -- poj -- 1087

    链接: http://poj.org/problem?id=1087 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82835#probl ...