美文网首页
C_C++ 字符串数字的转换

C_C++ 字符串数字的转换

作者: NHFX | 来源:发表于2022-01-06 14:14 被阅读0次

C++ 字符串流 stringstream

C++ stream library 中的 stringstream 允许我们使用流输入输出操作符 <<、 >> 进行数字和字符串转换,使用stringstream 工具需要包含头文件 #include <sstream>。

数字转化为字符串 示例

#include <iostream>
#include <sstream>

// MARK: - Main 入口
int main(int argc, char *argv[])
{
    // number to be converted to a string
    int number = 123;
    // string which will contain the result
    std::string result;
    // stream used for the conversion
    // 转换时只需要对流进行输出操作,因此可以使用(ostringstream)代替(stringstream)
    std::ostringstream convert;
    // insert the textual representation of 'number'
    // in the characters in the stream
    convert << number;
    // set 'result' to the contents of the stream
    result = convert.str();

    std::cout << result <<std::endl;
    return 0;
}

简化写法

#include <iostream>
#include <sstream>

// MARK: - Main 入口
int main(int argc, char *argv[])
{
    // number to be converted to a string
    int number = 123;
    // 构造一个未命名的stringstream对象,并将number输入到流中
    // 返回ostream的引用,结果在强制转换为ostringstream,
    // 在使用str()取流中的内容
    std::string result = static_cast<ostringstream*>( &(ostringstream() << number) )->str();
    std::cout << result <<std::endl;
    return 0;
}

字符串转化为数字 示例

#include <iostream>
#include <sstream>

// MARK: - Main 入口
int main(int argc, char *argv[])
{
    // string containing the number
    std::string text = "456";

    int result;
    // stringstream used for the conversion
    // constructed with the contents of 'text'
    std::istringstream convert(text);
    if ( !(convert >> result) )
        result = 0;
    std::cout << result <<std::endl;
    return 0;
}

简化写法

#include <iostream>
#include <sstream>

// MARK: - Main 入口
int main(int argc, char *argv[])
{
    string text = "456";
    int number;
    if ( ! (istringstream(text) >> number) ) number = 0;
    std::cout << number <<std::endl;
    return 0;
}

功能封装

接下来,为了更方便我们在编程中的使用,我们使用stringstream来封装一些简单的转换函数

// MARK: - T转化为字符串
template <typename T>
std::string number_to_string ( T number ) {
    std::ostringstream ss;
    ss << number;
    return ss.str();
}
// MARK: - 字符串转化为T
template <typename T>
T string_to_number ( const std::string &text ) {
    std::istringstream ss(text);
    T result;
    return ss >> result ? result : 0;
}
// MARK: - Main 入口
int main(int argc, char *argv[])
{
    float number = 123.456;
    std::string result = number_to_string(number);
    std::cout << result << std::endl;
    std::string text = "78.9";
    number = string_to_number<float>(text);
    std::cout << number << std::endl;
    return 0;
}

C++2.0

到了C++11又引入了一些标准库函数,可以将基本的数据类型与字符串进行转换。

    函数 std::to_string() 将基本数字类型转换为字符串。
    函数 std::stoi()、std::stol()、std::stoll() 将字符串转换为整数类型。
    函数 std::stof()、std::stod()、std::stold() 将字符串转换为浮点值。

以上的这些方法声明在<string>中。

#include <iostream>
#include <string>

// MARK: - Main 入口
int main(int argc, char *argv[])
{
    float number = 123.456;
    std::string result = std::to_string(number);
    std::cout << result << std::endl;
    std::string text = "78.9";
    number = std::stof(text);
    std::cout << number << std::endl;
    return 0;
}

C 标准输出

C语言中没有流库, 我们可以使用sprintf对数据进行格式化。

数字转化为字符串 示例

int main(int argc, char *argv[])
{
    // number to be converted to a string
    int number = 123;
    // string which will contain the result
    char result[32];
    sprintf ( result, "%d", number );
    std::cout << result <<std::endl;
}

字符串转化为数字 示例

int main(int argc, char *argv[])
{
    // number to be converted to a string
    int number = 0;
    // string which will contain the result
    char text[] = "1234";
    int succeeded = sscanf ( text, "%d", &number );
    if(succeeded== EOF) number = 0;
    std::cout << number <<std::endl;
}

使用C的标准库

  • itoa
  • atoi
  • atol
  • atof
  • strtol
  • strtoul
  • strtod

相关文章

  • C_C++ 字符串数字的转换

    C++ 字符串流 stringstream C++ stream library 中的 stringstream ...

  • 隐式转换

    隐式转换结论 1、数字 + 字符串 :将数字转换为字符串 2、数字 + boolean :将 boolean 转换...

  • 数据类型的转换

    隐式转换结论 1、数字 + 字符串 :将数字转换为字符串 2、数字 + boolean :将 boolean 转换...

  • 第三天

    隐式转换结论 1、数字 + 字符串 :将数字转换为字符串 2、数字 + boolean :将 boolean 转换...

  • 2018-07-11

    隐式转换结论 1、数字 + 字符串 :将数字转换为字符串 2、数字 + boolean :将 boolean 转换...

  • ☝(•̀˓◞•́)哎呦

    隐式转换结论 1、数字 + 字符串 :将数字转换为字符串 2、数字 + boolean :将 boolean 转换...

  • ☝(•̀˓◞•́)哎呦

    隐式转换结论 1、数字 + 字符串 :将数字转换为字符串 2、数字 + boolean :将 boolean 转换...

  • 转换.运算符

    一、隐式转换 1、数字 + 字符串 :将数字转换为字符串 2、数字 + boolean :将 boolean 转换...

  • 谢老板

    1,隐式转换结论 数字+字符串:将数字转换为字符串 数字+boolean:将boolean转换为number类型 ...

  • 将字符串转换成数字

    将字符串转换成double类型的数字: 将字符串转换成Float类型的数字: 将字符串转换成整形的数字:

网友评论

      本文标题:C_C++ 字符串数字的转换

      本文链接:https://www.haomeiwen.com/subject/akxrcrtx.html