C++DAY2

#include <iostream>

using namespace std;

int main()
{
    string s1 = "hello world";
    string s2 = s1;             //使用s1给s2初始化

    cout << "s2 = " << s2 << endl;

    string s3(s1);              //使用s1给s3初始化
    cout << "s3 = " << s3 << endl;

    string s4;
    s4 = s1;                    //两个字符串可以直接相互赋值,无需使用strcpy
    cout << "s4 = " << s4 << endl;

    string s5 = string("ni hao");   //使用匿名对象给一个字符串赋值
    cout << "s5 = " << s5 << endl;

    string s6 = s1+s5;              //将s5连接到s1后,将结果赋值给s6,相当于strcat的功能
    cout << "s6 = " << s6 << endl;

    string s7 = s1 + "ami";         //将字符串变量与字符串连接
    cout << "s7 = " << s7 << endl;

    string s8 = "wo ai" + s5;       //将字符串与字符串变量连接
    cout << "s8 = " << s8 << endl;

    string s9 = string("i love") + "china";
    cout << "s9 = " << s9 << endl;



    return 0;
}

 

#include <iostream>

using namespace std;

int main()
{
    string s1;      //此时定义了一个字符串类型  (无参构造)

    cout << "s1 = " << s1 << endl;
    cout << "sizeof s1 = " << sizeof(s1) << endl;       //32字节

    //给一个字符串赋值
    s1 = "hello world11111111111111111111111111111111111111111111111111";         //等号运算符重载函数
    cout << "s1 = " << s1 << endl;
    cout << "sizeof s1 = " << sizeof(s1) << endl;   //32字节

    //定义一个字符串变量,并初始化
    string s2 = "nihao";
    cout << "s2 = " << s2 << endl;

    //定义一个变量并初始化
    string s3("i loce china");
    cout << "s3 = " << s3 << endl;

    string s4{"shang hai"};
    cout << "s4 = " << s4 << endl;

    //定义一个变量,使用多个连续字符初始化
    string s5(5,'k');
    cout << "s5 = " << s5 << endl;

    //字符串类型的变量互相赋值
    s1 = s2;
    cout << "s1 = " << s1 << endl;
    return 0;
}

#include <iostream>

using namespace std;

int *new_score()
{
    int *p = new int[6];
    for(int i = 0; i < 6; i++)
    {
        cout << "请输入学生成绩:" ;
        cin >> p[i];
    }
    return p;
}

void my_sort(int *p)
{
    int i,j;
    for(i = 1;i < 6; i++)
    {
        for(j = 0; j < 6-i; j++)
        {
            if(p[j] > p[j+1])
            {
                int t = p[j];
                p[j] = p[j+1];
                p[j+1] = t;
            }
        }
    }
    for(int i = 0; i < 6; i++)
    {
        cout << p[i] << endl ;
    }
}

int main()
{
    //申请6个连续空间
    int *p = new_score();

    //排序输出

    my_sort(p);

    //释放连续内存空间
    delete []p;
    p = nullptr;

    return 0;
}
#include <iostream>

using namespace std;

template <typename T>
T my_max(T m,T n)
{
    if(m > n)
        return m;
    else
        return n;
}
/*
double my_max(double m,double n)
{
    if(m > n)
        return m;
    else if(m < n)
        return n;
    else
        return -1;
}

string my_max(string m,string n)
{
    if(m > n)
        return m;
    else if(m < n)
        return n;
    else
        return NULL;
}

int my_max(int m,int n,int i)
{
    if(m > n && m >i)
        return m;
    else
    {
        if(n > i)
            return n;
        else if(n < i)
            return i;
        else
            return -1;
    }
}
*/
int main()
{
    cout << "两个整数的最大值:" << my_max(45,67) << endl;
    cout << "两个小数的最大值:" << my_max(45.4,45.8) << endl;
    cout << "两个字符串的最大值:" <<my_max("hello","world") << endl;
    cout << "三个整数的最大值:" << my_max(my_max(45,67),78) << endl;
    return 0;
}
#include <iostream>

using namespace std;

int main()
{
    string s1 = "hello world";
    cout <<"s1的长度为:" << s1.size() << endl;
    cout <<"s1的长度为:" << s1.length() << endl;

    cout <<"s1当前的最大容量: " << s1.capacity() << endl;
    s1 += "kkkkk";
    cout <<"s1当前的最大容量: " << s1.capacity() << endl;
    cout <<"s1的长度为:" << s1.length() << endl;

    s1 += "hh";
    cout <<"s1当前的最大容量: " << s1.capacity() << endl;
    cout <<"s1的长度为:" << s1.length() << endl;

    //将字符串清空
    s1.clear();
    cout <<"s1当前的最大容量: " << s1.capacity() << endl;
    cout <<"s1的长度为:" << s1.length() << endl;

    //判断字符串是否为空
    if(s1.empty())
    {
        cout << "字符串为空" << endl;
    }
    else
    {
        cout << "字符串非空" << endl;
    }

    string *p = &s1;
    p->push_back('h');
    p->push_back('e');
    p->push_back('l');
    p->push_back('l');
    p->push_back('o');
    cout << "*p = " << *p << endl;
    return 0;
}
#include <iostream>

using namespace std;

int main()
{
    //在堆区申请一个int类型大小的空间
    int *p1 = new int;
    cout << "*p1 = " << *p1 << endl;        //没有初始 化,空间中的值是随机值

    *p1 = 520;
    cout << "*p1 = " << *p1 << endl;

    //在堆区申请一个double类型的空间并初始化
    double *p2 = new double(3.14);
    cout << "*p2 = " << *p2 << endl;

    //在堆区申请一个字符类型的空间并初始化
    char *p3 = new char{'H'};
    cout << "*p3 = " << *p3 << endl;

    delete p1;
    delete p2;
    delete p3;

    cout << "*********************************" << endl;

    //在堆区连续申请5个int类型的空间
    int *p4 = new int[5];
    for(int i = 0; i < 5; i++)
    {
        cout << p4[i] << " ";

    }
    cout <<endl;
    for(int i = 0; i < 5; i++)
    {
        p4[i] = i+10;

    }
    for(int i = 0; i < 5; i++)
    {
        cout << p4[i] << " ";

    }
    cout <<endl;

    //在堆区连续申请5个int类型空间并初始化
    int *p5 = new int[5]{333,444,555,666,777};
    for(int i = 0; i < 5; i++)
    {
        cout << p5[i] << " ";

    }
    cout <<endl;

    //释放连续内存空间
    delete [] p4;
    delete [] p5;

    p1 = nullptr;
    p2 = nullptr;
    p3 = nullptr;
    p4 = nullptr;
    p5 = nullptr;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值