Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在bool数组中获取输入_C++_Arrays_Boolean - Fatal编程技术网

C++ 在bool数组中获取输入

C++ 在bool数组中获取输入,c++,arrays,boolean,C++,Arrays,Boolean,我想在布尔数组中获取输入 bool bmp[32]; 这将是程序交互 Enter binary number : 10101 我想在bool-like数组中存储用户输入的“10101” bmp[32]={1,0,1,0,1}; 请帮忙 这应该行得通,但下次你可以自己尝试一下(并发布你尝试过的代码) bool b[32]; std::string str=“10101”; 对于(std::string::size_type i=0U;i p>因为这是C++,我们使用: std::cout>

我想在布尔数组中获取输入

bool bmp[32];
这将是程序交互

 Enter binary number : 10101
我想在bool-like数组中存储用户输入的“10101”

bmp[32]={1,0,1,0,1};

请帮忙

这应该行得通,但下次你可以自己尝试一下(并发布你尝试过的代码)

bool b[32];
std::string str=“10101”;
对于(std::string::size_type i=0U;i
或许

std::vector< bool > b;
std::string str = "10101";
b.reserve( str.length() );
for ( const char c : str )
    b.push_back( c );
std::vectorb;
std::string str=“10101”;
b、 保留长度(str.length());
for(常量字符c:str)
b、 推回(c);

<代码> > p>因为这是C++,我们使用:

std::cout>b;

它不像您要求的那样是一个
bool
数组,但它要好得多。

没什么特别的,只需读取数据并将其存储到数组中,如下所示:

#include <string>
#include <cstdio>

int main() {
    std::string str;
    std::cout << "Enter binary number : ";
    std::cin >> str;
    bool b[32];
    std::size_t size = 0;
    for (auto c : str) {
        b[size++] = c == '1';
    }

    // you are all set now.

    return 0;
}
#包括
#包括
int main(){
std::字符串str;
std::cout>str;
布尔b[32];
std::size\u t size=0;
用于(自动c:str){
b[size++]=c==1';
}
//你现在都准备好了。
返回0;
}

…或者更糟的方法=)在我个人看来,我会使用一个
int
作为本例的二进制掩码。@Anton如果你想要132位而不是32位,那就另当别论了。我的意思是,解决方案在很大程度上取决于任务,当您需要在这些位上做一些工作时,std::bitset并不总是正确的选择。
std::cout << "Enter binary number : ";

std::bitset<32> b;
std::cin >> b;
#include <string>
#include <cstdio>

int main() {
    std::string str;
    std::cout << "Enter binary number : ";
    std::cin >> str;
    bool b[32];
    std::size_t size = 0;
    for (auto c : str) {
        b[size++] = c == '1';
    }

    // you are all set now.

    return 0;
}