std::setbase
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <iomanip> 中定义
|
||
/*unspecified*/ setbase( int base ); |
||
当在表达式中使用out << setbase(base)或in >> setbase(base),改变了
basefield
标志的流out
或in
,根据的价值base
:价值16套basefield
std::ios_base::hex,价值8套std::ios_base::oct,价值10套std::ios_base::dec.原文:
When used in an expression out << setbase(base) or in >> setbase(base), changes the
basefield
flag of the stream out
or in
, depending on the value of base
: the value 16 sets basefield
to std::ios_base::hex, the value 8 sets std::ios_base::oct, the value 10 sets std::ios_base::dec.base
其他的值大于8,10,或16复位到零,这对应于十进制的输出和前缀相关的输入basefield
.原文:
Values of
base
other than 8, 10, or 16 reset basefield
to zero, which corresponds to decimal output and prefix-dependent input.目录 |
[编辑] 参数
base | - | 新的价值basefield
|
[编辑] 返回值
返回一个对象的类型不明确等,如果
str
的名字是一个输出流的类型std::basic_ostream<CharT, Traits>或std::basic_istream<CharT, Traits>,然后表达str << setbase(base)或str >> setbase(base)的行为就好像下面的代码被执行原文:
Returns an object of unspecified type such that if
str
is the name of an output stream of type std::basic_ostream<CharT, Traits> or std::basic_istream<CharT, Traits>, then the expression str << setbase(base) or str >> setbase(base) behaves as if the following code was executed:str.setf(base == 8 ? std::ios_base::oct :
base == 10 ? std::ios_base::dec :
base == 16 ? std::ios_base::hex :
std::ios_base::fmtflags(0),
std::ios_base::basefield);
[编辑] 示例
#include <iostream> #include <sstream> #include <iomanip> int main() { std::cout << "Parsing string \"10 0x10 010\"\n"; int n1, n2, n3; std::istringstream s("10 0x10 010"); s >> std::setbase(16) >> n1 >> n2 >> n3; std::cout << "hexadecimal parse: " << n1 << ' ' << n2 << ' ' << n3 << '\n'; s.clear(); s.seekg(0); s >> std::setbase(0) >> n1 >> n2 >> n3; std::cout << "prefix-dependent parse: " << n1 << ' ' << n2 << ' ' << n3 << '\n'; std::cout << "hex output: " << std::setbase(16) << std::showbase << n1 << ' ' << n2 << ' ' << n3 << '\n'; }
输出:
Parsing string "10 0x10 010" hexadecimal parse: 16 16 16 prefix-dependent parse: 10 16 8 hex output: 0xa 0x10 0x8
[编辑] 另请参阅
改变用于整数I / O基地 原文: changes the base used for integer I/O (函数) | |
控制是否前缀用于指示数字基 原文: controls whether prefix is used to indicate numeric base (函数) |