std::string的substr越界问题

今天程序中出现程序奔溃,具体如下。


调试发现std::string中的substr(nbeg,nlen);参数nbeg大于string总长度,所以导致中断。所以在substr调用前,增加nbeg与总长度的判断。




参考资料: 

1. http://blog.sina.com.cn/s/blog_700a65cc0100mib6.html

C++ string的substr是一个很好用的函数,比如下面一个字符串

     string s = "abc http://defg.hij/kl.html--mno";

      可以用下面代码提取出http到html中间的部分:

      size_t pos1, pos2;
      pos1 = s.find("http");
      pos2 = s.find("html");
      string sub = s.substr(pos1, pos2 - pos1 + 4);

      不过一定要注意的是,如果s中不存在http或者html,对substr的调用就会产生一个异常。如果不能及时捕获这个异常并进行处理的话,那么程序的非正常退出是可以预期的。

      或者可以这样写:
      string sub;
      size_t pos1, pos2;
      pos1 = s.find("http");
      pos2 = s.find("html");
      if(pos1 != string::npos && pos2 != string::npos && pos2 > pos1)
              sub = s.substr(pos1, pos2 - pos1 + 4);

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
字符转的各种转换函数库,用这个库,字符串的各种转换再也不头疼 //-------------字符串转数字-------------- int CString_numberStr_2_int(CString str); int stdString_2_int(std::string str);//std string 字符串转int int std_hexString_2_int(std::string str);//std string 字符串转int double CString_2_double(CString in); double stdString_2_double(std::string str);//std string 字符串转double unsigned int hexString_2_u32(std::string str); //-------------数字转字符串-------------- CString CString_int_2_numberStr(int n); CString CString_int_2_numberStr6(int n); std::string stdString_int_2_numberStr(int a); CString double_2_number_CString(double n); std::string double_2_number_String(double a); CString GetCurrWorkingDir(); //获得当前工作路径,不含最后一个字符“\” CString ascii_2_cstring(int ascii);// 把ascii字符转换成对应的字符,返回cstring类型 //-------------字符串转字符串-------------- std::string CString_2_stdString(CString str); CString stdString_2_CString(std::string str); void CString_2_charP(CString str,char *q); //cstring change to ansi string //-------------判断字符串类型-------------- bool is_a_double_type_string(CString str); //-------------字符串复杂操作-------------- int break_string(std::string instr, std::string subStr, vector<std::string>* subs); //以subStr为标记切割instr为多个单词,存储于subs std::string super_CString::getTimeString(bool fgf);//获得表示当前时间的字符串,fgf表示是否需要分隔符 std::string super_CString::getDateTimeString(bool fgf);//获得表示当前时间和日期的字符串,fgf表示是否需要分隔符 std::string getDateString(bool fgf); //获得表示当前日期的字符串,fgf表示是否需要分隔符 std::string string_trim_LR_space(std::string str);//去除字符串前后的空白字符 std::string string_trim_L_space(std::string str);//去除字符串左边的空白字符 std::string string_trim_R_space(std::string str);//去除字符串右边的空白字符 //-------------GUI相关-------------- CString get_CEdit_text(CEdit* edit); //-------------文件、路径相关-------------- CString calc_CurrWorkingDir_filename(CString str);//返回程序运行的当前路径+filename CString calc_work_full_filename(CString filename,CString path);//path为路径,如果path为空返回程序运行的当前路径+filename,否则返回path+filename。 std::string GetCurrWorkingDir_stdString(); std::string calc_CurrWorkingDir_filename(std::string str); std::string calc_work_full_filename(std::string filename,std::string path);

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值