<small id='E39N4'></small><noframes id='E39N4'>

          <bdo id='E39N4'></bdo><ul id='E39N4'></ul>

      1. <legend id='E39N4'><style id='E39N4'><dir id='E39N4'><q id='E39N4'></q></dir></style></legend>
        <tfoot id='E39N4'></tfoot>
      2. <i id='E39N4'><tr id='E39N4'><dt id='E39N4'><q id='E39N4'><span id='E39N4'><b id='E39N4'><form id='E39N4'><ins id='E39N4'></ins><ul id='E39N4'></ul><sub id='E39N4'></sub></form><legend id='E39N4'></legend><bdo id='E39N4'><pre id='E39N4'><center id='E39N4'></center></pre></bdo></b><th id='E39N4'></th></span></q></dt></tr></i><div id='E39N4'><tfoot id='E39N4'></tfoot><dl id='E39N4'><fieldset id='E39N4'></fieldset></dl></div>

        cout uint8_t 作为整数而不是字符

        时间:2024-08-13
          <tbody id='DQwZ7'></tbody>

        <tfoot id='DQwZ7'></tfoot>
          <legend id='DQwZ7'><style id='DQwZ7'><dir id='DQwZ7'><q id='DQwZ7'></q></dir></style></legend>

            <bdo id='DQwZ7'></bdo><ul id='DQwZ7'></ul>

              <small id='DQwZ7'></small><noframes id='DQwZ7'>

                  <i id='DQwZ7'><tr id='DQwZ7'><dt id='DQwZ7'><q id='DQwZ7'><span id='DQwZ7'><b id='DQwZ7'><form id='DQwZ7'><ins id='DQwZ7'></ins><ul id='DQwZ7'></ul><sub id='DQwZ7'></sub></form><legend id='DQwZ7'></legend><bdo id='DQwZ7'><pre id='DQwZ7'><center id='DQwZ7'></center></pre></bdo></b><th id='DQwZ7'></th></span></q></dt></tr></i><div id='DQwZ7'><tfoot id='DQwZ7'></tfoot><dl id='DQwZ7'><fieldset id='DQwZ7'></fieldset></dl></div>
                  本文介绍了cout uint8_t 作为整数而不是字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  #include<algorithm>
                  #include<iostream>
                  using namespace std;
                  
                  int main()
                  {
                      cout << (uint8_t)123 << endl;
                  }
                  

                  这将输出 { ,因为 { 的 ASCII 是 123.

                  This will output { , since {'s ASCII is 123.

                  但我想得到 123 代替.我发现 cout <<(int)123< 会这样做,但我不愿意每次都将 uint_8 转换为 int .我可以配置 cout 来实现这一点吗?

                  But I want to get 123 instead. I found cout << (int)123 << endl; will do this, but I'm not willing to cast uint_8 to int every times. Can I configure cout to achieve this?

                  推荐答案

                  我绝对不会容忍我即将提出的解决方案.我也怀疑它可能不被标准所允许,但到目前为止我还不能证明这一点.如果有人可以向我提供表明不允许这样做的参考,那么我将删除此答案.无论如何,到目前为止,我的测试表明,在全局范围内简单地重载运算符似乎是可行的.

                  I definitely do not condone the solution I am about to suggest. I also suspect that it may not be permitted by the standard, but I cannot prove it, as of yet. If someone can provide me a reference that shows that it is not permitted, then I will delete this answer. Anyway, my tests so far indicate that simply overloading the operator in the global scope seems to work.

                  #include <iostream>
                  #include <cstdint>
                  
                  std::ostream & operator<<(std::ostream & os, std::uint8_t val)
                  {
                      return os << static_cast<int>(val);
                  }
                  
                  int main()
                  {
                      std::uint8_t val = 123;
                      std::cout << val;
                  }
                  

                  我没想到这会起作用,但后来我意识到 operator<<char/unsigned char/signed char 重载都是免费函数在 ADL 选取的 std 命名空间中.我想全局函数被认为比 ADL 函数更匹配,但我不确定这一点.

                  I wouldn't have thought this would work, but then I realized that the char/unsigned char/signed char overloads for operator<< are all free functions in the std namespace picked up by ADL. And I guess global functions are considered a better match than ADL functions, but I'm not sure about that.

                  这篇关于cout uint8_t 作为整数而不是字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  1. <small id='qBGzq'></small><noframes id='qBGzq'>

                      • <bdo id='qBGzq'></bdo><ul id='qBGzq'></ul>
                          <tbody id='qBGzq'></tbody>
                      • <legend id='qBGzq'><style id='qBGzq'><dir id='qBGzq'><q id='qBGzq'></q></dir></style></legend>
                      • <i id='qBGzq'><tr id='qBGzq'><dt id='qBGzq'><q id='qBGzq'><span id='qBGzq'><b id='qBGzq'><form id='qBGzq'><ins id='qBGzq'></ins><ul id='qBGzq'></ul><sub id='qBGzq'></sub></form><legend id='qBGzq'></legend><bdo id='qBGzq'><pre id='qBGzq'><center id='qBGzq'></center></pre></bdo></b><th id='qBGzq'></th></span></q></dt></tr></i><div id='qBGzq'><tfoot id='qBGzq'></tfoot><dl id='qBGzq'><fieldset id='qBGzq'></fieldset></dl></div>
                        <tfoot id='qBGzq'></tfoot>