Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

c++ - storing integer values in a bool variable

I'm trying to store integer values in a boolean array, however when I print the values out, they only come out as 1's and 0's which I assume represent true and false. Is there any way to store values greater than 1 or less than 0 in a bool? Thanks in advance for the help! :D

Here's a sample of what I'm trying to do:

#include <iostream>
#include <string>

using namespace std;

int main(){

    bool set[2];

    set[0] = 7;
    set[1] = 13;

    string setNotation = "{"+to_string(set[0])+", "+to_string(set[1])+"}";

    cout << setNotation << endl; //prints out as {1, 1} instead of {7, 13}

    //note: I have to use to_string to append and not cout << "{" << set[0] << ...
    //because I'm using this in a method that returns a string

    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Nope, bools are designed to only store a 1 or a 0. Sorry about that. You might want to try an int.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...