1111 WordReplace

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string sa,sb,s;
	while(getline(cin,s))
	{
		getline(cin,sa);
		getline(cin,sb);
		int begin=0;
		begin=s.find(sa,begin);
		int l=sa.length();
		while(begin!=-1)
		{
			s.replace(begin,l,sb);
			begin=s.find(sa,begin);
		}
		cout<<s<<endl;
	}
	return 0;
}

重点:getline(),find(),replace()函数

1.一个有用的string IO操作:getling。这个函数接受两个参数:一个输入流对象和一个string对象。getline函数从输入流的下一行读取,并保存读取的内容到string中,但不包括换行符。和输入操作符不一样的是,getline并不忽略行开头的换行符。只要getline遇到换行符,即便它是输入的第一个字符,getline也将停止读入并返回。如果第一个字符就是换行符,则string参数将被置为空string。

getline函数将istream参数作为返回值,和输入操作符一样也把它用作判断条件。例如:

int main()
{
string line;
// read line at time until end-of-file
while (getline(cin, line))
   cout << line << endl;
return 0;
}

由于line不含换行符,若要逐行输出需要自行添加。照常,用endl来输出一个换行符来刷新输出缓冲区。

注解:由于getline函数返回时丢弃换行符,换行符将不会存储在string对象中。

2.find()

  1.  find函数返回类型 size_type  
  2. string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");  
  3. string flag;  
  4. string::size_type position;  
  5.   
  6. //find 函数 返回jk 在s 中的下标位置   
  7. position = s.find("jk");  
  8.  if (position != s.npos)  //如果没找到,返回一个特别的标志c++中用npos表示,我这里npos取值是4294967295,  
  9.  {  
  10.   cout << "position is : " << position << endl;  
  11.  }  
  12.  else  
  13.  {  
  14.   cout << "Not found the flag" + flag;  
  15.  }   
[cpp]  view plain copy
  1. //find 函数 返回flag 中任意字符 在s 中第一次出现的下标位置  
  2.  flag = "c";  
  3.  position = s.find_first_of(flag);  
  4.  cout << "s.find_first_of(flag) is : " << position << endl;  
[cpp]  view plain copy
  1. //从字符串s 下标5开始,查找字符串b ,返回b 在s 中的下标  
  2. position=s.find("b",5);  
  3. cout<<"s.find(b,5) is : "<<position<<endl;  

 

[cpp]  view plain copy
  1. //查找s 中flag 出现的所有位置。  
  2.  flag="a";  
  3.  position=0;  
  4.  int i=1;  
  5.  while((position=s.find_first_of(flag,position))!=string::npos)  
  6.  {  
  7.   //position=s.find_first_of(flag,position);  
  8.   cout<<"position  "<<i<<" : "<<position<<endl;  
  9.   position++;  
  10.   i++;  
  11.  }  
[cpp]  view plain copy
  1.    
[cpp]  view plain copy
  1. //查找flag 中与s 第一个不匹配的位置  
  2. flag="acb12389efgxyz789";  
  3. position=flag.find_first_not_of (s);  
  4. cout<<"flag.find_first_not_of (s) :"<<position<<endl;  
[cpp]  view plain copy
  1.    
[cpp]  view plain copy
  1.  //反向查找,flag 在s 中最后出现的位置  
  2.  flag="3";  
  3.  position=s.rfind (flag);  
  4.  cout<<"s.rfind (flag) :"<<position<<endl;  
3.replace()函数

 
 
  1. basic_string::max_size  

C++ replace()函数返回string 能放的最大元素个数。(不同于capacity)

 
 
  1. size _ type max _ size( ) const;   
  2. basic_string <char>::size_type cap, max;   
  3. cap = s.capacity ( );   
  4. max = s.max_size ( ); // max=4294967294.   
  5. basic_string::rfind  

寻找给定的string。返回找到的第一个string 下标值;如果没找到则返回npos。

与find 不同的是:rfind 默认从npos 开始找。其他相同。

 
 
  1. basic_string::replace  

将原string 中的元素或子串替换。返回替换后的string。

(1)用string 或C-string 代替操作string 中从 _Pos1 开始的 _Num1 个字符

 
 
  1. basic _ string& replace( size _ type _Pos1 ,
    size _ type _Num1 , const value _ type* _Ptr );   
  2. basic _ string& replace(size _ type _Pos1 ,
    size _ type _Num1 ,const basic _ string _Str );   
  3. string a,b;   
  4. string s ( "AAAAAAAA" );   
  5. string s1p ( "BBB" );   
  6. const char* cs1p = "CCC" ;   
  7. a = s.replace ( 1 , 3 , s1p ); // s= ” ABBBAAAA ”   
  8. b = s.replace ( 5 , 3 , cs1p ); // s= ” ABBBACCC ”  

(2)用C++ replace()函数中从 _Pos2 开始的 _Num2 个字符,代替操作string 中从 _Pos1 开始的 _Num1 个字符

用C-string 中的 _Num2 个字符,代替操作string 中从 _Pos1 开始的 _Num1 个字符

 
 
  1. basic _ string& replace( size _ type _Pos1 , 
    size _ type _Num1 , const basic _ string& _Str ,   
  2. size _ type _Pos2 , size _ type );   
  3. basic _ string& replace( size _ type _Pos1 , size _ type _Num1 ,   
  4. const value _ type* _Ptr , size _ type _Num2 );   
  5. string a, b;   
  6. string s ( "AAAAAAAA" );   
  7. string s2p ( "BBB" );   
  8. const char* cs2p = "CCC";   
  9. a = s.replace ( 1 , 3 , s2p , 1 , 2 ); // s= ” ABBAAAA ”   
  10. b = s.replace ( 4 , 3 , cs2p , 1 ); // s= ” ABBAC ”  

(3)用 _Count 个character _Ch , 代替操作string 中从 _Pos1 开始的 _Num1 个字符

 
 
  1. basic _ string& replace( size _ type _Pos1 , size _ type _Num1 ,   
  2. size _ type _Count , value _ type _Ch );   
  3. string result;   
  4. string s ( "AAAAAAAA" );   
  5. char ch = 'C';   
  6. result = s.replace ( 1 , 3 , 4 , ch ); // s= ” ACCCCAAAA ”  

(4)用string 或C-string ,代替操作string 中从 First0 到 Last0 的字符

 
 
  1. basic _ string&replace(iterator First0 ,iterator Last0 , 
    const basic _ string& _Str );   
  2. basic _ string&replace(iterator First0 ,iterator _Last0 , 
    const value _ type* _Ptr );   
  3. string s ( "AAAAAAAA" ); string s4p ( "BBB" );   
  4. const char* cs4p = "CCC";   
  5. basic_string<char>::iterator IterF0, IterL0;   
  6. IterF0 = s.begin ( ); IterL0 = s.begin ( ) + 3;   
  7. string a, b;   
  8. a = s.replace ( IterF0 , IterL0 , s4p ); // s= ” BBBAAAAA ”   
  9. b = s.replace ( IterF0 , IterL0 , cs4p ); // s= ” CCCAAAAA ”  

(5)用C++ replace()函数中从 _Pos2 开始的 _Num2 个字符,代替操作string 中从 First0 到 Last0 的字符

用C-string 中的 _Num2 个字符,代替操作string 中从 First0 到 Last0 的字符

 
 
  1. basic _ string& replace( iterator _First0 , iterator _Last0 ,   
  2. const value _ type* _Ptr , size _ type _Num2 );   
  3. template<class InputIterator> basic _ string& replace(   
  4. iterator _First0 , iterator _Last0 ,   
  5. InputIterator _First , InputIterator _Last );   
  6. IterF3 = s.begin ( ) + 1; IterL3 = s.begin ( ) + 3;   
  7. IterF4 = s.begin ( ); IterL4 = s.begin ( ) + 2;   
  8. a = s.replace ( IterF3 , IterL3 , IterF4 , IterL4 );   
  9. b = s.replace ( IterF1 , IterL1 , cs5p , 4 );  

(6)用 _Count 个character _Ch , 代替操作string 中从 First0 到 Last0 的字符

 
 
  1. basic _ string& replace( iterator _First0 , iterator _Last0 ,   
  2. size _ type _Count , value _ type _Ch );   
  3. a = s.replace ( IterF2 , IterL2 , 4 , ch );   
  4. basic_string::swap  

交换两个string。

 
 
  1. void swap( basic _ string& _Str );   
  2. s1.swap ( s2 );   
  3. basic_string::substr  

返回从 _Off ( 下标)开始的 _Count 个字符组成的string

 
 
  1. basic _ string substr( size _ type _Off = 0
    size _ type 
    _Count = npos ) const;   
  2. string s("I love you!") , sub;   
  3. ssub=s.substr( ); // sub= ” I love you! ”   
  4. ssub=s.substr(1); // sub= ” love you! ”   
  5. ssub=s.substr(3,4); // sub= ” ove ”  


转载于:https://www.cnblogs.com/simplelifestyle/p/3761891.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NSIS 用户手册 新闻、信息、支持、例子、指南等可以到 http://nsis.sf.net 查看。 快速链接: FAQ - 常见问题列表 NSIS Wiki - 例子、函数、指南、插件、软件等等 Forum - 发表你的问题或进行 NSIS 相关讨论 版权所有 (C) 1999-2009 贡献者 第一章: 介绍 NSIS 关于 NSIS 主要特性 特性列表 第二章: 教程: 基础知识 介绍 脚本文件 脚本结构 安装程序属性 页面 区段 函数 脚本的工作方式 逻辑代码结构 变量 调试脚本 脚本的执行 编译器命令 编译器 新式用户界面(Modern UI) 插件 更多 第三章: 命令行的用法 MakeNSIS 的使用 选项 注意事项 环境变量 例子 安装程序的使用 公共选项 卸载程序特殊选项 例子 第四章: 脚本参考 脚本文件格式 变量 用户变量 Var 其他可写的变量 常量 在字符串里使用常量 标记 相对跳转 页面 次序关系 页面选项 回调 Page UninstPage PageEx PageExEnd PageCallbacks 区段 区段命令 AddSize Section SectionEnd SectionIn SectionGroup SectionGroupEnd 卸载区段 函数 函数命令 Function FunctionEnd 回调函数 安装回调 .onGUIInit .onInit .onInstFailed .onInstSuccess .onGUIEnd .onMouseOverSection .onRebootFailed .onSelChange .onUserAbort .onVerifyInstDir 卸载回调 un.onGUIInit un.onInit un.onUninstFailed un.onUninstSuccess un.onGUIEnd un.onRebootFailed un.onSelChange un.onUserAbort 安装程序属性 常规属性 AddBrandingImage AllowRootDirInstall AutoCloseWindow BGFont BGGradient BrandingText Caption ChangeUI CheckBitmap CompletedText ComponentText CRCCheck DetailsButtonText DirText DirVar DirVerify FileErrorText Icon InstallButtonText InstallColors InstallDir InstallDirRegKey InstProgressFlags InstType LicenseBkColor LicenseData LicenseForceSelection LicenseText MiscButtonText Name OutFile RequestExecutionLevel SetFont ShowInstDetails ShowUninstDetails SilentInstall SilentUnInstall SpaceTexts SubCaption UninstallButtonText UninstallCaption UninstallIcon UninstallSubCaption UninstallText WindowIcon XPStyle 编译器标记 AllowSkipFiles FileBufSize SetCompress SetCompressor SetCompressorDictSize SetDatablockOptimize SetDateSave SetOverwrite 版本信息 VIAddVersionKey VIProductVersion 指令 基本指令 Delete Exec ExecShell ExecWait File Rename ReserveFile RMDir SetOutPath 注册表、INI 文件指令 DeleteINISec DeleteINIStr DeleteRegKey DeleteRegValue EnumRegKey EnumRegValue ExpandEnvStrings FlushINI ReadEnvStr ReadINIStr ReadRegDWORD ReadRegStr WriteINIStr WriteRegBin WriteRegDWORD WriteRegStr WriteRegExpandStr 常规用途指令 CallInstDLL CopyFiles CreateDirectory CreateShortCut GetDLLVersion GetDLLVersionLocal GetFileTime GetFileTimeLocal GetFullPathName GetTempFileName SearchPath SetFileAttributes RegDLL UnRegDLL 流程控制指令 Abort Call ClearErrors GetCurrentAddress GetFunctionAddress GetLabelAddress Goto IfAbort IfErrors IfFileExists IfRebootFlag IfSilent IntCmp IntCmpU MessageBox Return Quit SetErrors StrCmp StrCmpS 文件指令 FileClose FileOpen FileRead FileReadByte FileSeek FileWrite FileWriteByte FindClose FindFirst FindNext 卸载程序指令 WriteUninstaller 混合指令 GetErrorLevel GetInstDirError InitPluginsDir Nop SetErrorLevel SetRegView SetShellVarContext Sleep 字符串操作指令 StrCpy StrLen 堆栈支持 Exch Pop Push 整数支持 IntFmt IntOp 重新启动指令 Reboot SetRebootFlag 安装记录指令 LogSet LogText 区段管理 SectionSetFlags SectionGetFlags SectionSetText SectionGetText SectionSetInstTypes SectionGetInstTypes SectionSetSize SectionGetSize SetCurInstType GetCurInstType InstTypeSetText InstTypeGetText 用户界面指令 BringToFront CreateFont DetailPrint EnableWindow FindWindow GetDlgItem HideWindow IsWindow LockWindow SendMessage SetAutoClose SetBrandingImage SetDetailsView SetDetailsPrint SetCtlColors SetSilent ShowWindow 多语言指令 LoadLanguageFile LangString LicenseLangString 多语言 语言选择 LangDLL 插件 RTL 语言 插件 DLLs 使用插件命令 手动调用插件 静默安装程序、卸载程序 第五章: 编译时的命令 编译时的命令 !include !addincludedir !addplugindir !appendfile !cd !delfile !echo !error !execute !packhdr !system !tempfile !warning !verbose 预定义 ${__FILE__} ${__LINE__} ${__DATE__} ${__TIME__} ${__TIMESTAMP__} 范围预定义 ${__GLOBAL__} ${__SECTION__} ${__FUNCTION__} ${__PAGEEX__} ${__UNINSTALL__} 读取环境变量 $%envVarName% 条件编译 !define !undef !ifdef !ifndef !if !ifmacrodef !ifmacrondef !else !endif !insertmacro !macro !macroend !searchparse !searchreplace 附录 A: 新式用户界面(Modern UI) 附录 B: DLL/TLB 库的安装 介绍 库的安装 介绍 参数 选项 LIBRARY_X64 LIBRARY_SHELL_EXTENSION LIBRARY_COM LIBRARY_IGNORE_VERSION 注意事项 例子 取消共享 DLL 共享 DLL 库的卸载 介绍 参数 选项 LIBRARY_X64 LIBRARY_SHELL_EXTENSION LIBRARY_COM 例子 Visual Basic 6 运行库 附录 C: 常用脚本 获取 Internet Explorer 版本 判断 .NET Framework 是否已安装? 判断 Macromedia Flash Player 是否已安装? 连接到 Internet 获取安装程序文件名 禁止多个安装程序实例 更多 Appendix D: 常用信息 错误级别 添加卸载信息到添加/删除程序面板 使用 System.dll 插件来调用一个外部 DLL 把安装详细信息导出到文件 如何读取 REG_MULTI_SZ 值 附录 E: 常用头文件 文件函数头文件 介绍 Locate GetSize DriveSpace GetDrives GetTime GetFileAttributes GetFileVersion GetExeName GetExePath GetParameters GetOptions GetOptionsS GetRoot GetParent GetFileName GetBaseName GetFileExt BannerTrimPath DirState RefreshShellIcons 文本函数头文件 介绍 LineFind LineRead FileReadFromEnd LineSum FileJoin TextCompare TextCompareS ConfigRead ConfigReadS ConfigWrite ConfigWriteS FileRecode TrimNewLines 文字函数头文件 介绍 WordFind WordFindS WordFind2X WordFind2XS WordFind3X WordFind3XS WordReplace WordReplaceS WordAdd WordAddS WordInsert WordInsertS StrFilter StrFilterS VersionCompare VersionConvert 附录 F: 更新纪录和发行说明 2.44 更改日志 主要更改 次要更改 2.43 发行说明 更改日志 次要更改 实用程序和插件 翻译 Plug-in API 构建系统 2.42 发行说明 更改日志 主要更改 次要更改 翻译 构建系统 2.41 更改日志 次要更改 翻译 构建系统 2.40 更改日志 主要更改 次要更改 翻译 2.39 更改日志 主要更改 次要更改 2.38 更改日志 主要更改 次要更改 Modern UI 翻译 2.37 更改日志 主要更改 次要更改 实用程序和插件 翻译 构建系统 2.36 发行说明 更改日志 主要更改 Modern UI nsDialogs 次要更改 实用程序和插件 翻译 构建系统 2.35 更改日志 主要更改 次要更改 实用程序和插件 2.34 发行说明 更改日志 主要更改 次要更改 实用程序和插件 翻译 构建系统 2.33 发行说明 更改日志 主要更改 次要更改 实用程序和插件 翻译 构建系统 2.32 发行说明 更改日志 主要更改 次要更改 实用程序和插件 翻译 构建系统 2.31 发行说明 更改日志 次要更改 实用程序和插件 翻译 构建系统 2.30 发行说明 更改日志 主要更改 次要更改 实用程序和插件 翻译 构建系统 2.29 发行说明 更改日志 主要更改 次要更改 实用程序和插件 翻译 构建系统 2.28 更改日志 主要更改 次要更改 实用程序和插件 翻译 构建系统 2.27 发行说明 更改日志 主要更改 次要更改 构建系统 2.26 发行说明 更改日志 主要更改 次要更改 新/更改的命令 实用程序和插件 翻译 构建系统 2.25 更改日志 主要更改 次要更改 新/更改的命令 实用程序和插件 翻译 构建系统 2.24 更改日志 主要更改 次要更改 新/更改的命令 实用程序和插件 翻译 构建系统 2.23 更改日志 次要更改 实用程序和插件 构建系统 2.22 发行说明 更改日志 次要更改 新/更改的命令 翻译 构建系统 2.21 更改日志 主要更改 次要更改 新/更改的命令 翻译 构建系统 2.20 更改日志 次要更改 翻译 构建系统 2.19 更改日志 次要更改 新/更改的命令 插件 翻译 2.18 更改日志 次要更改 翻译 2.17 更改日志 次要更改 翻译 2.16 发行说明 更改日志 主要更改 新/更改的命令 次要更改 翻译 构建系统 2.15 更改日志 新/更改的命令 次要更改 翻译 构建系统 2.14 发行说明 更改日志 主要更改 次要更改 2.13 发行说明 更改日志 主要更改 新/更改的命令 次要更改 实用程序和插件 翻译 构建系统 2.12 更改日志 主要更改 新/更改的命令 次要更改 实用程序和插件 翻译 构建系统 2.11 发行说明 更改日志 主要更改 新/更改的命令 次要更改 实用程序和插件 翻译 构建系统 2.10 更改日志 主要更改 次要更改 翻译 构建系统 2.09 更改日志 主要更改 新/更改的命令 次要更改 翻译 构建系统 2.08 发行说明 更改日志 主要更改 次要更改 构建系统 2.07 发行说明 更改日志 主要更改 新/更改的命令 次要更改 Include Files 实用程序和插件 翻译 2.06 更改日志 主要更改 新/更改的命令 次要更改 实用程序和插件 2.05 发行说明 更改日志 新/更改的命令 次要更改 2.04 更改日志 主要更改 次要更改 2.03 发行说明 更改日志 主要更改 新/更改的命令 次要更改 2.02 更改日志 主要更改 新/更改的命令 次要更改 实用程序和插件 2.01 发行说明 更改日志 主要更改 新/更改的命令 次要更改 实用程序和插件 2.0 发行说明 更改日志 Changes from 1.98 Changes from RC4 2.0 Release Candidate 4 更改日志 主要更改 次要更改 实用程序和插件 2.0 Release Candidate 3 更改日志 次要更改 实用程序和插件 2.0 Release Candidate 2 更改日志 次要更改 实用程序和插件 2.0 Release Candidate 1 发行说明 更改日志 主要更改 实用程序和插件 2.0 Beta 4 发行说明 更改日志 主要更改 新/更改的命令 次要更改 实用程序和插件 2.0 Beta 3 2.0 Beta 2 2.0 Beta 1 2.0 Beta 0 2.0 Alpha 7 2.0 Alpha 6 2.0 Alpha 5 2.0 Alpha 4 2.0 Alpha 3 2.0 Alpha 2 2.0 Alpha 1 2.0 Alpha 0 旧版本 附录 G: 构建 NSIS 常规构建 在 Windows 上构建 在 POSIX 上构建 每日构建 附录 H: 致谢名单 程序员 资源设计师 翻译人员 文档编写者 附录 I: 许可协议 版权 适用许可协议 zlib/libpng 许可协议 bzip2 许可协议 通用公共许可证 版本1.0 LZMA 压缩模块特殊例外声明 附录 J: 简体中文用户手册说明 致谢 NSIS 交流 NSIS 相关网站 翻译反馈 免责声明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值