C++DAY2

#include 

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 

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 

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 

using namespace std;

template 
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 << "两个字符串的最大值:" <
#include 

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 

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 <

C++DAY2_第1张图片

 

你可能感兴趣的:(c++,算法,数据结构)