通过清心醉

C++字符串和字符串数组

//#define _CRT_SECURE_NO_WARNINGS 
//定义_CRT_SECURE_NO_WARNINGS 主要考虑strcpy_s /和 strcpy等部分string字符串操作异常,如果考虑向下兼容则使用,否则不建议使用。
#include <iostream>
#include <exception> //异常
#include <string> //字符串需要
using namespace std;




//------【变量类型】------>
int tempdemo_value = true;
int cc = 100;
void tempdemo() {
    static int ax = 100; //静态变量
    auto bx = 100;
    register int cx = 1000; //寄存器变量,直接在CPU内进行运算
    extern int tempdemo_value; //在函数体内引用,就可以直接引用,不过如果是外部的全局变量,直接应用也是可以的。比如一下的变量cc,同样可以对cc变量进行重新赋值
    sizeof(bx); //查看变量占用的字节
    std::cout << "cc=" << cc << "\n";
}

//------【变量类型】------>

//一维数组
void CoutArray() {
    __int64 a[10];
    //int a[10]; //定义一个数组,10个元素 
    for (int i = 0; i <= 9; i++) {
        cout << i << "\n";
    }
    //前面的for循环有长度,在实际开发过程中,如果不确定的情况下
    //a[11] = true;
    int length = sizeof(a) / sizeof(a[0]); //确定一定要有0开始的下标
    for (int i = 0; i < length; i++) { //这里注意,比如有10个,这里的length的实际获取是10,所以不能用i<=length,不然0自增的话,就是i<=10,就会列出0-10共计11个
        a[i] = (100 + i);
        cout << a[i] << "\n";
    }
    cout << "\n";
    //数组初始化
    int int32[10] = { 0,1,2,3,4,5,6,7,8,9 };
    for (int i = 0; i < 10; i++) {
        cout << "int32next=" << int32[i] << "\n";
    }
    cout << "\n";
    //定义未知长度数组
    int int32_[] = { 9,8,7,6,5,4,3,2,1,0 };
    for (int i = 0; i < 10; i++) {
        cout << "int32_next=" << int32_[i] << "\n";
    }
    cout << "\n";
}


//多维数组
void AllArray() {
    __int64 arr[1][2][3]; //定义3维数组
    int length_1 = sizeof(arr) / sizeof(arr[0]); //通过字节占用获取1维数组的数量
    int length_2 = sizeof(arr[0]) / sizeof(arr[0][0]); //通过字节占用获取2维数组的数量
    int length_3 = sizeof(arr[0][0]) / sizeof(arr[0][0][0]); //通过字节占用获取3维数组的数量
    //cout << length_1 << length_2 << length_3;

    for (int i = 0; i < length_1; i++) {
        for (int o = 0; o < length_2; o++) {
            for (int p = 0; p < length_3; p++) {
                arr[i][o][p] = (100 + p); //对3维下标进行赋值(100+当前的下标),其他更复杂的就暂时不写了,同理
            }
        }
    }
    for (int i = 0; i < length_1; i++) {
        for (int o = 0; o < length_2; o++) {
            for (int p = 0; p < length_3; p++) {
                cout << "3维数组下标" << p << "值=" << arr[i][o][p] << "\n";
            }
        }
    }

}



void CharType() {

    const char* str = "Visual Studio"; //字符变量,当然也可以使用String库或者char str[]
    cout << str << "\n"; //输出字符变量
    cout << "----------------------------------------------------------------------------\n";
    //字符串拼接,要设置下长度,比如str[32],str[64],但是不可设置str[],虽然一样可以执行,但是会抛出异常
    char str1[32] = "你好";
    char str2[32] = "中国";
    int str1_str2_strlen = int(strlen(str1)) + int(strlen(str2)) + 1;
    //具体参考strcat_s的方法原型 -- 注意:strcat_s第二个参数的计算,该参数是拼接后的字符串大小,并非原字符串大小或者目标字符串大小
    //VS11+建议使用strcat_s替换strcat,但是如果应用如果要考虑兼容XP等老系统,需要重新定义_CRT_SECURE_NO_WARNINGS
    strcat_s(str1, str1_str2_strlen, str2); //拼接之后,str1  = str1.str2 , str2则不变
    cout << "字符串拼接:" << str1 << "\n";
    cout << "----------------------------------------------------------------------------\n";

    //字符串复制1
    char str3[32], str4[32] = "清心醉";
    //VS11+建议使用strcpy_s替换strcpy,但是如果应用如果要考虑兼容XP等老系统,需要重新定义_CRT_SECURE_NO_WARNINGS
    strcpy_s(str3, str4);
    cout << "字符串复制1:" << str3 << "\n";
    //字符串复制2
    char str5[32];
    char str6[32] = "Visual Studio";
    strcpy_s(str5, strlen(str6) + 1, str6); //第二个参数为str6+1 使用strlen返回的长度不会包含末尾空字符
    cout << "字符串复制2" << str5 << "\n";

    //复制个别字符串
    char str7[32];
    char str8[32] = "Visual Studio Hello";
    strncpy_s(str7, str8, 13); //复制str8的前面13个字符串
    cout << "字符串提取:" << str7 << "\n";
    cout << "----------------------------------------------------------------------------\n";
    //字符串对比
    char str9[16] = "This";
    char str10[16] = "This";
    int str9_str10_return_status = strcmp(str9, str10); //  == 0 标识匹配
    cout << "字符串对比返回状态:" << str9_str10_return_status << "\n";


    //字符串常量类型
    const char* c[] = { "AA","BB","CC","DD","EE" }; //初始化字符数组,不建议定义为const char* c[5]
    int char_length_ = sizeof(c) / sizeof(c[0]); //获取字符数组长度
    for (int i = 0; i < char_length_; i++) {
        cout << c[i] << "\n";
    }


}

template <typename STR> //函数模板声明 //注意这里定义的I为未知的类型,也就是可以是int,float,doublue等类型,I为自定义名称
//定义函数模板,带2个参数
int TempCheckStr(STR str1, STR str2) { //定义模板方法,来检测字符串是否匹配
    int status = 0;
    int str1_ascii = -1;
    int str2_ascii = -2;
    if (sizeof(str1) == sizeof(str2)) {
        for (int k = 0; k < sizeof(str1); k++) {
            //cout << "STR1:" << str1[k] << "-ASCII=" << int(str1[k]) << "--";
            //cout << "STR2:" << str2[k] << "-ASCII=" << int(str2[k]) << "\n";
            str1_ascii = int(str1[k]);
            str2_ascii = int(str2[k]);
            if (str1_ascii != str2_ascii) {
                return 1;
            }
        }
    }
    return 0;
}
void StringType() {
    //字符串变量
    string str = "这是一个字符串变量";
    cout << str << "\n";
    cout << "----------------------------------------------------------------------------\n";
    //字符串拼接,和char类型不同,直接使用+ 即可。
    string str1 = "(字符串的变量拼接)";
    cout << str + str1 << "\n";
    cout << "str 的地址:" << &str << "\n";
    cout << "str1的地址:" << &str1 << "\n";
    cout << "----------------------------------------------------------------------------\n";
    //字符串复制,则直接赋值即可,即str = str1; 因为string是类库使用。
    //但是可以复制部分的长度
    string str3 = "Visual Studio 2022";
    char str4[16];
    str3.copy(str4, 13, 0); //复制str3到str4,复制长度13,开始位置0开始。
    str4[13] = '\0'; //Visual Studio实际占位13,但是因为0开始,所以其实=12,定义[13]给与\0是进行结束,否则可能出现其他异常乱码
    //如果str4[13]改成str4[12],则复制的内容为Visual Studi,因为第12个字符为o
    cout << str4 << "\n";
    cout << "----------------------------------------------------------------------------\n";
    string str5 = "Visual Studio";
    string str6 = "Visual Studio";
    if (str5 == str6) {
        cout << str5 << " == " << str6 << "\n";
    }
    else {
        cout << str5 << " != " << str6 << "\n";
    }

    //string to char
    //不过不严谨,最好使用VS的String类方法检查
    int str5_str6_status = str5.compare(str6);
    switch (str5_str6_status) {
    case 1:cout << "字符串【" << str5 << "】和字符串【" << str6 << "】对比的返回结果为:" << str5_str6_status << "\n"; break;
    case 0:cout << "字符串【" << str5 << "】和字符串【" << str6 << "】对比的返回结果为:" << str5_str6_status << "\n"; break;
    case -1:cout << "字符串【" << str5 << "】和字符串【" << str6 << "】对比的返回结果为:" << str5_str6_status << "\n"; break;
    default:cout << "字符串【" << str5 << "】和字符串【" << str6 << "】对比的返回结果为:" << str5_str6_status << "\n"; break;
    }
    //减产是否对等,最快的是查看ascii码,因为要使用char的方式,所以
    char str5ascii[14];
    char str6ascii[14];
    int i = 0;
    for (i = 0; i < str5.length(); i++) {
        str5ascii[i] = str5[i];
    }
    str5ascii[i] = '\0'; //一定要有结束符
    i = 0;
    for (i = 0; i < str6.length(); i++) {
        str6ascii[i] = str6[i];
    }
    str6ascii[i] = '\0'; //一定要有结束符
    cout << "string转char:\n";
    cout << "str5 : " << str5ascii << "--占用字节:" << sizeof(str5ascii) << "\n";
    cout << "str6 : " << str6ascii << "--占用字节:" << sizeof(str6ascii) << "\n";
    //const char* str_5_as[16] = str5ascii;
    //const char* str_6_as[16] = str6ascii;;
    //循环输出字符串的对应ASCII码
    cout << "输出str5和str6的对应ASCII:\n";
    int status = TempCheckStr(str5ascii, str6ascii); //利用模板方法,来检验对应的所有元素的ASICC码是否匹配,=0标识匹配
    cout << "status = " << status << "\n";
    //这里也可以用string的类型进行同样的方式校验

    cout << "----------------------------------------------------------------------------\n";
    //如果字符串不是单个,不要使用int来获取ASCII吗
    //int str5_ascii = int(str5ascii);
    //int str6_ascii = int(str6ascii);
    //cout << "str5Ascii:[" << str5_ascii << "] str6Ascii:[" << str6_ascii << "]\n"; //查看ascii码
    //
    //char转string
    char str7[] = "你好";
    string str8 = str7;
    cout << "char转string:" << str7 << "-" << str8 << "\n";
    cout << "----------------------------------------------------------------------------\n";
    //字符串数组
    string str_arr[] = { "AA","BB","CC","DD","EE" };
    cout << "变量数组str_arr占用字节:" << sizeof(str_arr) << "\n";
    cout << "每个元素占用字节" << sizeof(str_arr[0]) << "\n";

    //string类型,校验字符的ASCII是否匹配

}


int main()
{
    try {
        cout << "String类型操作:" << "\n";
        StringType(); //String封装的使用
        cout << "char类型操作:" << "\n";
        CharType();//char类型
        cout << "多维数组操作:" << "\n";
        AllArray(); //多维数组的数组
        cout << "1维数组操作:" << "\n";
        CoutArray(); //1维数组的输出
        return true;  //
    }
    catch (exception e) {
        cout << "Exception \n";
    }

}

这里只是使用char[]和String的方式,还有vector的方式后续更新了。

不得不说,还是写C++有味道

关于作者

清心醉 administrator

发表评论

请输入验证码: