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

时间:2019-02-12 23:25:02

标签: c++

我目前正在开发一个程序,该程序根据“ word”数组中是否存在字符来将“ used”数组的索引设置为true或false。

#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相对的数组中吗?

1 个答案:

答案 0 :(得分:0)

  

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

您可以初始化例如:

bool variable = true;

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