C++ 如何将字符串与 const char* 进行比较?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2931704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare string with const char*?
提问by arzeth
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string cmd;
while(strcmp(cmd.c_str(),"exit")==0 && strcmp(cmd.c_str(),"\exit")==0)
{
cin>>cmd;
cout<<cmd;
}
return 0;
}
I am stuck.
我被困住了。
回答by CB Bailey
A std::string
instance can be compared directly with a string literal using !=
or ==
operators. This makes your comparison clearer.
std::string
可以使用!=
或==
运算符直接将实例与字符串文字进行比较。这使您的比较更加清晰。
Note that \e
isn't a valid character escape, you need to double the \
if you meant a literal \\
.
请注意,这\e
不是有效的字符转义,\
如果您的意思是文字\\
.
while( cmd == "exit" && cmd == "\exit" )
Obviously cmd
can't be equal to two different strings at the same time, presumably you meant !=
.
显然cmd
不能同时等于两个不同的字符串,大概你的意思是!=
.
Also, consider whether std::getline( std::cin, cmd )
is more appropriate than std::cin >> cmd;
. In either case you should check for success of the read operation otherwise you may end in an infinite loop if the stream is closed or enters a failed state.
另外,考虑是否std::getline( std::cin, cmd )
比 更合适std::cin >> cmd;
。在任何一种情况下,您都应该检查读取操作是否成功,否则如果流关闭或进入失败状态,您可能会以无限循环结束。
Personally I'd go with something like this, assuming that you want to echo the exit command as your code does.
就我个人而言,我会采用这样的方法,假设您想像代码一样回显 exit 命令。
#include <string>
#include <iostream>
#include <ostream>
int main()
{
std::string cmd;
while (std::getline(std::cin, cmd))
{
std::cout << cmd << std::endl;
if (cmd == "exit" || cmd == "\exit")
break;
}
return 0;
}
回答by sbi
After fixing a couple of small bugs, this works on my machine:
修复了几个小错误后,这适用于我的机器:
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
int main()
{
std::string cmd;
while( std::strcmp(cmd.c_str(),"exit")!=0
&& std::strcmp(cmd.c_str(),"\exit")!=0)
{
std::cin>>cmd;
std::cout<<cmd << '\n';
}
return 0;
}
However, I wonder why you want to use std::strcmp()
at all. As you have just found out, it's not as easy to use as the std::string
class. This
但是,我想知道您为什么要使用std::strcmp()
。正如您刚刚发现的那样,它不像类那样易于使用std::string
。这个
while(cmd!="exit" && cmd!="\exit")
works just as well, is easier to understand, and thus easier to get right.
效果一样好,更容易理解,因此更容易正确。
回答by Brian R. Bondy
strcmp
returns 0 when they are equal. So I think you want != 0
strcmp
当它们相等时返回 0。所以我认为你想要 != 0
Surely strcmp
won't return 0 for both, because it can't be equal to both.
肯定strcmp
不会为两者都返回 0,因为它不能等于两者。
Also it looks like you have a backslash at the start of your string, you should escape that with a double backslash.
此外,您的字符串开头似乎有一个反斜杠,您应该使用双反斜杠对其进行转义。
回答by rsp
The condition in your while will never evaluate to true
because you're testing to check the cmd string is equal to "exit"
and"\\exit"
. One string can never be equals to two values at the same time.
您 while 中的条件永远不会评估为,true
因为您正在测试以检查 cmd 字符串是否等于"exit"
和"\\exit"
。一个字符串永远不能同时等于两个值。
回答by pcent
Your problem are the while conditions.
你的问题是 while 条件。
You probably would want do exit the loop when the user enters exit, so you should use:
您可能希望在用户进入 exit 时退出循环,因此您应该使用:
while(strcmp(cmd.c_str(),"exit")!=0 && strcmp(cmd.c_str(),"\exit")!=0)
回答by PavanMysore
Just a few things to keep in mind, i am reiterating some of the suggestions that are worth repeating while(1) times.
只需要记住几件事,我重申一些值得重复 while(1) 次的建议。
You are using C++, it is object oriented, i.e., Its best to combine together the data and the function that works on it. In this case use the string compare options provided by string class rather than strcmp.
There is a logic error in your program, well it will compile, but i am afraid thats not what you want. if ( a == x && a == y ) this will always be false, as a cannot be both equal to x and y unless x=y, in your case clearly x!=y.
您使用的是 C++,它是面向对象的,即最好将数据和处理它的函数结合在一起。在这种情况下,使用字符串类而不是 strcmp 提供的字符串比较选项。
你的程序有逻辑错误,它会编译,但恐怕那不是你想要的。if ( a == x && a == y ) 这将永远是错误的,因为除非 x=y,否则 a 不能同时等于 x 和 y,在您的情况下显然是 x!=y。
Cheers, Pavan
干杯,帕万