c/c++代码 No.2 位与

本文介绍了一个使用C++实现的简单程序,该程序能够将字符型数据转换为二进制字符串并将其再转换回原始的十进制数值。通过具体的实例展示了如何进行位操作,包括按位与运算。此外,还提供了对C++标准库中用于格式化输出的setw和setfill函数的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <cstring>
#include <iomanip> 

using namespace std;

char* getBit(unsigned char n) {
    char *str = new char[101];
    int i = 0;
    while (n != 0) {
        str[i++] = n % 2 + '0';
        n /= 2;
    }
    str[i] = '\0';
    strrev(str); 
    return str;
}

char toBit(char *str) {
    char n = 0;
    for (int i = 0; str[i] != '\0'; i++) {
        n *= 2;
        n += str[i] - '0';
    }
    return n;
}

int main(void) {
    char a = 2;
    char b = 3; 

    cout << "a:" << endl;
    char *str = getBit(a);
    cout << "二进制:" << setfill('0') << setw(8) << str << endl;
    cout << "十进制:" << (int)toBit(str) << endl;
    delete str;
    cout << "b:" << endl;
    str = getBit(b);
    cout << "二进制:" << setfill('0') << setw(8) << str << endl;
    cout << "十进制:" << (int)toBit(str) << endl;
    delete str;

    cout << "a & b" << endl;
    str = getBit(a & b);
    cout << "二进制:" << setfill('0') << setw(8) << str << endl;
    cout << "十进制:" << (int)toBit(str) << endl;
    delete str;
    return 0;
}
这段代码string restoreString(string s, vector<int>& indices) { unordered_map<int,char> hash; string str; for(int i = 0; i < indices.size(); i++){ hash[i] = s[indices[i]]; } for(auto it = hash.begin(); it !=hash.end(); it++){ str.append(it->second); } return str; } 为什么会报错Line 10: Char 17: error: no matching member function for call to 'append' 10 | str.append(it->second); | ~~~~^~~~~~ /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/bits/basic_string.h:1480:7: note: candidate function not viable: no known conversion from 'char' to 'const char *' for 1st argument; take the address of the argument with & 1471 | append(const _CharT* __s) | ^ ~~~~~~~~~~~~~~~~~ /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/bits/basic_string.h:1435:7: note: candidate function not viable: no known conversion from 'char' to 'const basic_string<char>' for 1st argument 1426 | append(const basic_string& __str) | ^ ~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/bits/basic_string.h:1509:7: note: candidate function not viable: no known conversion from 'char' to 'initializer_list<char>' for 1st argument 1500 | append(initializer_list<_CharT> __l) | ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/bits/basic_string.h:1541:9: note: candidate template ignored: requirement '__and_<std::is_convertible<const char &, std::basic_string_view<char, std::char_traits<char>>>, std::__not_<std::is_convertible<const char *, const std::basic_string<char, std::char_traits<char>, std::allocator<char>> *>>, std::__not_<std::is_convertible<const char &, const char *>>>::value' was not satisfied [with _Tp = char] 1532 | append(const _Tp& __svt) | ^ /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/bits/basic_string.h:1466:7: note: candidate function not viable: requires 2 arguments, but 1 was provided 1457 | append(const _CharT* __s, size_type __n) | ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/bits/basic_string.h:1498:7: note: candidate function not viable: requires 2 arguments, but 1 was provided 1489 | append(size_type __n, _CharT __c) | ^ ~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/bits/basic_string.h:1529:9: note: candidate function template not viable: requires 2 arguments, but 1 was provided 1520 | append(_InputIterator __first, _InputIterator __last) | ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/bits/basic_string.h:1453:7: note: candidate function not viable: requires at least 2 arguments, but 1 was provided 1444 | append(const basic_string& __str, size_type __pos, size_type __n = npos) | ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/bits/basic_string.h:1557:2: note: candidate function template not viable: requires at least 2 arguments, but 1 was provided 1548 | append(const _Tp& __svt, size_type __pos, size_type __n = npos) | ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1 error generated.
最新发布
06-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值