【问题标题】:C++ - Vertical Line Operator. What does it do? [closed]C++ - 垂直线运算符。它有什么作用? [关闭]
【发布时间】:2013-10-12 03:11:39
【问题描述】:

当您将标志传递给函数时,使用运算符“|”有什么作用做什么,它的正确名称是什么?我将如何在我自己的函数中实现这一点?感谢您的帮助。

【问题讨论】:

  • 答案在人类已知的每个运算符优先级图表上。
  • 这是非常基础的。如果你不知道,你应该有一个现成的参考(书或书签)来查看有关基本语言的详细信息。你不应该在这里问。
  • 抱歉,我没想到。下次会研究得更好。

标签: c++ operators


【解决方案1】:

它是按位或。例如:

(1 | 2) == 3
(5 | 3) == 7

【讨论】:

    【解决方案2】:

    嗯,这取决于。垂直线运算符| 绘制一条垂直线,与水平线运算符'-' 绘制一条水平线非常相似。还有兄弟||=分别画两条平行的竖线或横线:

    #include <algorithm>
    #include <iostream>
    #include <iterator>
    
    struct graphic
    {
        void operator-(int n) {
            *std::fill_n(std::ostream_iterator<char>(std::cout), n, '-')++ = '\n';
        }
        void operator=(int n) {
            *std::fill_n(std::ostream_iterator<char>(std::cout), n, '=')++ = '\n';
        }
        void operator|(int n) {
            std::fill_n(std::ostream_iterator<char>(std::cout, "\n"), n, '|');
        }
        void operator||(int n) {
            std::fill_n(std::ostream_iterator<char const*>(std::cout, "\n"), n, "||");
        }
    };
    
    int main()
    {
        graphic g;
        g - 10;
        g = 10;
        g | 4;
        g || 4;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-23
      • 2013-01-15
      • 2017-04-15
      • 1970-01-01
      • 2013-01-21
      • 2012-05-22
      • 2013-08-27
      • 2014-01-14
      相关资源
      最近更新 更多