c++不使用C标准库将字符串和int转换为char *

标签 c++ string integer char

not using the C standard library do this in C++ ?

  1. 将字符串转换为char*
  2. 将int转换为char*

如果我必须在 C++ 中将字符串转换为 int,请使用类似这样的东西

#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
    int num;
    string str="2020";
    (stringstream)"2020">>num;
    cout<<num+2;
    return 0;
}

最佳答案

#include <sstream>
#include <iostream>
#include <string>

int main (int argc, char* argv[])
{
    std::string str ("123");
    const char* c_str = str.c_str();

    char* so_bad = const_cast<char*>(c_str);

    std::stringstream ss;
    ss << so_bad;

    int int_value;
    ss >> int_value;

    std::cout << int_value;

    return 0;
}    

关于c++不使用C标准库将字符串和int转换为char *,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5352717/

相关文章:

c++ - BFS for Maze C++ 段错误

java - Java中字符串池的底层机制?

c# - 如何使用两个不同的数字生成唯一 key ,然后使用这些数字之一检索此 key

c++ - #error "SSE2 instruction set not enabled"包含 <emmintrin.h> 时

c++ - 如何新建和删除 AVPacket?

c++ - 仅通过其参数查找重载地址

java - 更改长变量的格式?

java - 字符串过滤..任何一个..仅包含所选字符之一或两者

c# - 将字符串从文本文件转换为整数数组

c - 整数常量的无效后缀 "-252"