| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1428 人关注过本帖
标题:重载运算符 这几个应该怎么使用?
只看楼主 加入收藏
ClearningC
Rank: 2
等 级:论坛游民
帖 子:98
专家分:43
注 册:2016-10-26
结帖率:89.47%
收藏
 问题点数:0 回复次数:3 
重载运算符 这几个应该怎么使用?
operator[]();
operator<() ;
operator<=();
operator>();
operator>=();
operator&=();
operator|=();
operator^=();
operator&();
operator|();
operator[]();
这里有一道题
目的是写出类里面的函数。
程序代码:
#include<iostream>

using namespace std;
int main() {
    bitset a, b;
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        a.set(x);
    }
    cout << "a.count() is " << a.count() << "\n";
    cout << "a.test(5) is " << (a.test(5) ? "true" : "false") << "\n";
    cout << "a.any() is " << (a.any() ? "true" : "false") << "\n";
    cout << "a.none() is " << (a.none() ? "true" : "false") << "\n";
    cout << "a.all() is " << (a.all() ? "true" : "false") << "\n";
    b = ~b;
    for (int i = 0; i < m; i++) {
        int x;
        cin >> x;
        b.reset(x);
    }
    cout << a << "\n";
    cout << b << "\n";
    if (a == b) {
        cout << "hello\n";
    }
    if (a != b) {
        cout << "world\n";
    }
    bitset c;
    // test &
    c = a;
    c &= b;
    cout << c << "\n";
    c = a & b;
    cout << c << "\n";
    // test |
    c = a;
    c |= b;
    cout << c << "\n";
    c = a | b;
    cout << c << "\n";
    // test ^
    c = a;
    c ^= b;
    cout << c << "\n";
    c = a ^ b;
    cout << c << "\n";
    // test <<
    c = a;
    c <<= 2;
    cout << c << "\n";
    c = a << 2;
    cout << c << "\n";
    // test >>
    c = b;
    c >>= 2;
    cout << c << "\n";
    c = b >> 2;
    cout << c << "\n";
    // test []
    for (int i = 0; i < q; i++) {
        int x;
        cin >> x;
        if (a[i])
            cout << "Yes\n";
        else
            cout << "No\n";
    }
}
#define N 5
const int max_length = 32 * N;//这个数字共有32*5位。从第0位到第max_length-1位,第0位是最低位。
class bitset {
private:
int a[N];//一个int32位,所以只用开N个int
public:
bitset();//默认构造函数,所有位初始化为0
void set(int pos);//把位置pos设置成1
void reset(int pos);//将位置pos设置成0
int count() const;//输出一共有多少个为1的位
bool test(int pos) const;//位置pos是否是1
bool any() const;//是否有是1的位
bool none() const;//是否没有是1的位
bool all() const;//是否所有位都是1

//位运算部分和普通数字的位运算相同。
bitset& operator&= (const bitset& b);
bitset& operator|= (const bitset& b);
bitset& operator^= (const bitset& b);
bitset& operator= (const bitset& b);
bitset& operator <<= (int pos);
bitset& operator >>= (int pos);
bitset operator~() const;
bitset operator&(const bitset& b) const;
bitset operator|(const bitset& b) const;
bitset operator^(const bitset& b) const;
bitset operator<<(int pos) const;
bitset operator>>(int pos) const;
bool operator== (const bitset& b) const;
bool operator!= (const bitset& b) const;
bool operator[] (int pos) const;//返回位置pos是不是1.
搜索更多相关主题的帖子: color 
2017-03-31 19:40
ClearningC
Rank: 2
等 级:论坛游民
帖 子:98
专家分:43
注 册:2016-10-26
收藏
得分:0 
除了operator[](),其它的我在网上找到了很有用的东西,感兴趣的可以去看一下。
http://
2017-03-31 22:43
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
重载运算符 这几个应该怎么使用?
你的main函数中都使用了呀,不知道问什么?


2017-04-01 08:58
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
程序代码:
#include <iostream>
#include <bitset>
using namespace std;

int main( void )
{
    std::bitset<5*sizeof(unsigned)*CHAR_BIT> a; // 5*sizeof(unsigned)*CHAR_BI 对应着你源代码中的 32 * N
    cin >> a;

    cout << boolalpha
         << "count() is " << a.count() << '\n'
         << "test(5) is " << a.test(5) << '\n'
         << "any()   is " << a.any()   << '\n'
         << "none()  is " << a.none()  << '\n'
         << "all()   is " << (a.count()==a.size())  << '\n';
    // …… 一样的做法,省略 ……
    cout << a << endl;

    return 0;
}
2017-04-01 10:36
快速回复:重载运算符 这几个应该怎么使用?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018141 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved