学习C++STL容器及其算法

#include <iostream>
#include <algorithm>

#include <vector>
#include <deque>
#include <queue>
#include <string>
#include <stack>
#include <list>
#include <set>
#include <map>

using namespace std;

class Student
{
public:
    int id;
    string name;
    int grade;

public:
    Student(int id,string name,int grade):id(id),name(name),grade(grade) {};

public:
    Student() {};

public: 
    const bool operator<(const Student& other)
    {
        return grade < other.grade;
    }
};

//给vector<Student> 类型变量赋值
vector<Student> addStudent()
{
    Student stu;
    vector<Student> v;
    while (1)
    {
        cout << "请输入id:";
        cin >> stu.id;
        cout << "请输入name:";
        cin >> stu.name;
        cout << "请输入grade:";
        cin >> stu.grade;
        if (stu.id == 0)break;
        v.push_back(stu);
    }
    return v;
}

//打印所有vector的所有成员;
void PrintAllVector(vector<Student> v)
{
    void PrintVector(Student s);
    for_each(v.begin(), v.end(), PrintVector);
}

//作为for_each()的第三个参数;
void PrintVector(Student s)
{
    cout <<"id:" << s.id << "; name:" << s.name << "; grade:" << s.grade << endl;
}
void PrintDeque(int a)
{
    cout << a << " ";
}
void PrintList(int a)
{
    cout << a << " ";
}

//用于set< T ,myCompare>类型数据 进行排序
class myCompare
{
public:
    bool operator()(Student s1, Student s2)const
    {
        return s1.grade > s2.grade;
    }
};

//vector类
void test01()
{

    vector<Student> v;
    vector<Student>::iterator it = v.begin();
    v = addStudent();
    PrintAllVector(v);

    cout << "---------------------------------------" << endl;

    //排序
    sort(v.begin(), v.end());
    PrintAllVector(v);

    cout << "---------------------------------------" << endl;
    //v.capacity(); 
    cout << "v.capacity:" << v.capacity() << endl;

    //swap收缩空间
    vector<int>v1(1200);
    cout << "v1.capacity:" << v1.capacity() << endl;
    v1.resize(10);
    cout << "v1.capacity:" << v1.capacity() << endl;
    vector<int>(v1).swap(v1);
    cout << "v1.capacity:" << v1.capacity() << endl;

}

//string类
void test02()
{
    //str.append(string ,pos ,len );
    string str = "abcdefg";
    string str1 = "hijklmn";
    string::iterator it = str.begin();
    str.append(str1, 3, 2);
    cout << "append后的str:" << str << endl;

    cout << endl;

    //str.insert(pos,len);
    str = "abcdefg";
    str1 = "hijklmn";
    cout << "str = " << str << endl;
    str.insert(size(str), str1);
    cout << "insert后的str:"  << str << endl;

    cout << endl;

    if (str.empty() == 0)cout << "not empty!" << endl;

    cout << endl;

    //str.substr(pos,len);
    str = "abcdefg";
    str1 = "hijklmn";
    cout << "str = " << str << endl;
    string str3 = str.substr(3, 4);
    cout << str3 << endl;

    cout << endl;

    //str.replace()
    str = "abcdefg";
    str1 = "hijklmn";
    cout << "str = " << str << endl;
    str.replace(str.begin(), str.end(), str1); //迭代器写法
    //str.replace(0, 4, str1);        //pos写法,意思是从str的第0位开始到第四位,全部删除,替换为整一个str1字符串
    cout << "replace后的str:" << str << endl;

    cout << endl;

    //str.erase()
    str = "abcdefg";
    str1 = "hijklmn";
    cout << "str = " << str << endl;
    str.erase(it, it + 2);//迭代器写法
    cout << "erase后的str:" << str << endl;
    //str.erase(0, 2);//pos写法
    //cout << "str = " << str << endl;

    cout << endl;

    //str.compare()
    str = "abcdefg";
    str1 = "hijklmn";
    cout << "str = " << str << endl;
    cout << "str.compare(str1)的结果:";
    if (str.compare(str1) < 0)cout << "str < str1" << endl;

    cout << endl;

    //find
    str = "abcdefg";
    str1 = "hijklmn";
    cout << "str = " << str << endl;
    cout << "find 'd'的下标:" << str.find('d') << endl;

    cout << endl;  


}

//deque类 双端队列
void test03()
{
    deque<int> d1;
    deque<int> d2(10, 5);
    deque<int> d3(d2.begin(),d2.end());
    deque<int> d4(d3);

    //遍历输出  迭代器方法
    for (deque<int>::iterator it = d4.begin();it != d4.end();it++)
    {
        cout << *it << " ";
    }

    d1.push_back(4);
    d1.push_back(1);
    d1.push_back(7);
    d1.push_back(9);
    d1.push_back(2);
    cout << endl;
    cout << "遍历d1中所有元素:";
    for_each(d1.begin(), d1.end(), PrintDeque);
    cout << endl;

    d1.push_front(7);
    d1.push_front(4);
    d1.push_front(11);
    d1.push_front(7);
    cout << endl;

    cout << "再遍历d1中所有元素:";
    for_each(d1.begin(), d1.end(), PrintDeque);
    cout << endl;
    sort(d1.begin(), d1.end(), greater<int>());
    cout << "倒叙排列后:";
    for_each(d1.begin(), d1.end(), PrintDeque);
    cout << endl;
}

//stack类 栈
//不支持随机存取和遍历
void test04()
{
    stack<int> st1;
    st1.push(1);
    st1.push(5);
    st1.push(4);
    st1.push(2);
    st1.push(9);

    while (!st1.empty())
    {
        cout << st1.top() << " ";
        st1.pop();
    }
    cout << endl;
    st1.push(1);
    st1.push(5);
    st1.push(4);
    st1.push(2);
    st1.push(9);
    cout << st1.top() << endl;
    st1.pop();
    cout << st1.top() << endl;
    st1.pop();
    cout << st1.top() << endl;
    st1.pop();
    cout << st1.top() << endl;
    st1.pop();
    cout << st1.top() << endl;
}

//queue类 队列
void test05()
{
    queue<int> q1;
    q1.push(1);
    q1.push(7);
    q1.push(2);
    q1.push(9);

    while (q1.size() != 0)
    {
        cout << q1.front() << " ";
        q1.pop();
    }
}

//list类 链表
void test06()
{
    list<int> mlist1;
    list<int> mlist2(10, 10);
    list<int> mlist3(mlist2);
    list<int> mlist4(mlist2.begin(),mlist2.end());
    //list<int>::iterator it = mlist4.begin();
    auto it = mlist4.begin();//可以使用auto自动定义变量类型
    for (it;it != mlist4.end();it++)
    {
        cout << *it << " ";
    }
    cout << endl;
    for_each(mlist4.begin(), mlist4.end(), PrintList);
    cout << endl;
    

    //若想实现迭代器it的移动,应使用next()函数往后挪n位
    it = mlist4.begin();
    it = next(it, 5);

    mlist4.insert(it, 20);
    for_each(mlist4.begin(), mlist4.end(), PrintList);
    cout << endl;
    cout << "remove所有的10后:";

    //PS:remove函数是删除所有的等于该变量(10)的值
    mlist4.remove(10);
    for_each(mlist4.begin(), mlist4.end(), PrintList);
    cout << endl;

    mlist4.push_back(30);
    mlist4.push_back(7);
    mlist4.push_back(60);
    mlist4.push_back(71);
    mlist4.push_back(13);

    cout << "遍历mlist4:";
    for_each(mlist4.begin(), mlist4.end(), PrintList);
    cout << endl;

    //assign会重置整个list
    //mlist4.assign(3, 26);

    //cout << "遍历mlist4:";
    //for_each(mlist4.begin(), mlist4.end(), PrintList);
    //cout << endl;

    //reverse
    mlist4.reverse();
    cout << "倒叙遍历mlist4:";
    for_each(mlist4.begin(), mlist4.end(), PrintList);
    cout << endl;


    //sort     PS:这里的sort是List的成员函数
    mlist4.sort();    //倒叙:greater<int>()
    cout << "正序遍历mlist4:";
    for_each(mlist4.begin(), mlist4.end(), PrintList);
    cout << endl;


}

//pair类 对组
void test07()
{
    pair<int, int> p1(10, 20);
    cout << "p1.first:" << p1.first << "  p1.second:" << p1.second << endl;

    pair<int, string> p2 = make_pair(10, "aaa");
    cout << "p2.first:" << p2.first << "  p2.second:" << p2.second << endl;

}

//set类 二叉树
void test08()
{
    //set里面的元素是自动被排序的(有序二叉树)
    //set里面的元素不可以被重复,若想改变set里的元素的值,要删了该节点,重新insert新的节点
    set<int> s1;
    s1.insert(7);
    s1.insert(2);
    s1.insert(4);
    s1.insert(5);
    s1.insert(1);

    auto it = s1.begin();
    for (it;it != s1.end();it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    //查找操作
    
    //find     返回迭代器  找不到返回s.end(),;
    auto ret = s1.find(4);
    if (ret == s1.end())cout << "找不到";
    else cout << "ret:" << *ret << endl;

    //lower_bound
    //找第一个 >= key的数
    ret = s1.lower_bound(6);
    if (ret == s1.end())cout << "找不到";
    else cout << "ret:" << *ret << endl;
    //upper_bound()  第一个 > key 的值

    //equal_range    返回两个迭代器,用pair<it,it>来存储
    //第一个返回lower_bound,第二个返回upper_bound;都是迭代器;
    pair<set<int>::iterator, set<int>::iterator> myret = s1.equal_range(7);
    if (myret.first == s1.end())cout << "没有找到!" << endl;
    else cout << "myret.first:"  << *myret.first << endl;
    if (myret.second == s1.end())cout << "没有找到!" << endl;
    else cout << "myret.second:"  << *myret.second << endl;


    //假如我要给通过二叉树存储Student类的数据,应该自定义排序类
    set<Student,myCompare> s2;
    Student stu1(1, "Thoudam", 80);
    Student stu2(2, "lis", 75);
    Student stu3(3, "actr", 92);
    Student stu4(4, "mar", 84);
    s2.insert(stu1);
    s2.insert(stu2);
    s2.insert(stu3);
    s2.insert(stu4);
    auto it2 = s2.begin();
    for (it2;it2 != s2.end();it2++)
    {
        cout << "id:" << (*it2).id << "  name:" <<(*it2).name << "  grade:" << (*it2).grade << endl;
    }
    cout << endl;


    //查找
    if (s2.find(stu1) == s2.end())cout << "没找到stu1" << endl;
    else cout << "找到了,stu1的id为:" << (*s2.find(stu1)).id << endl;


}

//map类 (key,value)自动以key排序
void test09()
{
    //map容器模板参数,第一个是key的类型,第二个是value的
    map<int, int> mymap;


    //插入数据  pair.first,   map只能insert,没有push
    mymap.insert(pair<int, int>(10, 10));
    //insert返回一个  pair(iterator,bool)  
    pair<map<int, int>::iterator, bool> ret = mymap.insert(pair<int, int>(15, 15));
    if (ret.second)
    {
        cout << "Pair(15,15) inserted successfully!" << endl;
    }
    else cout << "False!" << endl;

    ret = mymap.insert(pair<int, int>(15, 20));
    if (ret.second)
    {
        cout << "Inserted successfully!" << endl;
    }
    else cout << "Pair(15,20) inserted failed!" << endl;

    mymap.insert(make_pair(20, 20));
    mymap.insert(map<int, int>::value_type(30, 30));
    mymap[40] = 40; //重要!!!
    mymap[15] = 50; //如果key存在则修改!!!

    cout << "mymap[60]:" << mymap[60] << endl;    //如果key不存在,则插入到map中,且默认初始化

    //打印
    map<int, int> ::iterator it = mymap.begin();
    for (it;it != mymap.end();it++)
    {
        cout << "key:" << it->first << ",value:" << it->second << endl;
    }


}


int main()
{
    test09();
    return 0;
}
/*
1
Thoudam
80
2
lis
90
3
asd
70
0
0
0
*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值