• 练习使用C++的string类


    #include<stdio.h>
    #include<iostream>
    #include<assert.h>
    #include<stdlib.h>
    #include<string>
    using namespace std;

    /*
    字符串处理函数
    strlen(str)//字符串长度
    strcmp(str1,str2)字符串比较 strncmp(str1,str2,n)//比较前n个
    strcat(str1,str2)字符串连接 strncat
    strcpy(str1,str2);strncpy(str1,str2,n);字符串拷贝

    */
    /*

    //按以下函数进行实际操作
    C++
    <string>
    string str;
    字符串长度:len=str.length(); || len=str.size();
    字符串比较:str.compare(str2); || str.compare(pos1,len1;str2,pos2,len2); >返回1 ,=返回0 , <返回-1 pos:字符串下标
    附加:str1+=str2; || str1.append(str2); || str1.append(str2,pos2,le2);
    字符串提取:str2=str1.substr(); || str2=str1.substr(pos1); || str2=str1.substr(pos1,len1);
    自符串搜索:where=str1.find(str); || where =str1.find(str2,pos1); 搜索位置|| where=str1.rfind(str2);从后搜
    插入字符串:str1.insert(pos1,str2);|| str1.insert(pos1,str2,pos2,len2); || str1.insert(pos1,numchar,str2);插入次数
    替换字符串:str1.replace(pos1,str2); ||str1.replace(pos1,str2,pos2,len2);
    删除字符串:str.erase(pos,len); || str.clear();
    交换字符串:swap(str1,str2);
    C-->C++ :char*cstr="hello";string str1;cstr=cstr:string str2(cstr);

    */
    char *My_strcpy( char*str1,const char *str2)
    {
    assert((str1 != NULL) &&(str2 != NULL));
    char* to = str1;
    while ((*str1++ = *str2++) != '');
    return to;
    }

    //结果最后附页

    int main()
    {
    string str1 ="hello word";
    string str2 = "hello";
    str1.erase(2, 3);
    cout << "str1 " << str1 << endl;
    //str1.replace(0,str2);
    //str1.replace(2, str2, 2, 2);
    str1.insert(2, str2); //hehellollo word
    cout << "str1 " << str1 << endl;

    cout << str1.find('l') << endl; //2
    cout << str1.rfind('l') << endl;//3


    const char *str3 = "c hello";
    char *str4 = "c hello word";
    string str5(str4);
    cout << "str5 :" << str5 << endl;
    cout << "str3 :" << str3 << endl;
    cout << "str4 :" << str4 << endl;
    printf("%s ", str3); //true :str3为C
    printf("%s ", str4); //true :str4为C
    //printf("%s ", str5);//error:str5为C++
    //cout<< My_strcpy(str4, str3) << endl;

    cout << "str1字符串为"<<str1 << endl;
    printf("%d ",str1.length()); //10
    printf("%d ", str1.compare(str2)); //1
    printf("%d ", str1.compare(2,1,str2,3,1)); // 0

    str1 + str2;//str1=hello wordhello;
    cout << "str1 "<<str1<< endl;
    str1.append(str2);//str1=hello wordhello hello;
    cout <<"str1 "<< str1 << endl;

    str1.append(str2, 2, 4);//str1=hello wordhello hello llo;
    cout << "str1 " << str1 << endl;

    str2 = str1.substr();//str2=hello word;
    cout << "str2 " << str2 << endl;

    str2 = str1.substr(2);//str2=llo word;
    cout <<"str2 "<< str2 << endl;
    str2 = str1.substr(2, 4);//str2=llo
    cout << "str2 "<<str2 << endl;


    swap(str1, str2);//2
    cout << "str1 :" << str1 << " str2 :" << str2 << endl;
    return 0;
    }
    /*
    str1 he word
    str1 hehello word
    4
    5
    str5 :c hello word
    str3 :c hello
    str4 :c hello word
    c hello
    c hello word
    str1字符串为hehello word
    12
    -1
    -1
    str1 hehello word
    str1 hehello wordhello
    str1 hehello wordhellollo
    str2 hehello wordhellollo
    str2 hello wordhellollo
    str2 hell
    str1 :hell str2 :hehello wordhellollo
    */

  • 相关阅读:
    C#开发规范
    Win32API使用技巧 -- 置顶应用
    Windows开发常用快捷键
    MarkDown简单语法回顾
    样本分布
    复变函数
    set theory
    Oscar的数理统计笔记本
    Oscar的拓扑笔记本
    c语言总结
  • 原文地址:https://www.cnblogs.com/xcb-1024day/p/11154896.html
Copyright © 2020-2023  润新知