zoukankan      html  css  js  c++  java
  • STL---llist

    #include<iostream>
    #include<algorithm>
    #include<list>
    using namespace std;
    struct Node
    {
        char a;
        int b;
    };
    
    void func(Node &t)
    {
        cout << t.a << "   " << t.b << "
    "; //使用换行,尽量使用
    ,endl的效率会慢很多
                                             //这里只会打印结构体中的b 因为对于int了类型的0可以直接打印,对于char类型的0,他会找到0对应的assic码,而0对应的assci码无法在控制台打印
        cout << "强制转换 " << (int)t.a << "
    ";
    }
    int main()
    {
        list<Node> li;   //无参构造
    
        list<Node> li1(5);   //结构体重的元素被初始化为0
        for_each(li1.begin(), li1.end(), func);
    
        return 0;
    }
    View Code
    #include<iostream>
    #include<algorithm>
    #include<list>
    using namespace std;
    struct Node
    {
        char a;
        int b;
    };
    
    void func(Node &t)
    {
        cout << t.b << "   " << t.a << "
    "; 
    }
    int main()
    {
        
        Node node = { 'a',5 };
        list<Node> li(6, node);
        for_each(li.begin(), li.end(), func);
    
        cout << "***************************************
    ";
        list<Node> li2(li);
        for_each(li2.begin(), li2.end(), func);
    
        cout << "***************************************
    ";
        list<Node> li3(li.begin(),li.end());
        for_each(li3.begin(), li3.end(), func);
    
        list<Node>::iterator ite = li3.begin();
        while (ite != li3.end())
        {
            cout << ite->a << "  " << ite->b << "
    ";
            ite++;
        }
        return 0;
    }
    View Code
    #include<iostream>
    #include<algorithm>
    #include<list>
    using namespace std;
    struct Node
    {
        char a;
        int b;
    };
    
    void func(Node &item)
    {
        cout << item.a << "  " << item.b << "
    ";
    }
    int main()
    {
        Node node = { 'a',5 };
        list<Node> li(5, node);
        cout << "list中有多少个元素: " << li.size() << "
    ";
        cout << "这个list是否为空 " << li.empty() << "
    ";
        for_each(li.begin(), li.end(), func);
    
        cout << "************************************************
    ";
        li.resize(3);
        for_each(li.begin(), li.end(), func);
        cout << "经过resize改变后的容量: " << li.size() << "
    ";
        cout << "这个list是否为空 " << li.empty() << "
    ";
    
        cout << "************************************************
    ";
        li.resize(0);
        for_each(li.begin(), li.end(), func);
        cout << "这个list是否为空 " << li.empty() << "
    ";
    
        return 0;
    }
    View Code
    #include<iostream>
    #include<algorithm>
    #include<list>
    using namespace std;
    struct Node
    {
        char a;
        int b;
    
        Node(char c, int d)
        {
            a = c;
            b = d;
        }
    };
    
    void func(Node &t)
    {
        cout << t.a << "  " << t.b << "
    ";
    }
    int main()
    {
        Node node('a', 5);   //挡结构体中有初始化函数,要像使用类一样去定义这个结构体变量
        list<Node> li(5, node);
    
        li.push_back(Node('b', 9));  //输入元素
        li.push_front(Node('c', 5));
    
        cout << "li.front  a   " << li.front().a << "
    ";   //输出元素
        cout << "li.back   a   " << li.back().a << "
    ";
    
    
        //在中间添加元素
        list<Node>::iterator ite = li.begin();
        ite++;
        li.insert(ite, Node('d', 1));  //第一个参数是一个迭代器,第二个参数是要插入的节点元素
        li.insert(ite, 3, Node('e', 2));  //插入三个相同的元素
    
        for_each(li.begin(), li.end(), func);  
    
        return 0;
    }
    View Code
    #include<iostream>
    #include<algorithm>
    #include<list>
    using namespace std;
    struct Node
    {
        char a;
        int b;
    
        Node(char c, int d)
        {
            a = c;
            b = d;
        }
    
        bool operator == (const Node& t)   //要使用list中的remove函数就必须重载这个函数
        {
            if ((t.a == this->a) && (t.b == this->b))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    };
    
    void func(Node &t)
    {
        cout << t.a << "  " << t.b << "
    ";
    }
    int main()
    {
        list<Node> li;
        li.push_back(Node('a', 1));
        li.push_back(Node('b', 2));
        li.push_back(Node('c', 3));
        li.push_back(Node('d', 4));
        li.push_back(Node('e', 5));
        li.push_back(Node('d', 4));
    
    
        //li.pop_back();  //尾删除
        //li.pop_front(); //头删除
    
        list<Node>::iterator ite = li.begin();
        ite++;
        li.erase(ite);  //删除一个指定的元素
                  //这个函数里面还可以放两个迭代器,表示删除这一段元素
    
        li.remove(Node('e', 5));   //当这个list中有与之一致的结点就删除这个结点,有多个的情况下就删除多个
        li.unique();  //去重
    
        li.assign(3, Node('a', 3));  //重新赋值
                                    //这个函数还可以传入两个迭代器,表示用迭代器中间的元素来重新赋值这个list对象
    
        for_each(li.begin(), li.end(), func);
    
    
        return 0;
    }
    View Code
    #include<iostream>
    #include<algorithm>
    #include<list>
    using namespace std;
    struct Node
    {
        char a;
        int b;
    
        Node(char c, int d)
        {
            a = c;
            b = d;
        }
    
        bool operator < (const Node& t)   //使用sort函数,要重载<
        {
            if ( t.b < this->b)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    };
    
    void func(Node &t)
    {
        cout << t.a << "  " << t.b << "
    ";
    }
    int main()
    {
        list<Node> li;
        list<Node> li1;
        li.push_back(Node('a', 1));
        li.push_back(Node('b', 2));
        li.push_back(Node('c', 3));
        li1.push_back(Node('d', 4));
        li1.push_back(Node('f', 6));
        li1.push_back(Node('e', 5));
        
    
        li.swap(li1);  //交换连个list对象
    
        li.reverse();  //翻转这个list对象中的元素
    
        li.sort();  //排序
        for_each(li.begin(), li.end(), func);
    
    
        return 0;
    }
    View Code
    #include<iostream>
    #include<algorithm>
    #include<list>
    using namespace std;
    struct Node
    {
        char a;
        int b;
    
        Node(char c, int d)
        {
            a = c;
            b = d;
        }
    
        bool operator < (const Node& t)  
        {
            if ( t.b > this->b)   //从小到大
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    };
    
    void func(Node &t)
    {
        cout << t.a << "  " << t.b << "
    ";
    }
    int main()
    {
        list<Node> li;
        list<Node> li1;
        li.push_back(Node('a', 1));
        li.push_back(Node('b', 2));
        li.push_back(Node('c', 3));
        li1.push_back(Node('d', 4));
        li1.push_back(Node('e', 5));
        li1.push_back(Node('f', 6));
    
    
        li.merge(li1);  //排序函数需要注意几观点
                //首先排序前 这两个list必须有序,且有序味相同性质(都是从小到大或者都是从大到小)
                //当按照你设置的规则排序两个从小到大的list的时候,注意上面的重载
                //当排序的list是从大到小的时候,就需要需要将上面的重载改变一下(改成相反的)
        for_each(li.begin(), li.end(), func);
    
    
        return 0;
    }
    View Code
    #include<iostream>
    #include<algorithm>
    #include<list>
    using namespace std;
    struct Node
    {
        char a;
        int b;
    
        Node(char c, int d)
        {
            a = c;
            b = d;
        }
    
        bool operator == (const Node t)
        {
            if ((t.a == this->a) && (t.b == this->b))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    
    };
    
    void func(Node &t)
    {
        cout << t.a << "  " << t.b << "
    ";
    }
    int main()
    {
        list<Node> li;
        li.push_back(Node('a', 1));
        li.push_back(Node('b', 2));
        li.push_back(Node('c', 3));
        li.push_back(Node('d', 4));
        li.push_back(Node('e', 5));
        li.push_back(Node('f', 6));
    
        list<Node>::iterator ite = find(li.begin(), li.end(), Node('c', 3));
        cout << ite->a << "   " <<ite->b << "
    ";
    
        //foreard_list  单向队列,有兴趣可以自行百度
    
        return 0;
    }
    View Code
  • 相关阅读:
    软件测试描述错误
    软件测试homework2
    第九次
    第七次作业
    第六次作业
    第五次作业
    第四次作业
    第三次
    软件测试Lab2 Selenium及自动化测试
    软件测试(四)主路径覆盖hw3
  • 原文地址:https://www.cnblogs.com/luojianyi/p/9737498.html
Copyright © 2011-2022 走看看