-4

出于某种原因,下面的这个 C++ 一直返回 false。任何帮助都会被接受!我会包含更多来源,但它在 LibPQ 中

std::string one = "stringa";
std::string two = "stringb";

std::string three = "stringa";
std::string four = "stringb";

if( one.compare( three ) == 0 && two.compare( four ) == 0 )  {

  return true;
} else {

  return false;
}
4

3 回答 3

0

问题是你的 if 后面没有 { 。尝试这个

std::string one = "stringa";
std::string two = "stringb";

std::string three = "stringa";
std::string four = "stringb";

if( one.compare( three ) == 0 && two.compare( four ) == 0 )
{
  return true;
} else {

  return false;
}

顺便说一句,你的代码甚至符合吗?

于 2013-01-04T08:25:57.327 回答
0

您可以尝试以下方法:

#include "iostream" 

using namespace std; 

std::string one = "amit"; 
std::string two = "kumar"; 
std::string three = "amit"; 
std::string four = "kumar"; 

bool strcomp() 
{ 
    if (one.compare(three) == 0 && two.compare(four) == 0) 
    { 
        return true; 
    } 

    return false; 
} 

int main() 
{ 
    if (strcomp()) 
    { 
       cout<<"Equal"<<endl; 
    } 
    else 
    { 
       cout<<"Defferent"<<endl; 
    } 

} 

输出:相等

于 2013-01-04T08:35:16.300 回答
0

你有没有尝试过:

std::string one = "stringa";
std::string two = "stringb";

std::string three = "stringa";
std::string four = "stringb";

if( one == three && two == four ) {

  return true;
} else {

  return false;
}
于 2013-01-04T08:23:07.387 回答