C++ A c++;以二进制序列存储每个位1的位置的程序
我制作了这段代码来存储二进制序列中输入的每一位1的位置。程序的输出不是所需的。我得到的10100的输出是0x7fff9109be00。代码如下:C++ A c++;以二进制序列存储每个位1的位置的程序,c++,C++,我制作了这段代码来存储二进制序列中输入的每一位1的位置。程序的输出不是所需的。我得到的10100的输出是0x7fff9109be00。代码如下: #include <iostream> #include <bitset> using namespace std; int main() { bitset <5> inpSeq; int x = 0; int xorArray[x]; unsigned in
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset <5> inpSeq;
int x = 0;
int xorArray[x];
unsigned int i;
cout << "Enter a 5-bit sequence: \n";
cin >> inpSeq;
for ( i = 0; i < inpSeq.size(); i++)
{
if ( inpSeq[i] == 1 )
{
x = x+1;
xorArray[x] = i;
}
}
cout << xorArray << "\n";
}
#包括
#包括
使用名称空间std;
int main()
{
位集inpSeq;
int x=0;
int-xorArray[x];
无符号整数i;
cout>inpSeq;
对于(i=0;i cout我有点不清楚你到底想实现什么,但我认为下面的内容可能会有所帮助
#include <iostream>
#include <bitset>
#include <list>
using namespace std;
int main() {
bitset<5> inpSeq;
unsigned int i;
list<int> xorList;
cout << "Enter a 5-bit sequence: \n";
cin >> inpSeq;
for (i = 0; i < inpSeq.size(); ++i) {
if (inpSeq[i] == 1) {
xorList.push_back(i);
}
}
for (list<int>::iterator list_iter = xorList.begin();
list_iter != xorList.end(); list_iter++)
{
cout << *list_iter << endl;
}
return 0;
}
希望这能有所帮助!我不太清楚您到底想实现什么,但我认为以下内容可能会有所帮助
#include <iostream>
#include <bitset>
#include <list>
using namespace std;
int main() {
bitset<5> inpSeq;
unsigned int i;
list<int> xorList;
cout << "Enter a 5-bit sequence: \n";
cin >> inpSeq;
for (i = 0; i < inpSeq.size(); ++i) {
if (inpSeq[i] == 1) {
xorList.push_back(i);
}
}
for (list<int>::iterator list_iter = xorList.begin();
list_iter != xorList.end(); list_iter++)
{
cout << *list_iter << endl;
}
return 0;
}
cout << xorArray << "\n";
希望这有帮助
cout << xorArray << "\n";
你的另一个问题是你试图使用一个不存在于C++中的可变长度数组。使用矢量代替。
现在:
#包括
#包括
#包括
使用名称空间std;
int main()
{
位集inpSeq(“10111”);
向量xorArray;
for(无符号整数i=0;i你的另一个问题是你试图使用一个不存在于C++中的可变长度数组。使用矢量代替。
现在:
#包括
#包括
#包括
使用名称空间std;
int main()
{
位集inpSeq(“10111”);
向量xorArray;
for(无符号整数i=0;i
难道你不想让我们猜猜你想要什么,或者你打算在午夜告诉我们吗?我想你正在打印数组的内存地址。如果我不够清楚,很抱歉。输出是xorArray,应该是序列中位1的位置列表。所以如果我输入10100,输出应该是2和4。@MohamedAhmed:你的假设那难道你不想让我们猜猜你想要什么,或者你打算在午夜告诉我们吗?我想你正在打印数组的内存地址。如果我不够清楚,很抱歉。输出是xorArray,应该是序列中位1的位置列表。所以如果我输入10100,输出应该是2和4。@MohamedAhmed:你r假设我可以使用循环中存储的位置对这些位置指向的位进行异或操作吗。我的最终目标是一个线性反馈移位寄存器。你的代码很完美,但我真的需要知道我的代码有什么问题。我已经更新了我的答案,解释了我的代码以及从原始po中需要做的更改我想使用循环中存储的位置对这些位置所指向的位进行异或运算。我的最终目标是一个线性反馈移位寄存器。你的代码很完美,但我真的需要知道我的代码有什么问题。我更新了我的答案,解释了我的代码以及从你最初的帖子中需要做的更改。我不熟悉你可以提供链接或简短的解释吗?!@ MaHaMead AHMMD:不,你不是。你已经做过了。它意味着循环一些东西。你的C++书籍确实覆盖了它。@ MaMaMead认为迭代器是指向容器中元素的指针式变量:<代码>
为您提供第一个元素,.end()
在容器结束后为您提供打开。@LightnessRacesinOrbit在您的初始响应中,您说“迭代它”,所以我很困惑。谢谢你的代码。@LightnessRacesinOrbit我不是用C++11,我用的是C++98。我不知道我为什么要用它!我只是在Ubuntu上用“g++-Wall-o”来编译代码。我想你提到的简单方法应该是xorArray.size而不是xorArray.length。我不熟悉迭代的概念。请你提供一个linK?还是简要的解释?!“MaHaMead AHMD:不,你不是。你已经做过了。它意味着循环某个东西。你的C++书籍确实覆盖了它。”MaMaMead认为迭代器是指向容器中的元素的指针式变量:<代码>。
在容器结束后为您打开。@LightnessRacesinOrbit在您的初始响应中说“迭代它”,所以我很困惑。谢谢你的代码。@LightnessRacesinOrbit我不是用C++11,我用的是C++98。我不知道我为什么要用它!我只是在Ubuntu上用“g++-Wall-o”编译代码。我认为你提到的天真方式应该是xorArray.size而不是xorArray.length。
for (auto x : xorArray)
cout << x << ' ';
cout << '\n';
#include <iostream>
#include <bitset>
#include <vector>
using namespace std;
int main()
{
bitset<5> inpSeq("10111");
std::vector<int> xorArray;
for (unsigned int i = 0; i < inpSeq.size(); i++) {
if (inpSeq[i] == 1)
xorArray.push_back(i);
}
for (auto x : xorArray)
cout << x << ' ';
cout << '\n';
}
for (std::vector<int>::const_iterator it = xorArray.begin(),
end = xorArray.end(),
it != end; ++it) {
cout << *it << ' ';
}
for (unsigned int i = 0; i < xorArray.size(); i++)
cout << xorArray[i] << ' ';