C++,STL 015(2024.09.25)

内容:string赋值操作。

#include <iostream>
#include <string>

using namespace std;

/*
函数原型:
1.string& operator=(const char* s);      --> 将char*类型的字符串赋值给当前字符串
2.string& operator=(const string &s);    --> 将字符串s赋值给当前字符串
3.string& operator=(char c);             --> 将字符c赋值给当前字符串
4.string& assign(const char* s);         --> 将char*类型的字符串赋值给当前字符串
5.string& assign(const string &s);       --> 将字符串s赋值给当前字符串
6.string& assign(int n, char c);         --> 将n个字符c赋值给当前字符串
7.string& assign(const char* s, int n);  --> 把字符串的前n个字符赋值给当前字符串
*/

void test01()
{
    string str1;
    str1 = "hello world";
    cout << str1 << endl;

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

    string str3;
    str3 = 'c';
    cout << str3 << endl;

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

    string str5;
    str5.assign(str4);
    cout << str5 << endl;

    string str6;
    str6.assign(10, 'a');
    cout << str6 << endl;

    string str7;
    // 把字符串的前n个字符赋值给当前字符串(注意空格也算一个字符串)
    str7.assign("hello world", 7);
    cout << str7 << endl;
};

int main()
{
    test01();

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值