我们常会比较两个字符串的大小,C++中,strin提供了strcomp函数用于比较ANSI编码下的字符串大小,Windows下会存在Unicode编码,所以提供CompareString、CompareStringOrdinal这两个函数。
#include <iostream>
#include "Windows.h"
using namespace std;
int main()
{
::string str1 = "123";
::string str2 = "345";
int l=strlen(str1.c_str());
::wstring wstr1 = L"123";
::wstring wstr2 = L"345";
int l2 = wcslen(wstr1.c_str());
//获取当前线程的LCID
LCID m_Id=GetThreadLocale();
//以符合用户语言习惯的方式向用户显示的字符串,进行比较
DWORD wRet=CompareStringA(m_Id,SORT_STRINGSORT,str1.c_str(),strlen(str1.c_str()), str2.c_str(),strlen(str2.c_str()));
//比较程序内部字符串(如路径名、注册表项/值、XML元素/属性等)
DWORD wRet1 = CompareStringOrdinal(wstr1.c_str(), lstrlen(wstr1.c_str()), wstr2.c_str(), lstrlen(wstr2.c_str()),true);
system("pause");
}