使用std :: string的printf? [英] printf with std::string?

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

问题描述

我的理解是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终止的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中可能没有实现,特别是

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.

这篇关于使用std :: string的printf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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