c++之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);

功能:

将数值转化为字符串。返回对应的字符串。

例子:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string pi = "pi is " + std::to_string(3.1415926);
    string perfect = to_string(1 + 2 + 4 + 7 + 14) + " is a perfect number";
    cout << pi << ‘\n‘;
    cout << perfect << ‘\n‘;
    system("pause");
    return 0;
}

参考:https://blog.csdn.net/u010455041/article/details/50097251



c++之to_string()函数

原文地址:https://www.cnblogs.com/yibeimingyue/p/9996923.html

时间: 2024-08-06 13:01:58

c++之to_string()函数的相关文章

C++实现to_string函数--int to string

to_string()函数返回字符串形式, 例如: #include<iostream> #include<string> using namespace std; int main() { int i=123; //aastring s=to_string(134) + "abc"; string s=to_string(i) + "abc"; cout<<s<<endl; system("pause&qu

编写STL中没有定义的函数(如果能编译)

实例目标:本例介绍如何编写标准模板库STL中没有定义的函数.这里列举两个例子,一个是将所有数据类型转换成字符串型的to_string函数,另一个是它的"反函数"from_string函数,用来将字符串转化为某一类型 具体内容: 在std::string模板中没有包含一些函数实用性较强的,原因就是因为客户轻易编写其中的代码.例如:可以实现一个将所有数据类型转换为字符串型的to_string函数代码如下: #include<iostream> template<class

Oracle函数-高阶篇

下面整理了部分oracle函数-高阶篇: 1. CATSTR    举例:SELECT CATSTR(COLUMN_NAME) NAME_LIST FROM DBA_TAB_COLUMNS WHERE TRIM(TABLE_NAME) = 'T_RZ_DKDATA'  --查询出的结果在一个列中2. INSTR() 检索字符串函数:匹配则返回首次检索的位置的索引值(从1开始),值>0,否则返回值=0    举例:SELECT * FROM USER_TABLES WHERE INSTR(TABL

【C++】int转换为string的两种方法(to_string、字符串流)

本文转自http://blog.csdn.net/chavo0/article/details/51038397 记录一下用到过的int转换成string的两种方法 第一种是to_string函数,这是C++11新增的,使用非常方便,简单查了下:C++11标准增加了全局函数std::to_string,以及std::stoi/stol/stoll等等函数(这几个就是string转int,long,以及long long啦~) to_string这个函数还是很强大的! string to_stri

c++ 字符串流 sstream(常用于格式转换) 【转载】

使用stringstream对象简化类型转换C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性.类型安全和可扩展性.在本文中,我将展示怎样使用这些库来实现安全和自动的类型转换. 为什么要学习 如果你已习惯了<stdio.h>风格的转换,也许你首先会问:为什么要花额外的精力来学习基于<sstream>的类型转换呢?也许对下面一个简单的例子的回顾能够说服你.假设你想用sprintf()函数将一个变量从int类型转

Boost总结汇总

从开始接触Boost已经有好几年了,而对它的掌握却难言熟悉,有对它部分的源代码的剖析也是蜻蜓点水.有时间一点点梳理一下吧. 1. 概述 [Boost]C++ Boost库简介[Boost]C++ Boost 学习资源列表[Boost]Boost使用几条简单笔记[Boost]Poco vs Boost 2. 工具 [Boost]利用typeid来获取变量的类型[Boost]boost::function介绍[Boost]boost::bind四种应用场景的例子 3. 字符串与Range相关 [Bo

字符串转数字_atoi_stringstream

一.#include <cstdlib> 字符串转换到整型数,函数原型:int atoi(const char *nptr) 注意事项:有符号整型,能转换的最大字符串是:"2147483647",所有大于这个数的字符串,转换后仍为有符号int的最大值:2147483647. 实现方法: #include <stdio.h> #include <stdlib.h> long long StrToIntCore(const char* str, bool

C++ sstream 中处理字符串

C++引入ostringstream.istringstream.stringstream这三个类,要使用他们创建对象就必须包含<sstream>这个头文件. istringstream的构造函数原形如下:istringstream::istringstream(string str);它的作用是从string对象str中读取字符,stringstream对象可以绑定一行字符串,然后以空格为分隔符把该行分隔开来. 下面我们分离以空格为界限,分割一个字符串. #include<iostre

转:stringstream的用法

[本文来自]http://www.builder.com.cn/2003/0304/83250.shtmlhttp://www.cppblog.com/alantop/archive/2007/07/10/27823.html使用stringstream对象简化类型转换C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性.类型安全和可扩展性.在本文中,我将展示怎样使用这些库来实现安全和自动的类型转换. 为什么要学习 如果你已习惯了&l