美文网首页
c/c++学习笔记10-string开发知识set

c/c++学习笔记10-string开发知识set

作者: scott_yu779 | 来源:发表于2018-03-04 13:58 被阅读0次

    资源和工具

    assign

    x.1 知识

    x.2 工程示例

    resultStr.assign(&buf[0], &buf[strlen(buf)]);
    

    基础知识

    工程示例

    x.1

    C++ string assign()赋值常用方法
    函数assign()常用在给string类变量赋值.
    常用方法有:
    1,直接用另一个字符串赋值.
    如str2.assign(str1);用str1给str2赋值.
    2,用另一个字符串的一个子串赋值
    如str3.assign(str1, 2, 3);
    3,用一个字符串的前一段子串赋值;
    如str4.assign("World", 5);
    4,用几个相同的字符,赋值.
    如str5.assign(10, 'c');
    #include<iostream>
    using namespace std;
    //========================================
    int main()
    {
        string str1("hello");
        string str2;
        string str3;
        string str4;
        string str5;
        //====================================
        str2.assign(str1);
        str3.assign("World", 4);
        str4.assign(str1, 2, 3);
        str5.assign(10, 'c');
        //====================================
        cout<<str1<<endl;
        cout<<str2<<endl;
        cout<<str3<<endl;
        cout<<str4<<endl;
        cout<<str5<<endl;
        //====================================
        system("pause");
        return 0;
    }
    //========================================
    结果为:
    hello
    hello
    Worl
    llo
    cccccccccc
    

    相关文章

      网友评论

          本文标题:c/c++学习笔记10-string开发知识set

          本文链接:https://www.haomeiwen.com/subject/bfgffftx.html