参考 《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. Codeforces C. NP-Hard Problem 搜索

    C. NP-Hard Problem time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

  2. 2018.08.20 loj#117. 有源汇有上下界最小流(模板)

    传送门 这题真有意思... 先是有一个点T的我怀疑人生. 然后学大佬们封装了我的dinic就莫名其妙的过了??? 所以说锅给谁好呢? 给dinic吧... 解法就是先求出一段可行流,然后从t到s加一条 ...

  3. hdu -1114(完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 思路:求出存钱罐装全部装满情况下硬币的最小数量,即求出硬币的最小价值.转换为最小背包的问题. # ...

  4. gj11 多线程、多进程和线程池编程

    11.1 python中的GIL # coding=utf-8 # gil global interpreter lock (cpython) # python中一个线程对应于c语言中的一个线程 # ...

  5. BZOJ 1005 [HNOI2008]明明的烦恼 (Prufer编码 + 组合数学 + 高精度)

    1005: [HNOI2008]明明的烦恼 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 5786  Solved: 2263[Submit][Stat ...

  6. HDU 2561 第二小整数 (排序)

    题意:中文题. 析:输入后,排一下序就好. 代码如下: #include <iostream> #include <cstdio> #include <algorithm ...

  7. Spring MVC controller 被执行两次

    interceptor 被执行两次 后来发现 时controller被执行两次 后来发现是jsp页面有个: <img src="#" > 导致被执行两次. 解决方案:去 ...

  8. /usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory

    https://stackoverflow.com/questions/39111930/usr-include-boost-python-detail-wrap-python-hpp5023-fat ...

  9. Windows10+Python3+BeautifulSoup4 安装

    用正则表达式来提取网页中的内容是相当麻烦的,这里介绍一个可以从HTML或XML文件中提取数据的Python库:Beautiful Soup.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的 ...

  10. 区间 桂林电子科技大学第三届ACM程序设计竞赛

    链接:https://ac.nowcoder.com/acm/contest/558/E 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...