如何比较两个整数,看它们是否相等
How to compare 2 integers to see if they are equal?
如何比较C++中的两个整数?
我有一个用户输入ID(即int
),然后有一个联系人ID,它是我的结构的一部分。联系人ID也是int
。
我需要进行比较,看看它们是否相同,才能知道它的存在。
我做了这样的事*:
if(user_input_id.compare(p->id)==0)
{
}
但我收到一条错误消息,说表达式必须具有类类型。
*基于阅读本页http://www.cplusplus.com/reference/string/string/compare/
您找到的函数用于比较两个std::string
s。您没有std::string
s,您有int
s。要测试两个int
s是否相等,只需使用==
,如下所示:
if (user_input_id == p->id) {
// ...
}
事实上,即使您有两个std::string
,您也很可能希望在那里使用==
。
我不确定你的意思,但IMHO
int k;
std::cin>>k;
if (k==p->id)
do_sth();
else
do_sth_else();
重点是您不将输入存储为字符串,而是将其存储为int。
//simple program to compare
#include<iostream>
using namespace std;
typedef struct node {
int a;
}node;
int main() {
node p;
p.a = 5;
int a;
cin >> a;
if( p.a == a )
cout << "Equal" << endl;
else
cout << "Not Equal"<< endl;
return 0;
}
如果结构的名称是p,并且其中有一个名为hello的整数,则可以执行以下
int input;
cin << input;
if(input == p.hello){
cout << "the input is equal to p.hello" << endl;
}
else{
cout << "the input is not equal to p.hello" << endl;
}
相关文章:
- 如何检查两个 std::向量在小于 O(n) 的时间复杂度内是否相等
- 是否可以在 OpenGL 中的同一调用中呈现两个具有不同索引起点的不同缓冲区?
- 检查两个节点在子节点上是否具有相同状态的更优雅的方法
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 使用XOR查找O(n)-解决方案中的两个字符串是否为变位符
- 给定一个大小为 N 的数组 S,检查是否可以将序列拆分为两个序列
- 具有相同特征的两个对象是否只在内存中存储一次?无论定义它们的函数是什么,都是不同的
- 如何更好地检查两个 char 变量是否在一组值中?
- 是否有一种标准方法来计算两个 asctime() 值之间的天数
- 是否可以使用非常量指针调用非常量函数,以及当两个unique_ptrs指向同一个对象时程序的行为方式?
- 检查两个模板参数是否相同
- 是否可以在 for 循环中添加两个浮点数?
- 是否允许三元运算符在C++中计算两个操作数?
- 使用 SET(C++) 检查两个给定字符串是否是字谜时出现运行时错误
- 使用 std::vector::swap 方法在C++中交换两个不同的向量是否安全?
- 检查两个向量是否并行的最有效方法
- 检查两个图是否为补码的功能
- gtest:检查字符串是否等于两个字符串之一
- 如何在c/c++中检查两个任意内存范围是否重叠
- 图问题:找出两个节点是否在每个节点的O(1)时间和O(2)存储中共享同一分支