c++ 字符串处理代码

79 阅读2分钟


#define _CRTDBG_MAP_ALLOC
#include <iostream>
#include <cstring>
#include <string>
#include <windows.h>
#include <VersionHelpers.h>


using namespace std;

class Person
{

    mutable int m_A;

};


void test01()
{
    cout << "********strcpy_s  拷贝全部***********" << endl;
    char a[] = "abcdefg";
    char b[sizeof(a)];
    strcpy_s(b, sizeof(a), a);
    cout << b << endl;//将 a copy 全部copy 给b,  abcdefg

    cout << endl << "*********strncpy_s 拷贝部分**********" << endl;
    strncpy_s(b, a,3);
    cout << b << endl; //abc  拷贝部分
    cout << sizeof(b) << endl;//8

    cout << endl << "*********strcmp 比较字符串**********" << endl;
    char str1[] = "abc";
    char str2[] = "abc";
    auto rst = strcmp(str1, str2);
    cout << rst << endl;//0  0:str1 和 str2 相等。负值:str1 小于 str2(在字典顺序上)。正值:str1 大于 str2(在字典顺序上)。
    cout << typeid(rst).name() << endl;//int

    cout << endl << "*********strcat_s  连接字符串**********" << endl;
    char str3[22] = "hello";
    char str4[] = " world";
    auto rst1 = strcat_s(str3,22, str4);
    cout << rst1 << endl;//0
    cout << str3 << endl;//hello world
    cout << str4 << endl;// world

    cout << endl << "******strchr 查找字符串中第一次出现的字符*************" << endl;
    char str5[] = "good luck";
    auto rst2 = strchr(str5, 'l');
    cout << rst2 << endl;//luck

    cout << endl << "******strstr 查找字符串中第一次出现的子字符串*************" << endl;
    char str6[] = "good luck";
    auto rst3 = strstr(str6, "od");
    cout << rst3 << endl;//luck
}

void test02() {

    cout << endl << "******size() length() 求字符串长度*************" << endl;
    string str1 = "abcde";
    cout << sizeof(str1) << endl;//28
    cout << str1.size() << endl;//5
    cout << str1.length() << endl;//5

    cout << endl << "******append  +追加*************" << endl;
    string str2 = "hello";
    str2.append(" world");
    cout << str2 << endl;//hello world
    str2 += " ni hao";
    cout << str2 << endl;//hello world ni hao


    cout << endl << "******substr 提取字符串*************" << endl;
    string str3 = "hello";
    string rst1 = str3.substr(0,2);
    cout << str3 << endl;//hello
    cout << rst1 << endl;//he


    cout << endl << "******find 查找字符串或者字符串第一次出现的位置*************" << endl;
    string str4 = "hellolloe";
    size_t rst2 = str4.find('e');
    size_t rst3 = str4.find("ll");
    cout << typeid(rst2).name() << endl;//unsigned int
    cout << rst2 << endl;//1
    cout << rst3 << endl;//2

    cout << endl << "******compare 比较字符串*************" << endl;
    string str5 = "hellolloe";
    string str6 = "zellolloe";
    auto rst4 = str5.compare(str6);
    cout << rst4 << endl;//0 相等 , -1 str5>str6 1 str5<str6
     
    cout << endl << "******c_str() 比较字符串*************" << endl;
    string str7 = "hellolloe";
    const char  * ch1 = str7.c_str();
    cout << ch1 << endl;//hellolloe
    cout << typeid(ch1).name() << endl;//char const *

}


typedef LONG(WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);

DWORD IsWindows11OrGreater() {
    HMODULE hMod = ::GetModuleHandleW(L"ntdll.dll");
    if (hMod) {
        RtlGetVersionPtr RtlGetVersion = (RtlGetVersionPtr)::GetProcAddress(hMod, "RtlGetVersion");
        if (RtlGetVersion != nullptr) {
            RTL_OSVERSIONINFOW rovi = { 0 };
            rovi.dwOSVersionInfoSize = sizeof(rovi);
            if (RtlGetVersion(&rovi) == 0) { // 0 means success
                // Windows 11 has a major version of 10 and a build number of 22000 or greater
                //return (rovi.dwMajorVersion == 10 && rovi.dwBuildNumber >= 22000);
                return  rovi.dwBuildNumber;

            }
        }
    }
    return 0;
}



void test03()
{
    std::cout << IsWindows11OrGreater() << std::endl;

}


int main() {
    
    
    test03();

    
    system("pause");
    // 程序退出时自动检测未释放的内存
    return 0;
}