0

太好了......我想写这段代码 using namespace std;因为我最近了解到“污染全局命名空间”是不好的做法。

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std; // bad practice, trying to remove

int main() {
    std::ofstream outFile; // instead use explicit namespaces

    /* lots of code */

    // eventually I set some manipulators
    outFile << std::fixed << std::showpoint << std::setprecision(2);

    /* more code */

    // later I wish to unset fixed
    outFile.unsetf(ios::fixed); // <-- this part

我在没有命名空间的情况下做得很好,直到这outFile.unsetf(ios::fixed)只有在我使用命名空间 std 时才有效。我试图在不使用命名空间的情况下编写以下变体:

outFile.unsetf(ios::fixed)
outFile.unsetf(std::fixed)
outFile.unsetf(ios::std::fixed)

命名空间是否ios在命名空间内std?然后,下一个对我来说最有意义,但也不起作用。

outFile.unsetf(std::ios::fixed)

首先,我需要一些帮助来使用显式命名空间来修复这条线。其次,如果我需要弥补一个关键的知识差距,一些帮助识别它,也许一些关键字去查找会有所帮助。

4

1 回答 1

3

使用std::ios_base::fixed

outFile.unsetf(std::ios_base::fixed);
于 2019-09-11T06:10:30.603 回答