16.1.2【string赋值操作】

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


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


void test1()
{
    string s1;
    s1 = "hello world";
    cout << s1 << endl;

    string s2;
    s2 = s1;
    cout << s2 << endl;

    string s3;
    s3 = 'a';
    cout << s3 << endl;

    string s4;
    s4.assign("hi,c++");
    cout << s4 << endl;

    string s5;
    s5.assign("hi,c++", 4);
    cout << s5 << endl;

    string s6;
    s6.assign(s5);
    cout << s6 << endl;

    string s7;
    s7.assign(10, 'b');
    cout << s7 << endl;
}


int main()
{
    test1();

    system("pause");
    return 0;
}

posted @   yub4by  阅读(179)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示