我不明白如何比较c ++中的字符串??
string s1="abc";
string s[]={"abc","vsj"};
int length=sizeof(s)/sizeof(s[0]);//length of s
for(int i=0;i<length;i++)
{
if(s[i].compare(s1))
{
cout<<"One of the string equal to s1";
}
}
有可能吗? 感谢..
答案 0 :(得分:2)
@used_date_first = Set.new
重载std::string
。您可以使用operator==
比较2个字符串。您也可以使用operato==
而不是数组。使用c ++ 11:
std::vector
使用c ++ 98:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string s1="abc";
vector<string> ss = { "abc", "vsj" };
for (auto &s: ss) {
if (s == s1) {
cout<<"One of the string equal to s1";
}
}
return 0;
}
答案 1 :(得分:0)
比较返回与strcmp相同的值:
&lt; 0 - 不匹配的第一个字符在ptr1中的值低于在ptr2中的值
0 - 两个字符串的内容相等
&gt; 0 - 不匹配的第一个字符在ptr1中的值大于在ptr2中的值