C++编程思想 第2卷 第4章 输入输出流 国际化 区域性字符流 基本区域性字符流

不同国家的计算机输出之间最显著的不同
在于分割整数和实数的小数部分 所使用的标点符号

在美国,一个句号表示一个小数点
但是世界上大多数国家用逗号表示小数点

这次抽象是区域
说明了基本区域性字符流的行为

//: C04:Locale.cpp {-g++}{-bor}{-edg} {RunByHand}
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Illustrates effects of locales.
#include <iostream>
#include <locale>
using namespace std;

int main() {
  locale def;
  cout << def.name() << endl;
  locale current = cout.getloc();
  cout << current.name() << endl;
  float val = 1234.56;
  cout << val << endl;
  // Change to French/France
  cout.imbue(locale("french"));
  current = cout.getloc();
  cout << current.name() << endl;
  cout << val << endl;

  cout << "Enter the literal 7890,12: ";
  cin.imbue(cout.getloc());
  cin >> val;
  cout << val << endl;
  cout.imbue(def);
  cout << val << endl;
  getchar();
} ///:~


命令行输出
C
C
1234.56
French_France.1252
1?34,56
Enter the literal 7890,12: 7890.12
7?90
7890

默认的区域为"C"区域
C和C++程序员多年来一直使用
所有的流最初都完全 浸透 imbue 在 C 区域环境下

猜你喜欢

转载自blog.csdn.net/eyetired/article/details/82055103