C++Stu0320

#include <iostream>
using namespace std;
#include <string>
//模板 
//函数模板
void swapint(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;
}
template<typename T>//声明创建模板
void myswap(T& a, T& b)
{
    T temp = a;
    a = b;
    b = temp;
}
int main()
{
    int a = 10;
    int b = 20;
    cout << a << b << endl;
    //swapint(a, b);
    myswap(a, b);//自己适应类型
    myswap<int>(a, b);//自己声明类型
    cout << a << b << endl;
}



//子类继承父类模板要说明数据类型

//成员函数类外实现
template<typename T1,typename T2>
class person
{
public:
    //person(T1 name, T2 age)//类内实现
    //{
    //    this->name = name;
    //    this->age = age;
    //}
    person(T1 name, T2 age);//类外实现

   /* void show()
    {
        cout << this->age << this->name << endl;
    }*/
    void show();
    T1 name;
    T2 age;
};
template<typename T1, typename T2>
person<T1 ,T2>::person(T1 name, T2 age)//函数类外实现
{
    this->name = name;
    this->age = age;

}
template<typename T1, typename T2>
void person<T1, T2>::show()
{
    cout << this->age << this->name << endl;
}
int main()
{
    person<string, int>p("abc", 12);
    p.show();
}





类模板分开编写
1  .h   .cpp  两个文件
2  调用时包含源文件  #include ”  .cpp“

1  将原先的.h  .cpp 内容写在一起,后缀名是 .hpp  只有单独一个文件
2  调用时包含  .hpp     (常用)



全局函数类外实现
先声明类模板和函数模板 , 开头加上说明
template<typename T1, typename T2>
class person;
template<typename T1, typename T2>
void print1(person<T1, T2>p)//全局函数 类外实现
{
    cout << p.age << endl;
}
全局函数在类内做友元 , 加入空模板参数列表
friend void print1<>(person<T1, T2>p);


#include <iostream>
using namespace std;
#include <string>
template<typename T1, typename T2>
class person;
template<typename T1, typename T2>
void print1(person<T1, T2>p)//全局函数 类外实现
{
    cout << p.age << endl;
}
//类模板分文件编写
//类模板  友元
template<typename T1, typename T2>
class person
{
    //全局函数 类内实现
    friend void print(person<T1,T2>p)
    {
        cout << p.age << p.name << endl;
    }
    //全局函数 类外实现
    //加入空模板参数列表
    friend void print1<>(person<T1, T2>p);
public:
    person(T1 name, T2 age)//类内实现
    {
        this->name = name;
        this->age = age;
    }
    

    void show()
    {
        cout << this->age << this->name << endl;
    }
private:
    T1 name;
    T2 age;
};

int main()
{
    person<string, int>p("abc", 12);
    p.show();
    print(p);
    print1(p);
}







#include <iostream>
using namespace std;
#include <string>
//STL
void print(int val)
{
    cout << val << endl;
}
void test()
{
    //创建vector容器
    vector<int>v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    //遍历方式1
    /* vector<int>::iterator itbegin = v.begin();//起始迭代器  指向容器第一个元素
    vector<int>::iterator itend = v.end();//结束迭代器  指向容器最后一个元素的下一个位置
    while (itend != itbegin)
    {
        cout << *itbegin++ << endl;
    } */
    //遍历方式2
    /* for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << endl;
    } */
    //遍历方式3
    for_each(v.begin(), v.end(), print);//算法方式
}

int main()
{
    test();
}




#include <iostream>
using namespace std;
#include <string>
class person
{
public:
    person(string name, int age)
    {
        this->m_name = name;
        this->m_age = age;
    }
    string m_name;
    int m_age;
};
void test()
{
    vector<person>v;
    person p1("a", 1);
    person p2("b", 2);
    person p3("c", 3);
    person p4("d", 4);
    person p5("e", 5);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);
    for (vector<person>::iterator it = v.begin(); it != v.end(); it++)
    {// *it 解引用的数据类型就是前面 < > 里面的数据类型
        cout << (*it).m_age << (*it).m_name << endl;
        cout << it->m_age<<it->m_name << endl;
    }

}
void test01()
{
    vector<person*>v;//放指针
    person p1("a", 1);
    person p2("b", 2);
    person p3("c", 3);
    person p4("d", 4);
    person p5("e", 5);
    v.push_back(&p1);
    v.push_back(&p2);
    v.push_back(&p3);
    v.push_back(&p4);
    v.push_back(&p5);
    for (vector<person*>::iterator it = v.begin(); it != v.end(); it++)
    {// *it 解引用的数据类型就是前面 < > 里面的数据类型
        cout << (*it)->m_age << (*it)->m_name << endl;
       
    }
}
int main()
{
    test();
}








#include <iostream>
using namespace std;
#include <string>
//容器嵌套容器
void test()
{
    vector<vector<int>>v;
    vector<int>v1;
    vector<int>v2;
    vector<int>v3;
    vector<int>v4;
    //填满小容器
    for (int i = 0; i < 4; i++)
    {
        v1.push_back(i);
        v2.push_back(i+1);
        v3.push_back(i+2);
        v4.push_back(i+3);
    }
    //小容器填满大容器
    v.push_back(v1);
    v.push_back(v2);
    v.push_back(v3);
    v.push_back(v4);

    //遍历
    for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++)//第 i 个小容器
    {// *it 就是 < > 里的数据类型   it就是容器第一个元素的地址
        for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++)//第 i 个小容器的数据遍历
        {
            cout << *vit << " ";
        }
        cout << endl;
    }
}
int main()
{
    test();
}





#include <iostream>
using namespace std;
#include <string>
//string 容器
//string构造函数
void test()
{
    const char* str = "hello world";
    string s1(str);
    cout << s1 << endl;

    string s2(s1);
    cout << s2 << endl;

    char cha = 'a';
    string s3(5, cha);
    cout << s3 << endl;
}
int main()
{
    test();
}




#include <iostream>
using namespace std;
#include <string>
void test()
{
    const char* str = "hello world";
    string s1(str);
    cout << s1 << endl;

    string s2(s1);
    cout << s2 << endl;

    char cha = 'a';
    string s3(5, cha);
    cout << s3 << endl;

    string s4;
    s4.assign("hello world", 7);
    cout << s4 << endl;

    string str5 = "wangananan";
    int pos = str5.find("an");//第一次出现位置
    if (pos == -1)
    {
        cout << "meiyou" << endl;
    }
    else
    {
        cout<<"di "<<pos<<" chuxian"<<endl;
    }
    str5.replace(2, 3, "111111111");//替换
    cout << str5 << endl;
    //string比较
    string a = "123456";
    string b = "123466";
    if (a.compare(b) == 0)// = 就是字符串相同
    {
        cout << " == " << endl;
    }
    else if (a.compare(b) > 0)
    {
        cout << ">" << endl;
    }
    else
    {
        cout << "<" << endl;
    }
    //字符获取  修改
    string c = "hello";
    for (int i = 0; i < c.size(); i++)
    {
        c[i] = '1';//修改
        cout << c[i] << " ";
    }
    cout << endl;
    for (int i = 0; i < c.size(); i++)
    {
        cout << c.at(i) << " ";
    }
    cout << endl;
    //获取子串
    string d = "qwerasdf";
    string e = d.substr(3, 3);//第三个开始 取三个
    cout << e << endl;
    string Emile = "wangshaobo@sina.com";
    int p = Emile.find("@");
    string name = Emile.substr(0, p);
    cout << name << endl;
}
int main()
{
    test();
}





#include <iostream>
using namespace std;
#include <string>
//vector容器
void print(vector<int>&a)
{
    for (vector<int>::iterator it = a.begin(); a.end() != it; it++)
    {
        cout << *it << endl;
    }
}
void test()
{
    vector<int>v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    print(v);
}
//vector容量  会动态扩展
//capacity永远>= size
void myprint(vector<int>&v)
{
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it <<" ";
    }
    cout <<  endl;
}
void test1()
{
    vector<int>a;
    for (int i = 0; i < 10; i++) 
    {
        a.push_back(i);
    }
    myprint(a);
    if (a.empty())
    {
        //真 就是空容器
        cout << "kong" << endl;
    }
    else
    {
        cout << "bu kong" << endl;
        cout << "容量是" << a.capacity() << endl;
        cout << "容量是" << a.size() << endl;
    }
    //重新指定大小
    //a.resize(15);//后面补0
   // a.resize(15, 100);//指定后面的填充值
    a.resize(5);//大小变化  但是容量capacity不变
    myprint(a);

    //vector数据存取
    for (int i = 0; i < a.size(); i++)
    {
        cout << a[i] << endl;
        cout << a.at(i) << endl;
    }
    cout << *a.begin() << endl;
    cout << a.front() << endl;//第一个元素
    //cout << *a.end() << endl;
    cout << a.back() << endl;//最后一个元素

    //容器互换
    v1.swap(v2);//互换
    //swap可以收缩内存
    vector<int>(v).swap(v);

    //vector预留空间
    v.reserve(10000);//提前知道要多少内存  就可以预留空间
}


int main()
{
    //test();
    test1();
}





#include <iostream>
using namespace std;
#include <string>
//deque容器
//deque双端数组  vector单端数组
void print(const deque<int>&a)//常量
{
    for (deque<int>::const_iterator it = a.begin(); it != a.end(); it++)
    {//  const_iterator
        //*it = 100;常量数据不可修改
        cout << *it << " ";
    }
    cout << endl;
}
void test()
{
    deque<int>d;
    for (int a = 0; a < 10; a++)
    {
        d.push_back(a);
    }
    print(d);
    deque<int>d1;
    d1 = d;
    print(d1);
    deque<int>d2;
    d2.assign(d1.begin(),d1.end());
    print(d2);
    deque<int>d3;
    d3.assign(10, 100);
    print(d3);


    d.empty();//真 为空
    d.size();//大小
    d.resize(15);//重置大小

    d.push_back(2000);//尾插
    d.push_front(10);//头插
    print(d);
    d.pop_back();//尾删
    d.pop_front();//头删
    d.insert(d.begin(), 5, 3);//插入
    d.insert(d.begin(), d3.begin(), d3.end());
    //d.erase(d.begin(),d.end());
    print(d);
    cout << d1[0] << endl;
    cout << d1.at(1) << endl;
    cout << d.front() << endl;
    cout << d.back() << endl;

    //deque排序
    sort(d.begin(), d.end());
    print(d);
    
}
int main()
{
    test();
    //test1();
}





#include <iostream>
using namespace std;
#include <string>
#include <ctime>
#include <algorithm>
//评委打分
class person
{
public:
    person(string name, int score)
    {
        this->m_name = name;
        this->m_score = score;
    }
    string m_name;
    int m_score;//平均分

};
void setscore(vector<person>&v)
{
    for (vector<person>::iterator it = v.begin(); it != v.end(); it++)
    {
        deque<int>d;//存分数
        for (int a = 0; a < 10; a++)
        {
            int score = rand() %41 + 60;
            d.push_back(score);
        }
        sort(d.begin(),d.end());
        d.insert(d.begin(), 0);
        d.insert(d.end(), 0);
        /*d.pop_back();
        d.pop_front();*/
        int sum = 0;
        for (deque<int>::iterator dt = d.begin(); dt != d.end(); dt++)
        {
            sum += *dt;//求和
        }
        int avg = sum / d.size();//平均分
        (*it).m_score = avg;

    }

}
void test()
{
    vector<person>v;//存学生容器
    person a("a", 0);
    person b("b", 0);
    person c("c", 0);
    person d("d", 0);
    person e("e", 0);
    v.push_back(a);
    v.push_back(b);
    v.push_back(c);
    v.push_back(d);
    v.push_back(e);//存学生到vector

    测试容器
    //for (vector<person>::iterator it = v.begin(); it != v.end(); it++)
    //{
    //    cout << (*it).m_name << (*it).m_score << endl;
    //}
    setscore(v);//获取分数
    for (vector<person>::iterator it = v.begin(); it != v.end(); it++)
        {
            cout << (*it).m_name << (*it).m_score << endl;
        }

}
int main()
{
    srand((unsigned int)time(NULL));//随机数种子
    test();
    //test1();
}





#include <iostream>
using namespace std;
#include <string>
//stack容器   栈  先进后出
//栈没有遍历行为
void test()
{
    stack<int>s;
    s.push(10);//入栈
    s.push(20);
    s.push(30);
    s.push(40);
    while (!s.empty())//真  为空
    {
        cout << s.top() << endl;//栈顶
        s.pop();//出栈
    }
    cout << s.size() << endl;;

}

int main()
{
    test();
    //test1();
}





#include <iostream>
using namespace std;
#include <string>
//queue  队列  先进先出  各端只能进出
//没有遍历行为  只有两端能被访问
class person
{
public:
    person(string name, int age)
    {
        this->m_name = name;
        this->m_age = age;

    }
    int m_age;
    string m_name;

};
void test()
{
    queue<person>q;
    person p1("a", 10);
    person p2("b", 20);
    person p3("c", 30);
    person p4("d", 40);
    q.push(p1);
    q.push(p2);
    q.push(p3);
    q.push(p4);
    cout << q.size() << endl;
    while (!q.empty())
    {
        cout << q.front().m_age<<q.front().m_name << endl;
        cout << q.back().m_age<<q.back().m_name << endl;
        q.pop();
    }
    cout << q.size() << endl;
}

int main()
{
    //srand((unsigned int)time(NULL));
    test();
    //test1();
}





#include <iostream>
using namespace std;
#include <string>
//list容器  链表
//存在数据域 指针域
void print(const list<int>& a)
{
    for (list<int>::const_iterator it = a.begin(); it != a.end(); it++)
    {
        cout << (*it) << endl;
    }
}
bool listsort(int a,int b)
{
    return a > b;
}
void test()
{
    list<int>L;
    L.push_back(10);
    L.push_back(20);
    L.push_back(30);
    L.push_back(40);
    L.push_front(90);//插
    L.pop_front();//删
    L.pop_back();
    print(L);

    list<int>L1(L.begin(), L.end());
    print(L1);
    list<int>L2(L);
    print(L2);
    list<int>L3(2,10);
    print(L3);
    list<int>L4;
    //L4 = L;
    // L4.assign(L.begin(), L.end());
    //赋值 交换
    L4.assign(10,0);
    print(L4);
    print(L1);
    L4.swap(L1);
    print(L4);
    print(L1);
    if (L4.empty())
    {
        cout << "kong" << endl;
    }
    else
    {
        cout << "bu kong" << endl;
        cout << L4.size() << endl;
    }
    //插入  删除
    L4.resize(2);
    L4.resize(5, 10000);
    L4.insert(++L4.begin() , 1000000);//list不支持随机访问 L4.begin()+1不行
    L4.erase(++L4.begin());//list支持双向递增 递减
    cout << L4.front() << endl;//第一个元素
    cout << L4.back() << endl;//最后一个元素
    L4.remove(10);//移除所有的10
    L4.clear();//清空
    print(L4);
    //反转
    list<int>L5;
    L5.push_back(1);
    L5.push_back(5);
    L5.push_back(9);
    print(L5);
    L5.reverse();//反转
    print(L5);
    //排序
    L5.sort();//不能 sort(L5.begin(),L5.end())    //升序
    L5.sort(listsort);//降序
    print(L5);


}

int main()
{
    //srand((unsigned int)time(NULL));
    test();
    //test1();
}







#include <iostream>
using namespace std;
#include <string>
//set/multiset  关联式容器  都用<set>头文件
//插入时自动排序
//set不允许有重复元素
//multiset允许有重复元素
class person
{
public:
    person(string name,int age)
    {
        this->m_name = name;
        this->m_age = age;
    }
    string m_name;
    int m_age;
};
class personcompare
{
public:
    bool operator()(const person& p1, const person& p2)const
    {
        return p1.m_age > p2.m_age;
    }

};
/* class mycompare//仿函数 决定内置数据类型排序规则
{
public:
    bool operator()(int a, int b)const//注意后面的 const
    {
        return a > b;
    }
}; */
/* void print(set<int>& a)
{
    for (set<int>::iterator it = a.begin(); it != a.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
} */
void test()
{
    /* set<int, mycompare>s;//插入数据前 就指定排序规则
    //只有insert方式插入数据  直接自己排序
    s.insert(80);
    s.insert(1);
    s.insert(90);
    s.insert(0);
    for (set<int, mycompare>::iterator it = s.begin(); it != s.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl; */
    //排序
    
    //寻找元素
   /*   set<int>::iterator pos = s.find(90);
    if (pos != s.end())
    {
        cout << "找到了" << *pos << endl;
    }
    else
    {
        cout << "没有" << endl;
    }
    //统计  对于set来说 不允许有重复数据
    cout << s.count(90) << endl;

    set<int>s1(s);
    print(s1);

    set<int>s2;
    s2 = s;
    print(s2);

    //s.swap(s1);//交换
    //s.empty();//真 为空
    s.erase(s.begin());//删除第一个
    s.erase(90);//删除所有 90 这个元素
    //s.erase(s.begin(), s.end());
    s.clear();//清空
    print(s);

    //查找 统计

    multiset<int>m;
    m.insert(10);
    m.insert(10);
    m.insert(10);
    m.insert(10);

    //数组  不需要头文件
    pair<string, int>p("tom", 10);
    cout << p.first << p.second << endl;//p.first  p.second直接调用 没有()
    pair<string, int>p1 = make_pair("jerry", 90);// make_pair
    cout << p1.first << p1.second << endl;*/


    //自定义数据类型排序  ,  指定排序规则

    set<person, personcompare>ss;
    person p1("a", 12);
    person p2("b", 100);
    person p3("c", 90);
    person p4("e", 190);
    ss.insert(p1);
    ss.insert(p2);
    ss.insert(p3);
    ss.insert(p4);
    for (set<person, personcompare>::iterator it = ss.begin(); it != ss.end(); it++)
    {
        cout << it->m_age << it->m_name << endl;
    }
}
int main()
{
    //srand((unsigned int)time(NULL));
    test();
    //test1();
}








#include <iostream>
using namespace std;
#include <map>
//map  multimap
//map里面都是  对组  第一个元素(key 键值)  第二个(value 实值)
// map不允许插入重复的 key 
// multimap允许插入重复的 key 
//自动排序
class mycompare
{
public:

    bool operator()(int a,int b)const
    {//降序
        return a > b;
    }

};
void print(map<int,int,mycompare>&m)
{
    for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
    {
        cout << (*it).first << (*it).second << endl;
    }
}
void test()
{
    map<int, int,mycompare>m;
    m.insert(pair<int, int>(3, 10));//map里面都是  对组
    m.insert(pair<int, int>(8, 19));//map里面都是  对组
    m.insert(pair<int, int>(1, 40));//map里面都是  对组
    //m.insert(pair<int, int>(2, 1));//map里面都是  对组
    print(m);

    //map<int, int>m1;
    //m1 = m;
    map<int, int>m1(m);//拷贝
    print(m1);
    m1.insert(pair<int, int>(2, 1));//map里面都是  对组
    m1.insert(make_pair(9, 9));//  建议
    int size = m.size();
    cout << size << endl;
    if (m.empty())
    {
        cout << "kong" << endl;
    }
    else
    {
        cout << "bu kong" << endl;
    }
    m.swap(m1);
    print(m);
    m.erase(9);//查找key值 来删除
    print(m);
    m.erase(m.begin());
    print(m);
    //m.clear();
    print(m);

    map<int,int>::iterator it= m.find(3);//第一个key值
    if (it != m.end())
    {
        cout << "zhao dao le" << endl;
        cout << (*it).first<<it->second << endl;
    }
    else
    {
        cout << "mei you" << endl;
    }
    int num = m.count(3);//通过key来查找,因为map不允许重复key  所以结果不是0就是1;multimap不一样
    cout << num << endl;
}


int main()
{
    //srand((unsigned int)time(NULL));
    test();
    //test1();
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值