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

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

      • <bdo id='VLLwR'></bdo><ul id='VLLwR'></ul>
      <legend id='VLLwR'><style id='VLLwR'><dir id='VLLwR'><q id='VLLwR'></q></dir></style></legend>

      1. <tfoot id='VLLwR'></tfoot>

        “永久"标准::设置

        时间:2023-05-24

            <tbody id='w5OK9'></tbody>

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

          • <legend id='w5OK9'><style id='w5OK9'><dir id='w5OK9'><q id='w5OK9'></q></dir></style></legend>

                • <bdo id='w5OK9'></bdo><ul id='w5OK9'></ul>
                  <tfoot id='w5OK9'></tfoot>
                  <i id='w5OK9'><tr id='w5OK9'><dt id='w5OK9'><q id='w5OK9'><span id='w5OK9'><b id='w5OK9'><form id='w5OK9'><ins id='w5OK9'></ins><ul id='w5OK9'></ul><sub id='w5OK9'></sub></form><legend id='w5OK9'></legend><bdo id='w5OK9'><pre id='w5OK9'><center id='w5OK9'></center></pre></bdo></b><th id='w5OK9'></th></span></q></dt></tr></i><div id='w5OK9'><tfoot id='w5OK9'></tfoot><dl id='w5OK9'><fieldset id='w5OK9'></fieldset></dl></div>
                  本文介绍了“永久"标准::设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  有没有办法永久设置 std::setw 操纵器(或其功能 width)?看看这个:

                  Is there any way how to set std::setw manipulator (or its function width) permanently? Look at this:

                  #include <iostream>
                  #include <iomanip>
                  #include <algorithm>
                  #include <iterator>
                  
                  int main( void )
                  {
                    int array[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256 };
                    std::cout.fill( '0' );
                    std::cout.flags( std::ios::hex );
                    std::cout.width( 3 );
                  
                    std::copy( &array[0], &array[9], std::ostream_iterator<int>( std::cout, " " ) );
                  
                    std::cout << std::endl;
                  
                    for( int i = 0; i < 9; i++ )
                    {
                      std::cout.width( 3 );
                      std::cout << array[i] << " ";
                    }
                    std::cout << std::endl;
                  }
                  

                  运行后,我看到:

                  001 2 4 8 10 20 40 80 100
                  
                  001 002 004 008 010 020 040 080 100
                  

                  即除了必须为每个条目设置的 setw/width 之外,每个操纵符都保持自己的位置.有没有什么优雅的方法可以将 std::copy(或其他东西)与 setw 一起使用?我所说的优雅当然不是指创建自己的函子或函数来将内容写入 std::cout.

                  I.e. every manipulator holds its place except the setw/width which must be set for every entry. Is there any elegant way how to use std::copy (or something else) along with setw? And by elegant I certainly don't mean creating own functor or function for writing stuff into std::cout.

                  推荐答案

                  好吧,这是不可能的.没有办法让它每次都调用 .width .但是你当然可以使用 boost:

                  Well, it's not possible. No way to make it call .width each time again. But you can use boost, of course:

                  #include <boost/function_output_iterator.hpp>
                  #include <boost/lambda/lambda.hpp>
                  #include <algorithm>
                  #include <iostream>
                  #include <iomanip>
                  
                  int main() {
                      using namespace boost::lambda;
                      int a[] = { 1, 2, 3, 4 };
                      std::copy(a, a + 4, 
                          boost::make_function_output_iterator( 
                                var(std::cout) << std::setw(3) << _1)
                          );
                  }
                  

                  确实创建了自己的函子,但它发生在幕后:)

                  It does create its own functor, but it happens behind the scene :)

                  这篇关于“永久"标准::设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:你如何将结构模板标记为朋友? 下一篇:我能在模板参数中声明一个 constexpr lambda 吗?

                  相关文章

                  最新文章

                • <small id='AJ9GI'></small><noframes id='AJ9GI'>

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