stoi()和to_string()
来源:互联网 发布:mac网桥未连接 编辑:程序博客网 时间:2024/05/23 17:35
在某些老版本的系统string中无法使用这两个函数:
stoi(): 它可以直接将string转化为int
int stoi (const string& str, size_t* idx = 0, int base = 10);int stoi (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to integer
Parses str interpreting its content as an integral number of the specifiedbase, which is returned as anint value.If idx is not a null pointer, the function also sets the value of idx to the position of the first character instr after the number.
The function uses strtol (orwcstol) to perform the conversion (seestrtol for more details on the process).
它可以被atoi()替换,它需要char*类型的参数,所以需要将string转化为char*,可以调用string.c_str()函数将标准的string转为char *
atoi():
int atoi (const char * str);
Convert string to integer
Parses the C-string str interpreting its content as an integral number, which is returned as a value of typeint
.The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many base-10 digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because eitherstr is empty or it contains only whitespace characters, no conversion is performed and zero is returned.
to_string():
string to_string (int val);string to_string (long val);string to_string (long long val);string to_string (unsigned val);string to_string (unsigned 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);
Convert numerical value to string
Returns a string with the representation ofval.The format used is the same that printf would print for the corresponding type:
The representations of negative values are preceded with a minus sign (-).long"%ldlong long"%lldunsigned"%u"Decimal-base representation of val.unsigned long"%luunsigned long long"%llufloat"%f"As many digits are written as needed to represent the integral part, followed by the decimal-point character and six decimal digits.
inf (or infinity) is used to represent infinity.
nan (followed by an optional sequence of characters) to represent NaNs (Not-a-Number).
The representations of negative values are preceded with a minus sign (-).double"%flong double"%Lf
sprintf() 可以代替上面的to_string()在老版本上
int sprintf ( char * str, const char * format, ... );
Write formatted data to string
Composes a string with the same text that would be printed if format was used onprintf, but instead of being printed, the content is stored as aC string in the buffer pointed by str.The size of the buffer should be large enough to contain the entire resulting string (seesnprintf for a safer version).
A terminating null character is automatically appended after the content.
After the format parameter, the function expects at least as many additional arguments as needed forformat.
Parameters
- str
- Pointer to a buffer where the resulting C-string is stored.
The buffer should be large enough to contain the resulting string. - format
- C string that contains a format string that follows the same specifications asformat inprintf (seeprintf for details).
- ... (additional arguments)
- Depending on the format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace aformat specifier in theformat string (or a pointer to a storage location, forn).
There should be at least as many of these arguments as the number of values specified in theformat specifiers. Additional arguments are ignored by the function.
0 0
- stoi()和to_string()
- C++11中的string - to_string/stoi
- atoi和stoi
- atol和stoi的用途
- Android NDK编译C++ 'stoi' and 'to_string' is not a member of 'std'
- hashcode和to_string
- string转int/float/double、int/float/double转string、转字符串数组的方法:stoi、stringstream、scanf、to_string、sprintf
- string转int/float/double、int/float/double转string、转字符串数组的方法:stoi、stringstream、scanf、to_string、sprintf
- 【数学】stoi
- stoi函数
- c++ to_String()
- C语言冒泡排序法和函数stoi()数字字符串转化的问题
- 字符串转为整型StoI
- c++/sstream/stoi/
- C++ 将int转换为string的两种方法【to_string和sstream】
- Reversible Primes (20)stoi应用
- C++11中的to_string
- C++11的to_string()
- Git碎片~~
- 使用npm升级node js
- HTML5移动端touch滑动与重力/方向感应结合
- Android字符串资源及其格式化
- Gson转换同父类不同子类列表
- stoi()和to_string()
- 为什么pthread_cond_wait需要传递mutex参数
- Servlet知识笔记
- Android开发艺术2之Activity的启动模式
- Hibernate Criteria的 Criterion,Projection,Restrictions等条件设置
- android 传参 StringRequest 线程 实现页面页面停留
- 跨线程操作窗体
- canvas雪景
- 蓝桥杯 数列特征(排序,求和)