首页  >  文章  >  后端开发  >  C++程序将字符串类型变量转换为布尔类型

C++程序将字符串类型变量转换为布尔类型

WBOY
WBOY转载
2023-09-15 17:49:021116浏览

C++程序将字符串类型变量转换为布尔类型

在 C++ 中,布尔变量由二进制数据 true 或 false 组成,字符串变量是字母、数字和特殊字符的序列。编译器本身无法将字符串转换为布尔值,但有多种方法可以执行此转换。我们探索将字符串值转换为布尔值的各种方法。

如果我们考虑一下算法,那就很简单了。我们采用字符串值并使用各种方式将其转换为布尔值。

算法(广义)

  • 在字符串变量中获取输入。
  • 将字符串值(true 或 false)转换为布尔值。
  • 输出值。

使用 boolalpha 和 istringstream

Boolalpha 是一个流 I/O 操纵器,可用于操纵布尔值和字母数字值。 Istringstream 是一个字符串流,用于在字符流上实现不同的功能。由于 boolalpha 与流一起使用,因此它可以与 istringstream 一起使用,将字符串值转换为布尔值。

语法

string ip = <string literal>;
bool op;
istringstream(ip) >> std::boolalpha >> op;

算法

  • 在字符串变量中获取输入。
  • 将值放入 istringstream 对象中,并使用 boolalpha 将值分配给布尔变量。
  • 输出值。

示例

#include <iostream>
#include<sstream>

using namespace std;
bool solve(string ip) {
   bool op;
   istringstream(ip) >> std::boolalpha >> op;
   return op;
}

int main()
{
   string ip = "true";
   bool op = solve(ip);
   cout << "The value of ip is: " << ip <<endl;
   cout << "The value of op is: " << op <<endl;
   return 0;
}

输出

The value of ip is: true
The value of op is: 1

在此示例中,我们采用字符串值作为输入。然后,我们使用 istringstream 对象来包含字符串值,然后使用 boolalpha 修饰符将其转换为布尔变量。我们打印输入和输出值进行比较。

使用字符串比较

In the next example, we have done a basic string comparison to convert a string value into a Boolean value. If the string value is equal to ‘false’, then 0 is returned; otherwise, 1 is returned. One thing is to be noted that this returns true for all strings other than ‘false’. But this method is the easiest to implement.

语法

string ip = <string literal>;
bool op = ip != “false”;

算法

  • 在字符串变量 ip 中获取输入。
  • 采用布尔变量运算。
  • 如果 ip 与“false”相同,则
    • op = false
  • 否则,
    • op = true
  • 显示op的值。

示例

#include <iostream>
using namespace std;

bool solve(string s) {
   return s != "false";
}
using namespace std;
int main() {
   string ip = "true";
   bool op = solve(ip);
   cout<< "The input value is: " << ip << endl;
   cout<< "The output value is: " << op << endl;
   return 0;
}

输出

The input value is: true
The output value is: 1

使用 std::stoi

在前面的示例中,我们仅将“true”转换为布尔值“1”,将“false”转换为布尔值“0”。现在,在某些情况下,字符串值可能是 0 或 1。对于这种情况,我们可以使用 stoi 函数将字符串值转换为整数,然后转换为布尔值。 stoi 函数将字符串值转换为整数,并使用显式类型转换,我们可以将该值转换为布尔值。

语法

string ip = <string literal>;
bool op = (bool)stoi(ip);

算法

  • 在字符串变量 ip 中获取输入。
  • 采用布尔变量运算。
  • 将值显式转换为 bool 为 stoi(ip) 的结果。
  • 显示op的值。

示例

#include <iostream>
#include <string>

using namespace std;
bool solve(string s) {
   //using std:: stoi function
   return (bool)stoi(s);
}

using namespace std;
int main() {
   string ip = "1";
   bool op = solve(ip);
   cout<< "The input value is: " << ip << endl;
   cout<< "The output value is: " << op << endl;
   return 0;
}

输出

The input value is: 1
The output value is: 1

结论

我们将字符串作为输入,其中可能包含任何值“true”、“1”、“false”或“0”。前两个方法将“true”或“false”分别转换为 1 和 0。如果我们用“1”或“0”替换“true”或“false”,它也会以同样的方式工作。但在第三个示例中,如果我们将 '1' 或 '0' 更改为 'true' 或 'false',它将不起作用,因为 stoi 函数无法将不包含字母数字字符的字符串转换为整数值,因此不能转换为布尔值。因此,根据使用情况,我们必须确定要使用的最佳方法。

在特定项目中使用某些第三方库或 API 时,需要进行字符串到布尔值的转换。有些API或库以字符串格式输出,为了使结果兼容,我们必须将字符串值转换为布尔值。

以上是C++程序将字符串类型变量转换为布尔类型的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除