gpt4 book ai didi

将 ZZ (NTL) 转换为 C 中的字符串

转载 作者:太空宇宙 更新时间:2023-11-04 02:48:49 25 4
gpt4 key购买 nike

ZZ可以用c++输出

cout << ZZ;

因为NTL是一个c++库。

但是,如何使用 printf 在 C 中输出 ZZ,或者将 ZZ 转换为 string

最佳答案

您可以使用 stringstreamZZ 打印(转换)为 string:

#include <sstream>
#include <string>

std::string zToString(const ZZ &z) {
std::stringstream buffer;
buffer << z;
return buffer.str();
}

编辑:您可以使用 .c_str() 方法从 std::string 获取 C 字符串,但是如果您想要一个独立的 C 字符串,那么您可以 strdup 它:

#include <cstring>

char *zzstring = strdup(zToString(z).c_str());

并且记得在超出范围之前free(zzstring)

关于将 ZZ (NTL) 转换为 C 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24240120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com