printf 与 std::string? [英] printf with std::string?

查看:22
本文介绍了printf 与 std::string?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是stringstd命名空间的成员,那么为什么会出现以下情况呢?

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::stringc_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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆