zoukankan      html  css  js  c++  java
  • C++基础--string转

    有时候除了要将数值型转为string外,可能也需要将一些string转为数值型,这个时候也还是可以用sstream字符串流来实现,同时也可以用C++标准库得到函数来实现。

    1、字符串流

    这个时候使用istringstream()函数来实现:

    std::istringstream iss(sStr);
    double dNum1 = 0.0;
    iss >> dNum1;
    std::cout << "dNum1 = " << dNum1 << std::endl;

    2、C++标准库

    C++标准库提供了string转为其它数值类型的函数:

    double atof(const char *_String);
    int atoi(const char *_Str);
    long atol(const char *_Str);
    long long atoll(const char *_Str);

    通过标准库的函数可以实现string转为基本的数值:

    int iNum1 = atoi(sStr.c_str());
    std::cout << "iNum1 = " << iNum1 << std::endl;

    才见乔叶绿

    又闻杜鹃鸣

    夏至木鱼美

    独钓此山中

    上善若水,为而不争。
  • 相关阅读:
    Codeforces 385C
    Codeforces 496C
    HDU 6114 Chess
    Codeforces 839B
    Codeforces 483B
    Codeforces 352B
    Codeforces 768B
    Codeforces 38B
    Codeforces 735B
    Codeforces 534B
  • 原文地址:https://www.cnblogs.com/Bearoom/p/11721751.html
Copyright © 2011-2022 走看看