如何将布尔值设置为true或false而不是1或0? [重复]

问题描述 投票:-1回答:1

这个问题在这里已有答案:

我目前正在开发一个程序,它将“used”数组的索引设置为true或false,具体取决于“word”数组中是否有字符。

#include <iostream>

int main(){
  const int SIZE = 5;
  char word[SIZE] = {'H', 'e', 'l', '\0', '\0'};

  bool used[SIZE];

  cout << "word: ";
  for (int i = 0; i < SIZE; i++)
  {
     cout << " " << word[i];
  }

  cout << endl << endl;
  for (int i = 0; i < SIZE; i++)
  {
      if (word[i] != '\0')
         used[i] = true;
      else
         used[i] = false;
   }

   cout << "used: ";
   for (int i = 0; i < SIZE; i++)
   {
      cout << " " << used[i];
   }

   return 0;
}

产量

word: H e l
used: 1 1 1 0 0

我的问题是,你可以将'true'或'false'存储在一个与1和0相对的数组中吗?

c++
1个回答
0
投票

如何将布尔值设置为true或false

您可以初始化例如:

bool variable = true;

您可能对如何在输出流中插入布尔值时输出"true""false"感兴趣。 std::boolalpha I / O操纵器就是为了这个目的。

© www.soinside.com 2019 - 2024. All rights reserved.