2012-01-31 2 views
0

szDir을 확인하고 싶습니다. szSelectedDir에 있습니다. 나는 아래의 입력 값이 다르다고 생각하는 함수를 원한다. 예제 기능이 시나리오를 처리하지 못해서 좋은 해결책을 알려주십시오.문자열 비교 C++

string szSelectedDir ="C:\\Windows1\Test"; 
string szDir="C:\\Windows"; 

void IsWindowsDirectory(const string szSpecialPath, const string szSelectedPath) 
{ 

    if(szSelectedPath.compare(szSpecialPath) == 0) 
    { 
     printf("Both paths are same "); 

    } 
    else if(szSelectedPath.find(szSpecialPath) != string::npos) 
    { 
     printf("Both paths are same "); 
    } 
    else 
    { 
     printf("Both paths are different "); 

    } 

} 
+2

'if (szSelectedPath == szSpecialPath)'운영자에게 유용합니다! – AJG85

+0

길이를 먼저 비교하십시오. – Stu

+0

당신이 사용하고있는'string' 클래스가 실제로'std :: string'이고'std :: string;을 사용하고 있다고 가정합니다. –

답변

0

에있는 szDir을 확인하십시오. 누군가가 이미 말했듯이, C++에서 문자열을 비교하는 것은 매우 간단합니다

if (szSelectedDir.find(szDir) != string::npos) 
{ 
    // You found szDir in szSelectedDir 
} 

...

if (szSelectedPath == szSpecialPath) 

나는 당신이 (다른 것들 사이에, 문자열 null이 아닌) 참조로 문자열을 통과하는 것이 좋습니다.

+0

'const'로 객체를 전달하는 것은 일반적으로 복사 비용을 피하고 참조를 통해 원본 객체를 변경하지 못하게하는 것입니다. OP가 현재하고있는 값에 의한'const'는 포인터가 널 (null)이 될 수 있다는 것보다 다소 이상합니다. – AJG85