C++程序员应了解的那些事(74)final关键字 + ostream_iterator(cout,“ “) + C++对and/or/compl 等运算符的支持

【C++11之final关键字】

<1>禁用继承

      C++11中允许将类标记为final,使用时直接在类名称后面使用关键字final,如此,意味着继承该类会导致编译错误。示例如下:

class Super final
{
  //......
};

<2>禁用重写

      C++中还允许将方法标记为fianal,这意味着无法在子类中重写该方法。这时final关键字放在方法参数列表后面,示例如下:

class Super
{
  public:
    Super();
    virtual void SomeMethod() final;
};

【ostream_iterator(cout," ")的含义】

  • Constructs an ostream_iterator that is initialized and delimited(带分隔符的) to write to the output stream.
  • 构造一个带分隔符的ostream_iterator,该迭代器用来初始化输出流(该迭代器用来写入输出流)。
ostream_iterator(
   ostream_type& _Ostr
);
ostream_iterator(
   ostream_type& _Ostr, 
   const CharType* _Delimiter
);

Parameters:
  _Ostr
The output stream object used to initialize the output stream pointer.
  _Delimiter (分隔符)
The output stream delimiter used to initialize the output stream pointer.

Remarks:
  The first constructor initializes the output stream pointer with &_Ostr. The delimiter string pointer designates an empty string.
  The second constructor initializes the output stream pointer with &_Ostr and the delimiter string pointer with _Delimiter.
<示例代码>
#include <iterator>
#include <vector>
#include <iostream>
int main( )
{
    using namespace std;
    // ostream_iterator for stream cout
    ostream_iterator<int> intOut (cout ,"\n" );
    *intOut = 10;
    intOut++;  
    *intOut = 20;
    intOut++; 

    int i;
    vector<int> vec;
    for ( i = 1; i < 7; ++i )
    {
        vec.push_back (  i );
    }
    // Write elements to standard output stream
    cout << "Elements output without delimiter: ";
    copy (vec.begin ( ), vec.end ( ),ostream_iterator<int> (cout) );
    cout << endl;
    // Write elements with delimiter " : " to output stream
    cout << "Elements output with delimiter: ";
    copy (vec.begin ( ), vec.end ( ),ostream_iterator<int> (cout," : "));
    cout << endl;
}
输出:
10
20
Elements output without delimiter: 123456
Elements output with delimiter: 1 : 2 : 3 : 4 : 5 : 6 : 

【C++对and、or、compl 等运算符的支持】

<1>C代码

// test.c
// gcc test.c  --- compile error
#include <stdio.h>
int main()
{
  int a = (0) or (0);
  printf("%d", a);
  return a;
}

上面这段C代码不能编译通过增,加一个头文件iso646.h 后可以编译通过:

// test.c
// gcc test.c  --- compile success
#include <stdio.h>
#include <iso646.h>
int main()
{
   int a = (0) or (0);
   printf("%d", a);
   return a;
}

<2>C++代码

      对于C++,不增加头文件,依然能编译过下面的test.cpp文件:

// test.cpp
// g++ test.cpp     ---- compile success<pre name="code" class="cpp">#include <stdio.h>
#include <stdio.h>
int main()
{
  int a = (0) or (0);
  printf("%d", a);
  return a;
}

compl】 

    compl是一个内置关键字,至少从C ++ 98起就存在。 它是〜 ( Compliment )运算符的替代方法,它主要用于位操作:compl关键字将操作数的所有位取反。

#include <iostream>
#include <bitset>
using namespace std;
 
int main()
{
    //bitsets
    bitset<4> value("1011");
    // before operation
    cout << "value: " << value << endl;

    value = compl value;
    cout << "After operation (1)...\n";
    cout << "value: " << value << endl;
 
    value = compl value;
    cout << "After operation (2)...\n";
    cout << "value: " << value << endl;
    return 0;
}
输出:
value: 1011
After operation (1)...
value: 0100
After operation (2)...
value: 1011

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值