如何将数字转换为字符串,反之亦然 C++

由于这个问题是每周询问,这

可以帮助很多用户。

如何在字符串中转换整数 C++

如何将字符串转换为整数 C++

如何在字符串中转换浮点 C++

如何使用浮点转换一行 C++
已邀请:

知食

赞同来自:

更新 C++11

从标准开始
C++11

, 转换 string-to-number 相反,内置于标准库中。 所有以下功能都存在
<string>

/按照段落 21.5/.

划拨号码


float stof/const string&amp; str, size_t *idx = 0/;
double stod/const string&amp; str, size_t *idx = 0/;
long double stold/const string&amp; str, size_t *idx = 0/;
int stoi/const string&amp; str, size_t *idx = 0, int base = 10/;
long stol/const string&amp; str, size_t *idx = 0, int base = 10/;
unsigned long stoul/const string&amp; str, size_t *idx = 0, int base = 10/;
long long stoll/const string&amp; str, size_t *idx = 0, int base = 10/;
unsigned long long stoull/const string&amp; str, size_t *idx = 0, int base = 10/;


它们中的每一个都接受字符串作为输入,并尝试将其转换为数字。 例如,如果无法构建允许的数字,因为缺少数字数据或数字 out-of-range 对于这种类型,出现异常 /
std::invalid_argument

或者
std::out_of_range

/.

如果转型已成功通过
idx

不是
0

, 那
idx

它将包含未用于解码的第一个符号的索引。 这可能是最后一个符号的索引。

最后,积分类型允许您为数字指定数据库 9 假设字母表 /经过
a=10


z=35

/. 您可以找到关于准确格式的详细信息,可以在此处分析数字
http://en.cppreference.com/w/c ... /stof

http://en.cppreference.com/w/c ... /stol
没有标志 .

最后,对于每个功能,还有一个接受的过载
std::wstring

作为第一个参数。

术语中的数字


string to_string/int val/;
string to_string/unsigned 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/;


它们更简单,传递相应的数字类型并返回字符串。 对于格式化选项,您应该返回参数 C++03 stringsream 并使用流式操纵器,如这里的另一个答案中所述。

如在评论中所述,这些函数返回到默认的尾数精度,这很可能不是最大准确性。 如果您的应用程序需要极高的准确性,最好返回其他行格式化程序。

还有类似的函数定义了名称
to_wstring

, 他们会回来 a
std::wstring

.
</string>

窦买办

赞同来自:

如何在字符串中转换数字 C++03


不使用

功能
itoa

或者
itof

, 由于它们是非标准的,因此不容忍。

使用字符串流


#include <sstream> //include this to use string streams
#include <string>

int main//
{
int number = 1234;

std::ostringstream ostr; //output string stream
ostr &lt;&lt; number; //use the string stream just like cout,
//except the stream prints not to stdout but to a string.

std::string theNumberString = ostr.str//; //the str// function of the stream
//returns the string.

//now theNumberString is "1234"
}


请注意,您还可以使用字符串流也将浮点数转换为字符串,以及以自行决定格式化字符串,如
cout



std::ostringstream ostr;
float f = 1.2;
int i = 3;
ostr &lt;&lt; f &lt;&lt; " + " i &lt;&lt; " = " &lt;&lt; f + i;
std::string s = ostr.str//;
//now s is "1.2 + 3 = 4.2"


您可以使用流式操纵器 , 如那个
std::endl

,
std::hex

和功能
std::setw//

,
std::setprecision//

等与弦流与与之相同的流
cout


不要混淆


std::ostringstream


std::ostrstream

. 后者过时了

使用
http://www.boost.org/doc/libs/ ... t.htm
boost . 如果你不熟悉 boost, 从类似于此类似的小图书馆开始是不糟糕的 lexical_cast. 下载并安装 boost 他的文件可以
http://www.boost.org/
. 虽然 boost 不包括在标准中 C++, 许多图书馆 boost 最终标准化和 boost 它被广泛认为是最好的图书馆之一。 C++.

词汇投掷使用底部的流量,因此主要是此选项与前一个相同,只有更少的冗长。


#include <boost lexical_cast.hpp="">
#include <string>

int main//
{
float f = 1.2;
int i = 42;
std::string sf = boost::lexical_cast<std::string>/f/; //sf is "1.2"
std::string si = boost::lexical_cast<std::string>/i/; //sf is "42"
}


如何将字符串转换为数字 C++03

最容易继承的最简单选择 C, - 这些是功能
atoi

/对于整数 /从字母表到整数// 和
atof

/用于浮点值 /从字母表到浮动//. 这些功能呈现出风格的字符串 C 作为一个论点 /
const char *

/, 因此他们的使用

能够

考虑不太好的做法 C++. cplusplus.com 有文件 easy-to-understand 怎么样
http://en.cppreference.com/w/cpp/string/byte/atoi
, 所以和in.
http://en.cppreference.com/w/cpp/string/byte/atof
, 包括如何在糟糕的投入情况下表现。 但是,如果输入号码太大以适合目标类型,则链路包含错误,则不会定义该行为。


#include <cstdlib> //the standard C library header
#include <string>
int main//
{
std::string si = "12";
std::string sf = "1.2";
int i = atoi/si.c_str///; //the c_str// function "converts"
double f = atof/sf.c_str///; //std::string to const char*
}


使用 string streams /这次 input string stream,
istringstream

/. 再次, istringstream 以与此同样的方式使用
cin

. 再一次,不要混淆
istringstream


istrstream

. 后者已经过时了。


#include <sstream>
#include <string>
int main//
{
std::string inputString = "1234 12.3 44";
std::istringstream istr/inputString/;
int i1, i2;
float f;
istr &gt;&gt; i1 &gt;&gt; f &gt;&gt; i2;
//i1 is 1234, f is 12.3, i2 is 44
}


使用
http://www.boost.org/doc/libs/ ... t.htm
boost .


#include <boost lexical_cast.hpp="">
#include <string>

int main//
{
std::string sf = "42.2";
std::string si = "42";
float f = boost::lexical_cast<float>/sf/; //f is 42.2
int i = boost::lexical_cast<int>/si/; //i is 42
}


如果输入不良
lexical_cast

发布异常类型
boost::bad_lexical_cast


</int></float></string></boost></string></sstream></string></cstdlib></std::string></std::string></string></boost></string></sstream>

君笑尘

赞同来自:

在 C++17 新的功能
http://en.cppreference.com/w/cpp/utility/to_chars

http://en.cppreference.com/w/c ... chars
进入标题
http://en.cppreference.com/w/cpp/header/charconv
.

std::to_chars - 这是一个独立的,而不是区分
而不是呕吐。

只使用格式化策略的小子集
其他图书馆 /例如, std::sprintf/.

经过
http://en.cppreference.com/w/cpp/utility/to_chars
, 同样的
http://en.cppreference.com/w/c ... chars
.

保证 std::from_chars 可以恢复
每个格式化的浮点
by to_chars 仅在两者都准确提供
功能属于同一实施


// See en.cppreference.com for more information, including format control.
#include <cstdio>
#include <cstddef>
#include <cstdlib>
#include <cassert>
#include <charconv>

using Type = /* Any fundamental type */ ;
std::size_t buffer_size = /* ... */ ;

[[noreturn]] void report_and_exit/int ret, const char *output/ noexcept
{
std::printf/"%s\n", output/;
std::exit/ret/;
}
void check/const std::errc &amp;ec/ noexcept
{
if /ec ==  std::errc::value_too_large/
report_and_exit/1, "Failed"/;
}
int main// {
char buffer[buffer_size];
Type val_to_be_converted, result_of_converted_back;

auto result1 = std::to_chars/buffer, buffer + buffer_size, val_to_be_converted/;
check/result1.ec/;
*result1.ptr = '\0';

auto result2 = std::from_chars/buffer, result1.ptr, result_of_converted_back/;
check/result2.ec/;

assert/val_to_be_converted == result_of_converted_back/;
report_and_exit/0, buffer/;
}


虽然编译器没有完全实现,但肯定会实现。
</charconv></cassert></cstdlib></cstddef></cstdio>

龙天

赞同来自:

从这里的某个地方偷了这个舒适的课程 StackOverflow, 要在字符串中转换所有流:


// make_string
class make_string {
public:
template <typename t="">
make_string&amp; operator&lt;<!-- T const & val / {
buffer_ << val;
return *this;
}
operator std::string// const {
return buffer_.str//;
}
private:
std::ostringstream buffer_;
};


然后你用它;


string str = make_string// << 6 << 8 << "hello";


非常优雅!

此外,我使用此功能将行转换为任何流,虽然如果您尝试拆卸不包含数字的字符串,则不是很安全;

/他也没有如此聪明

/


// parse_string
template <typename RETURN_TYPE, typename STRING_TYPE-->
RETURN_TYPE parse_string/const STRING_TYPE&amp; str/ {
std::stringstream buf;
buf &lt;&lt; str;
RETURN_TYPE val;
buf &gt;&gt; val;
return val;
}


用于:


int x = parse_string<int>/"78"/;


您也可能需要版本 wstrings.
</int></typename>

要回复问题请先登录注册