字符串搜索 find()
参考 《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()的更多相关文章
- Linux常用命令学习2---(文件搜索命令locate find、命令搜索命令whereis which、字符串搜索命令grep、帮助命令man)
1.文件搜索命令:locate [文件名] 在后台数据库中按文件名搜索,搜索速度比find快,耗费资源更少 例子:locate test.txt,就会显示文件名包含 test.txt的所 ...
- 【ToolGood.Words】之【StringSearch】字符串搜索——基于BFS算法
字符串搜索中,BFS算法很巧妙,个人认为BFS算法效率是最高的. [StringSearch]就是根据BFS算法并优化. 使用方法: string s = "中国|国人|zg人|fuck|a ...
- C#下利用正则表达式实现字符串搜索功能的方法(转)
关键字:正则表达式.元字符.字符串.匹配: 1.正则表达式简介:正则表达式提供了功能强大.灵活而又高效的方法来处:.NET框架正则表达式并入了其他正则表达式实现的: 2.字符串搜索:正则表达式语言由两 ...
- Windows和linux环境下按文件名和字符串搜索命令
Windows 1.遍历C盘下所有txt 命令:for /r c:\ %i in (*.txt) do @echo %i 注释:for 循环的意思 /r 按照路径搜索 c:\ 路径 %i in ( ...
- (九)Centos之搜索命令whereis、which和字符串搜索命令grep
一.Centos之命令搜索命令whereis与which 1.1 whereis 命令名(搜索命令所在路径及帮助文档所在位置) 选项: -b :只查找可执行文件位置 -m:只查找帮助文件 1.2 wh ...
- Trie——解决字符串搜索、异或最值问题
Trie--解决字符串搜索.异或最值问题 在说到Trie之前,我们设想如下问题: 给我们1e5个由小写字母构成的不重复的字符串,每个字符串长度不超过6,之后是1e5次查询操作,每次给我们一个字符串,要 ...
- linq字符串搜索条件,排序条件-linq动态查询语句 Dynamic LINQ
在做搜索和排序的时候,往往是前台传过来的字符串做条件,参数的数量还不定,这就需要用拼sql语句一样拼linq语句.而linq语句又是强类型的,不能用字符串拼出来. 现在好了,有个开源的linq扩展方法 ...
- Substrings 子字符串-----搜索
Description You are given a number of case-sensitive strings of alphabetic characters, find the larg ...
- ACM字符串处理算法经典:字符串搜索
语法:result=strfind(char str[],char key[]); 参数: str[]:在这个源字符串查找操作 key[]:搜索字符串.不能为空字符串 回报值: 假设查找成功. ...
- Python: 字符串搜索和匹配,re.compile() 编译正则表达式字符串,然后使用match() , findall() 或者finditer() 等方法
1. 使用find()方法 >>> text = 'yeah, but no, but yeah, but no, but yeah' >>> text.find( ...
随机推荐
- [BAT]远程执行或停止计划任务
执行 schtasks /run /tn "IPADForAdvisor_QA_APITest" /s SZPCWIN2K801 /u msdomain1\jzhang6 /p j ...
- winsock select 学习代码(1)
// SelectCli.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <winsock2.h> #incl ...
- 8.14 右键自定义菜单 更加iframe 内容高度调整 iframe高度 js定时
<div class="main_contain" id="z_div" style="position: relative;"> ...
- ubuntu系统下安装pyspider:搭建pyspider服务器新手教程
首先感谢“巧克力味腺嘌呤”的博客和Debian 8.1 安装配置 pyspider 爬虫,本人根据他们的教程在ubuntu系统中进行了实际操作,发现有一些不同,也出现了很多错误,因此做此教程,为新手服 ...
- spring mvc 用cookie和拦截器实现自动登录(/免登录)
Cookie/Session机制详解:http://blog.csdn.net/fangaoxin/article/details/6952954 SpringMVC记住密码功能:http://blo ...
- 2018.07.28 uoj#164. 【清华集训2015】V(线段树)
传送门 线段树好题. 要求支持的操作: 1.区间变成max(xi−a,0)" role="presentation" style="position: rela ...
- hadoop 学习之异常篇
本文旨在给予自己在学习hadoop过程中遇到的问题的一个记录和解决方法. 一. copyFromLocal: java.io.IOException: File /user/hadoop/slaves ...
- 链家笔试链家——找寻最小消费获取最大平均分java
链家找寻最小消费获取最大平均分 输入: 5 5 4#表示科目数n,每科最大分值r,平均分avg 5 2#每科的实际得分,分数加1分的消耗的能量 4 7 3 1 3 2 2 5 输出: 4 #到达n*a ...
- kallinux2.0安装网易云音乐
安装 dpkg -i netease-cloud-music_1.0.0_amd64.kali2.0(yagami).deb apt-get -f install dpkg -i netease-cl ...
- 9) 依赖查询 & 镜像站
依赖查询 http://mvnrepository.com/ Maven仓库查询 http://search.maven.org 仓库 加上这两个,如果使用中央仓库 Eclipse 极有可能会卡死 & ...