printf with std::string?(printf 与 std::string?)
问题描述
我的理解是string是
std
命名空间的成员,那么为什么会出现以下情况呢?
My understanding is that string
is a member of the std
namespace, so why does the following occur?
#include <iostream>
int main()
{
using namespace std;
string myString = "Press ENTER to quit program!";
cout << "Come up and C++ me some time." << endl;
printf("Follow this command: %s", myString);
cin.get();
return 0;
}
程序每次运行时,myString
都会打印一个看似随机的 3 个字符的字符串,例如上面的输出.
Each time the program runs, myString
prints a seemingly random string of 3 characters, such as in the output above.
推荐答案
它正在编译,因为 printf
不是类型安全的,因为它使用 C 意义上的可变参数1.printf
没有 std::string
的选项,只有 C 风格的字符串.使用其他东西代替它所期望的绝对不会给你想要的结果.这实际上是未定义的行为,所以任何事情都可能发生.
It's compiling because printf
isn't type safe, since it uses variable arguments in the C sense1. printf
has no option for std::string
, only a C-style string. Using something else in place of what it expects definitely won't give you the results you want. It's actually undefined behaviour, so anything at all could happen.
由于您使用的是 C++,因此解决此问题的最简单方法是使用 std::cout
正常打印它,因为 std::string
通过运算符支持它重载:
The easiest way to fix this, since you're using C++, is printing it normally with std::cout
, since std::string
supports that through operator overloading:
std::cout << "Follow this command: " << myString;
如果由于某种原因需要提取 C 风格的字符串,可以使用 std::string
的 c_str()
方法获取 <以 null 结尾的 code>const char *.使用您的示例:
If, for some reason, you need to extract the C-style string, you can use the c_str()
method of std::string
to get a const char *
that is null-terminated. Using your example:
#include <iostream>
#include <string>
#include <stdio.h>
int main()
{
using namespace std;
string myString = "Press ENTER to quit program!";
cout << "Come up and C++ me some time." << endl;
printf("Follow this command: %s", myString.c_str()); //note the use of c_str
cin.get();
return 0;
}
如果您想要一个类似于 printf
但类型安全的函数,请查看可变参数模板(C++11,自 MSVC12 起在所有主要编译器上都支持).您可以找到一个 这里.我知道在标准库中没有这样的实现,但在 Boost 中可能有,特别是 boost::format
.
If you want a function that is like printf
, but type safe, look into variadic templates (C++11, supported on all major compilers as of MSVC12). You can find an example of one here. There's nothing I know of implemented like that in the standard library, but there might be in Boost, specifically boost::format
.
[1]:这意味着你可以传递任意数量的参数,但函数依赖于你告诉它这些参数的数量和类型.在 printf
的情况下,这意味着具有编码类型信息的字符串,例如 %d
表示 int
.如果你对类型或数字撒谎,该函数没有标准的获知方式,尽管一些编译器能够在你撒谎时检查并发出警告.
[1]: This means that you can pass any number of arguments, but the function relies on you to tell it the number and types of those arguments. In the case of printf
, that means a string with encoded type information like %d
meaning int
. If you lie about the type or number, the function has no standard way of knowing, although some compilers have the ability to check and give warnings when you lie.
这篇关于printf 与 std::string?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:printf 与 std::string?


基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07