• c++ 11字符串与string转换常用函数


      这里主要介绍一下string to int 其他方法与这个类似,可到头文件 <string> 中查看
         @_Str 转换的字符串
         @_Idx 转换的长度(位数)
         @_Base 进制
    • double stod(const string& _Str, size_t *_Idx = nullptr);
    • float stof(const string& _Str, size_t *_Idx = nullptr);
    • int stoi(const string& _Str, size_t *_Idx = nullptr, int _Base = 10);
    • long stol(const string& _Str, size_t *_Idx = nullptr, int _Base = 10);
    • long double stold(const string& _Str, size_t *_Idx = nullptr);
    • unsigned long stoul(const string& _Str, size_t *_Idx = nullptr, int _Base = 10);
    • long long stoll(const string& _Str, size_t *_Idx = nullptr, int _Base = 10);
    • unsigned long long stoull(const string& _Str, size_t *_Idx = nullptr, int _Base = 10);
         std::string y = "253647586946334221002101219955219971002";
    	int x;
     
    	try {
    		x = stoi(y);
    	}
    	catch (std::invalid_argument&){
    		// if no conversion could be performed
    		cout << "Invalid_argument" << endl;
    	}
    	catch (std::out_of_range&){
    		// if the converted value would fall out of the range of the result type 
    		// or if the underlying function (std::strtol or std::strtoull) sets errno 
    		// to ERANGE.
    		cout << "Out of range" << endl;
    	}
    	catch (...) {
    		// everything else
    		cout << "Something else" << endl;
    	}
    	return 0;
    • string to_string(int _Val);
    • string to_string(unsigned int _Val);
    • string to_string(long _Val); string
    • to_string(unsigned long _Val);
    • string to_string(long long _Val);
    • string to_string(unsigned long long _Val);
    • string to_string(float _Val);
    • string to_string(double _Val);
    • string to_string(long double _Val);

    可以看到 to_string(); 方法不仅支持各种整型( 有符号的、无符号的、长的、短的 任意组合 ),还支持各种浮点型( float/double 包括长的、短的 )

     

  • 相关阅读:
    解决:TypeError: object() takes no parameters
    刷题(三)
    刷题(二)
    web自动化流程总结
    不能在Python Console中运行pytest
    关于pytest的一些问题
    UML设计,可以设计程序的用例图、类图、活动图等_SurfaceView
    android系统下消息推送机制
    Android中的动画,选择器,样式和主题的使用
    内存监测工具 DDMS --> Heap
  • 原文地址:https://www.cnblogs.com/wsw-seu/p/13377048.html
Copyright © 2020-2023  润新知