格式化浮点数:恢复默认值

时间:2018-10-28 11:09:10

标签: c++ floating-point precision iomanip

在返回“默认格式”的意义上,我遇到了浮点值的格式问题。 假设我有2个彩车:

float f1 = 3.0f, f2 = 1.5f;
std::cout << f1 << " - " << f2 << "\n";

会将其显示为:3 - 1.5

现在,由于某种原因,我需要在std::cout上设置精度(用于其他打印):

cout << std::precision(2);

如果我再次打印两个浮子,将导致:3.00 - 1.50

现在,我想恢复默认格式。在C ++ 11之前,这似乎很困难(或者是?)。但是,谢谢,我现在得到了这个新标志: std::defaultfloat。让我们尝试一下:

std::cout << std::defaultfloat << f1 << " - " << f2;

将打印:3 - 1.50。很好

哦,等一下。假设我改为:

float f1 = 444.0f, f2 = 444.5f;

默认打印将显示:444 - 444.5

设置精度(和“固定”):

cout << std::precision(2) << std::fixed;

将显示:444.00 - 444.50

但返回到“默认”:

std::cout << std::defaultfloat << f1 << " - " << f2;

将打印:4.4e+02 - 4.4e+02(自动切换为科学格式)。而且,以防万一,添加“ fixed”标志将保持先前分配的精度,从而不会返回到原始设置。

问题:如何返回默认模式?

FWIW,live code is here

编辑:此问题已被标记为重复,但linked answer并未提供该问题的答案,仅提及如何获得当前的精度。

编辑2 :根据要求,以下是演示此问题的完整代码:

int main()
{
    float f1 = 444.5f, f2=443.0f;
    std::cout << f1 << " - " << f2 << "\n";
    std::cout << std::fixed << std::setprecision(2);
    std::cout << f1 << " - " << f2 << "\n";
    std::cout << std::defaultfloat;
    std::cout << f1 << " - " << f2 << "\n";
}

结果:

444.5 - 443
444.50 - 443.00
4.4e+02 - 4.4e+02

4 个答案:

答案 0 :(得分:6)

在 C++20 中,您可以使用 std::format,这是一个无状态 API。特别是,在一个调用中指定精度不会影响另一个:

float f1 = 444.0f, f2 = 444.5f;
std::cout << std::format("{} - {}\n", f1, f2);
// Output: 444 - 444.5
std::cout << std::format("{:.2f} - {:.2f}\n", f1, f2);
// Output: 444.00 - 444.50
std::cout << std::format("{} - {}\n", f1, f2);
// Output: 444 - 444.5

std::format 尚未广泛使用,但您可以同时使用 the {fmt} librarystd::format 是基于。它还提供了一个 print 函数,它结合了格式化和 I/O:

float f1 = 444.0f, f2 = 444.5f;
fmt::print("{} - {}\n", f1, f2);
// Output: 444 - 444.5
fmt::print("{:.2f} - {:.2f}\n", f1, f2);
// Output: 444.00 - 444.50
fmt::print("{} - {}\n", f1, f2);
// Output: 444 - 444.5

免责声明:我是 {fmt} 和 C++20 std::format 的作者。

答案 1 :(得分:1)

std :: precision指定cout显示的精度,更改格式时不会更改。

因此,当您设置std :: precision(2)时,它指定以下所有格式将以应用的精度显示。

固定格式应用精度,因为n是小数点后的位数。 defaultfloat格式将精度作为要显示的最大总位数。

它不会重置精度值。

供参考: ios_base::precision

答案 2 :(得分:1)

std::defaultfloat不会重置精度。 (不要问我为什么)。您可以将其重置为定义为6的默认值:

std::cout << std::defaultfloat << std::setprecision(6) << f1 << " - " << f2;

或者,您可以在操作之前保存整个流状态,然后在操作之后恢复它。参见this thread

答案 3 :(得分:1)

您可以简单地在原始 Launching lib\main.dart on SM A115F in debug mode... Running Gradle task 'assembleDebug'... ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/path_provider_windows-0.0.4+3/lib/src/path_provider_windows_real.dart:126:25: Error: The getter 'addressOf' isn't defined for the class 'GUID'. - 'GUID' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. knownFolderID.addressOf, KF_FLAG_DEFAULT, NULL, pathPtrPtr); ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:1067:7: Error: The getter 'addressOf' isn't defined for the class 'LOGFONT'. - 'LOGFONT' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. addressOf.cast<Uint8>().elementAt(28).cast<Utf16>(); ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:1112:29: Error: The getter 'addressOf' isn't defined for the class 'ENUMLOGFONTEX'. - 'ENUMLOGFONTEX' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. LOGFONT get elfLogFont => addressOf.cast<LOGFONT>().ref; ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:1114:29: Error: The getter 'addressOf' isn't defined for the class 'ENUMLOGFONTEX'. - 'ENUMLOGFONTEX' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. String get elfFullName => addressOf ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:1120:26: Error: The getter 'addressOf' isn't defined for the class 'ENUMLOGFONTEX'. - 'ENUMLOGFONTEX' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. String get elfStyle => addressOf ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:1126:27: Error: The getter 'addressOf' isn't defined for the class 'ENUMLOGFONTEX'. - 'ENUMLOGFONTEX' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. String get elfScript => addressOf ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:2688:7: Error: The getter 'addressOf' isn't defined for the class 'OSVERSIONINFO'. - 'OSVERSIONINFO' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. addressOf.cast<Uint8>().elementAt(20).cast<Utf16>().unpackString(128); ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:2699:11: Error: The getter 'addressOf' isn't defined for the class 'OSVERSIONINFO'. - 'OSVERSIONINFO' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. ..addressOf.cast<Uint8>().elementAt(20).value = 0; ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:2744:24: Error: The getter 'addressOf' isn't defined for the class 'BLUETOOTH_DEVICE_INFO'. - 'BLUETOOTH_DEVICE_INFO' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. String get szName => addressOf ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:2837:7: Error: The getter 'addressOf' isn't defined for the class 'BLUETOOTH_PIN_INFO'. - 'BLUETOOTH_PIN_INFO' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. addressOf.cast<Uint8>().elementAt(BTH_MAX_PIN_SIZE).value; ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:2840:5: Error: The getter 'addressOf' isn't defined for the class 'BLUETOOTH_PIN_INFO'. - 'BLUETOOTH_PIN_INFO' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. addressOf.cast<Uint8>().elementAt(BTH_MAX_PIN_SIZE).value = ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:2845:28: Error: The getter 'addressOf' isn't defined for the class 'BLUETOOTH_PIN_INFO'. - 'BLUETOOTH_PIN_INFO' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. String.fromCharCodes(addressOf.cast<Uint8>().asTypedList(pinLength)); ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:2853:7: Error: The getter 'addressOf' isn't defined for the class 'BLUETOOTH_PIN_INFO'. - 'BLUETOOTH_PIN_INFO' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. addressOf.cast<Uint8>().elementAt(idx).value = pinData[idx]; ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/com/combase.dart:38:31: Error: The getter 'addressOf' isn't defined for the class 'GUID'. - 'GUID' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. final iid = GUID.allocate().addressOf; ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/com/combase.dart:56:33: Error: The getter 'addressOf' isn't defined for the class 'GUID'. - 'GUID' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. final clsid = GUID.allocate().addressOf; ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/winrt/winrt_helpers.dart:86:40: Error: The getter 'addressOf' isn't defined for the class 'GUID'. - 'GUID' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. final riidCalendar = GUID.allocate().addressOf; ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/generated/IDesktopWallpaper.dart:197:38: Error: The getter 'addressOf' isn't defined for the class 'COMObject'. - 'COMObject' is from 'package:win32/src/com/combase.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/com/combase.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. final ptr = COMObject.allocate().addressOf; ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/generated/IDesktopWallpaper.dart:200:49: Error: The getter 'addressOf' isn't defined for the class 'GUID'. - 'GUID' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. GUID.fromString(CLSID_DesktopWallpaper).addressOf, ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/generated/IDesktopWallpaper.dart:203:48: Error: The getter 'addressOf' isn't defined for the class 'GUID'. - 'GUID' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. GUID.fromString(IID_IDesktopWallpaper).addressOf, ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/generated/IFileOpenDialog.dart:58:38: Error: The getter 'addressOf' isn't defined for the class 'COMObject'. - 'COMObject' is from 'package:win32/src/com/combase.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/com/combase.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. final ptr = COMObject.allocate().addressOf; ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/generated/IFileOpenDialog.dart:61:47: Error: The getter 'addressOf' isn't defined for the class 'GUID'. - 'GUID' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. GUID.fromString(CLSID_FileOpenDialog).addressOf, ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/generated/IFileOpenDialog.dart:64:46: Error: The getter 'addressOf' isn't defined for the class 'GUID'. - 'GUID' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. GUID.fromString(IID_IFileOpenDialog).addressOf, ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/generated/IFileSaveDialog.dart:99:38: Error: The getter 'addressOf' isn't defined for the class 'COMObject'. - 'COMObject' is from 'package:win32/src/com/combase.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/com/combase.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. final ptr = COMObject.allocate().addressOf; ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/generated/IFileSaveDialog.dart:102:47: Error: The getter 'addressOf' isn't defined for the class 'GUID'. - 'GUID' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. GUID.fromString(CLSID_FileSaveDialog).addressOf, ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/generated/IFileSaveDialog.dart:105:46: Error: The getter 'addressOf' isn't defined for the class 'GUID'. - 'GUID' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. GUID.fromString(IID_IFileSaveDialog).addressOf, ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/generated/IKnownFolderManager.dart:164:38: Error: The getter 'addressOf' isn't defined for the class 'COMObject'. - 'COMObject' is from 'package:win32/src/com/combase.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/com/combase.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. final ptr = COMObject.allocate().addressOf; ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/generated/IKnownFolderManager.dart:167:51: Error: The getter 'addressOf' isn't defined for the class 'GUID'. - 'GUID' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. GUID.fromString(CLSID_KnownFolderManager).addressOf, ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/generated/IKnownFolderManager.dart:170:50: Error: The getter 'addressOf' isn't defined for the class 'GUID'. - 'GUID' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. GUID.fromString(IID_IKnownFolderManager).addressOf, ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/generated/INetworkListManager.dart:128:38: Error: The getter 'addressOf' isn't defined for the class 'COMObject'. - 'COMObject' is from 'package:win32/src/com/combase.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/com/combase.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. final ptr = COMObject.allocate().addressOf; ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/generated/INetworkListManager.dart:131:51: Error: The getter 'addressOf' isn't defined for the class 'GUID'. - 'GUID' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. GUID.fromString(CLSID_NetworkListManager).addressOf, ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/generated/INetworkListManager.dart:134:50: Error: The getter 'addressOf' isn't defined for the class 'GUID'. - 'GUID' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. GUID.fromString(IID_INetworkListManager).addressOf, ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/generated/IWbemLocator.dart:82:38: Error: The getter 'addressOf' isn't defined for the class 'COMObject'. - 'COMObject' is from 'package:win32/src/com/combase.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/com/combase.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. final ptr = COMObject.allocate().addressOf; ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/generated/IWbemLocator.dart:84:68: Error: The getter 'addressOf' isn't defined for the class 'GUID'. - 'GUID' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. final hr = CoCreateInstance(GUID.fromString(CLSID_WbemLocator).addressOf, ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/generated/IWbemLocator.dart:85:64: Error: The getter 'addressOf' isn't defined for the class 'GUID'. - 'GUID' is from 'package:win32/src/structs.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. nullptr, CLSCTX_ALL, GUID.fromString(IID_IWbemLocator).addressOf, ptr); ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/ffi-0.1.3/lib/src/utf8.dart:63:33: Error: The getter 'addressOf' isn't defined for the class 'Utf8'. - 'Utf8' is from 'package:ffi/src/utf8.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/ffi-0.1.3/lib/src/utf8.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'addressOf'. String toString() => fromUtf8(addressOf); ^^^^^^^^^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:1111:7: Error: Struct 'ENUMLOGFONTEX' is empty. Empty structs are undefined behavior. class ENUMLOGFONTEX extends Struct { ^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:2835:7: Error: Struct 'BLUETOOTH_PIN_INFO' is empty. Empty structs are undefined behavior. class BLUETOOTH_PIN_INFO extends Struct { ^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:2960:7: Error: Struct 'EXCEPINFO' is empty. Empty structs are undefined behavior. class EXCEPINFO extends Struct {} ^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:2966:7: Error: Struct 'PROPERTYKEY' is empty. Empty structs are undefined behavior. class PROPERTYKEY extends Struct {} ^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:2973:7: Error: Struct 'PROPVARIANT' is empty. Empty structs are undefined behavior. class PROPVARIANT extends Struct {} ^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:2978:7: Error: Struct 'SAFEARRAY' is empty. Empty structs are undefined behavior. class SAFEARRAY extends Struct {} ^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:2985:7: Error: Struct 'CLSID' is empty. Empty structs are undefined behavior. class CLSID extends Struct {} ^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:2992:7: Error: Struct 'STATSTG' is empty. Empty structs are undefined behavior. class STATSTG extends Struct {} ^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/win32-1.7.4/lib/src/structs.dart:2999:7: Error: Struct 'NLM_SIMULATED_PROFILE_INFO' is empty. Empty structs are undefined behavior. class NLM_SIMULATED_PROFILE_INFO extends Struct {} ^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/ffi-0.1.3/lib/src/utf8.dart:23:7: Error: Struct 'Utf8' is empty. Empty structs are undefined behavior. class Utf8 extends Struct { ^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/ffi-0.1.3/lib/src/utf16.dart:16:7: Error: Struct 'Utf16' is empty. Empty structs are undefined behavior. class Utf16 extends Struct { ^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/ffi-0.1.3/lib/src/allocation.dart:47:33: Error: Expected type 'T' to be a valid and instantiated subtype of 'NativeType'. final int totalSize = count * sizeOf<T>(); ^ FAILURE: Build failed with an exception. * Where: Script 'C:\Users\me\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1035 * What went wrong: Execution failed for task ':app:compileFlutterBuildDebug'. > Process 'command 'C:\Users\me\flutter\bin\flutter.bat'' finished with non-zero exit value 1 * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 24m 43s std::ostream 上创建您自己的 std::streambuf,并在这个新的个人 std::ostream 上设置格式。

std::ostream

此代码将打印:

#include <iostream>
#include <ostream>
#include <iomanip>

float const f1 = 3.0f, f2 = 1.5f;

void proc1() {
    std::ostream out(std::cout.rdbuf());
    out << "Inside proc1(), floats are printed like: " << std::setprecision(2) << std::fixed << f1 << " and " << f2 << '\n';
}

int main() {
    std::cout << "Inside main(), floats are printed like: " << f1 << " and " << f2 << '\n';
    proc1();
    std::cout << "Back in main(), floats are printed like: " << f1 << " and " << f2 << '\n';
}

您可以在那里尝试:http://coliru.stacked-crooked.com/a/4994c3f604b63d67

基于另一个 Inside main(), floats are printed like: 3 and 1.5 Inside proc1(), floats are printed like: 3.00 and 1.50 Back in main(), floats are printed like: 3 and 1.5 创建一个 std::ostream 是正确的,因为 std::streambuf 的析构函数不会触及其分配的 std::ostream。请参阅:https://en.cppreference.com/w/cpp/io/basic_ostream/%7Ebasic_ostream