C++ - 垂直线操作员.它有什么作用

C++ - Vertical Line Operator. What does it do?

本文关键字:什么 作用 操作员 垂直线 C++      更新时间:2023-10-16

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

它是

按位或的。例如:

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

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

#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;
}