美文网首页
2.string赋值操作

2.string赋值操作

作者: lxr_ | 来源:发表于2021-04-03 17:01 被阅读0次
#include<iostream>
using namespace std;

//string 赋值操作
//string& operator=(const char* s);char* 类型字符串复制给当前的字符串
//string& operator=(const string& s);
//string& operator=(char c);
//string& assign(const char* s);
//string& assign(const char* s,,int n);把字符串的前n个字符串赋值给当前字符串
//string& assign(const string& s);
//string& assign(int n,char c);n个字符赋给当前字符串

void test0201()
{
    string str1;
    str1 = "xiansifan";
    cout << "str1=" << str1 << endl;

    string str2;
    str2 = str1;
    cout << "str2=" << str2 << endl;

    string str3;
    char c = 'a';
    str3 = c;
    cout << "str3=" << str3 << endl;

    string str4;
    str4.assign("hello");
    cout << "str4=" << str4 << endl;

    string str5;
    str5.assign("xiansifan", 4);
    cout << "str5=" << str5 << endl;

    string str6;
    str6.assign(str5);
    cout <<"str6=" << str6 << endl;

    string str7;
    str7.assign(10, c);
    cout << "str7=" << str7 << endl;
}
int main()
{
    test0201();

    system("pause");
    return 0;
}

相关文章

网友评论

      本文标题:2.string赋值操作

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