错误:'运营商^'不匹配

-1 c++ string

如何在字符串中按位xor?谁能帮我这个...

#include <bits/stdc++.h>
using namespace std;

int main() {
    // your code goes here

    string a="1001";
    string b="1111";
    string c=a^b;
    cout << "c: " << c << "\n";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误:'operator ^'不匹配(操作数类型是'std :: __ cxx11 :: string {aka std :: __ cxx11 :: basic_string}'和'std :: __ cxx11 :: string {aka std :: __ cxx11 :: basic_string }')string c = a ^ b;

Che*_* OT 5

考虑使用std :: bitset,这可能是你正在寻找的.

std::bitset<4> a("1001");
std::bitset<4> b("1111");
std::bitset<4> c = a ^ b;
cout << "c: " << c << "\n";
Run Code Online (Sandbox Code Playgroud)

ideone中看到它

它们可以从您的位字符串初始化,并具有operator^重载以执行XOR操作.还有一个ostream&operator <<(ostream&,const bitset <N>&)用于打印结果std::cout.