16.1.1【string的基本概念和构造函数】

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


/*
    3.1 string容器

    3.1.1 string基本概念
        string和char * 区别:
            char * 是一个指针
            string是c++风格的字符串,实质是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器。
        特点:
            string 类内部封装了很多成员方法
            例如:查找find,拷贝copy,删除delete 替换replace,插入insert
            string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责

    3.1.2 string构造函数
        string(); //创建一个空的字符串 例如: string str;
        string(const char* s); //使用字符串s初始化
        string(const string& str); //使用一个string对象初始化另一个string对象
        string(int n, char c); //使用n个字符c初始化
*/


void test1()
{
    string s1; //默认构造

    const char * str = "hello world"; //c风格字符串
    string s2(str);
    cout << s2 << endl;

    string s3(s2); //拷贝构造
    cout << s3 << endl;

    string s4(10, 'c');
    cout << s4 << endl;
}


int main()
{
    test1();

    system("pause");
    return 0;
}

posted @   yub4by  阅读(132)  评论(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 让容器管理更轻松!
点击右上角即可分享
微信分享提示