zoukankan      html  css  js  c++  java
  • assign

    #include <iostream> 
    #include <boost/assign.hpp> 
    #include <boost/thread.hpp>
    #include <windows.h> 
    using namespace std; 
    using namespace boost;
    using namespace boost::assign; 
    int main()
    {  
        
        vector<int> v; 
        v += 1,2,3,4,5,6 * 6;
    
        set<string> s; 
        s += "cpp","java","C#","python";
    
        map<int,string> m; 
        m += make_pair(1,"one"),make_pair(2,"two");  
        cout << m[1] << endl;  
        Sleep(2000);//这里不能写成system("pause");?????????
    
        return 0; 
    }
    View Code

    操作符+=和()解决了对容器的赋值问题,但有的时候我们需要在容器构造时就完成对数据的初始化。这种赋值方式更为高效。C++内建的数组和标准字符串类string支持这样做,但其他stl容器则不行。

    assign库使用list_of(),map_list_of()、pair_list_of和tuple_list_of这几个函数解决了这个问题。

    list_of:

    #include <iostream>
    using namespace std;
    
    #include <boost/assign.hpp>
    using namespace boost::assign;
    using namespace boost;
    
    int main()
    {
        vector<int> v = list_of(1)(2)(3)(4)(5);
        deque<string> d = 
            (list_of("Power")("bomb")("phazon")("suit"));
    
        set<int> s = (list_of(10),20,30,40,50);
    
        map<int,string> m = list_of(make_pair(1,"one"))(make_pair(2,"two"));
    
        return 0;
    }
    View Code

    list_of()函数可以全部使用括号操作符,也可以把括号与逗号结合在一起,但这要求将整个list_of表达式用括号括起来。

    #include <iostream>
    using namespace std;
    
    
    #include <map>
    
    #include <boost/assign.hpp>
    using namespace boost::assign;
    using namespace boost;
    
    int main()
    {
        map<int,int>m1 = map_list_of(1,2)(3,4)(5,6);
    
        map<int,string>m2 = map_list_of(1,"one")(2,"two");
        cout << m2[2].c_str() << endl;
        cout << m2[2].data() << endl;
        system("pause");
        return 0;
    }
    View Code


    repeat()函数:repeat_fun()函数第一个参数为重复的次数,第二个参数为一个无参数的函数或函数对象,她返回填回的值。让rang()函数可以把一个序列全部或者部分元素插入到另一个序列里。

    #include <iostream>
    using namespace std;
    
    
    #include <map>
    
    #include <boost/assign.hpp>
    using namespace boost::assign;
    using namespace boost;
    
    int main()
    {
        vector<int> v = list_of(1).repeat(3,2)(3)(4)(5);
    
        multiset<int>ms;
        insert(ms).repeat_fun(5,&rand).repeat(4,1),10;
    
        deque<int> d;
        push_front(d).range(v.begin(),v.begin() + 5);
        system("pause");
        return 0;
    }
    View Code

    assign库不仅支持全部八个STL标准容器(vector,string,deque,list,set,multiset,map,multimap),也对STL中的容器适配器提供适当的支持。包括stack,queue,和prority_queue。

    #include <iostream>
    using namespace std;
    
    #include <string>
    #include <map>
    #include <stack>
    #include <queue>
    
    
    #include <boost/assign.hpp>
    using namespace boost::assign;
    using namespace boost;
    
    int main()
    {
        stack<int> stk = (list_of(1),2,3).to_adapter();
    
        while(!stk.empty())
        {
            cout << stk.top() << " ";
            stk.pop();
        }
        cout << endl;
    
        queue<string> q = (list_of("china")("US")("UK")).repeat(2,"Russia").to_adapter();
    
        while(!q.empty())
        {
            cout << q.front() << " ";
            q.pop();
        }
        cout << endl;
    
        priority_queue<double> pq = (list_of(1.44),1.7656,2.556).to_adapter();
    
        while(!pq.empty())
        {
            cout << pq.top() << " ";
            pq.pop();
        }
        system("pause");
        return 0;
    }
    View Code

    因为stack等容器适配器不符合容器的定义,没有insert,push_back等成员函数,所以不能使用赋值值得方式填入元素,只能使用初始化的方式。并在list_of表达式最后使用to_adapter()成员函数来适配到非标准容器。如果使用逗号操作符还需要把整个表达使用括号括起来。才能调用to_adapter()。

    #include <iostream>
    using namespace std;
    
    #include <string>
    #include <map>
    #include <stack>
    #include <queue>
    
    
    #include <boost/assign.hpp>
    using namespace boost::assign;
    using namespace boost;
    
    int main()
    {
        stack<int> stk = (list_of(1),2,3).to_adapter();
    
        while(!stk.empty())
        {
            cout << stk.top() << " ";
            stk.pop();
        }
        cout << endl;
    
        queue<string> q = (list_of("china")("US")("UK")).repeat(2,"Russia").to_adapter();
    
        while(!q.empty())
        {
            cout << q.front() << " ";
            q.pop();
        }
        cout << endl;
    
        priority_queue<double> pq = (list_of(1.44),1.7656,2.556).to_adapter();
    
        while(!pq.empty())
        {
            cout << pq.top() << " ";
            pq.pop();
        }
        system("pause");
        return 0;
    }
    View Code

    list_of()的嵌套使用:

    list_of()可以就地创建匿名列表,这一点很有用,他可以嵌套在assign库用法中使用,创建复杂的数据结构。

    #include <iostream>
    using namespace std;
    
    #include <string>
    #include <map>
    #include <stack>
    #include <queue>
    
    
    #include <boost/assign.hpp>
    using namespace boost::assign;
    using namespace boost;
    
    
    int main()
    {
        vector<vector<int> > v = list_of(list_of(1)(2))(list_of(3)(4));
        
        v += list_of(5)(6),list_of(7)(8);
        system("pause");
        return 0;
    }
    View Code
  • 相关阅读:
    OAuth 2 深入介绍
    浅谈 EF CORE 迁移和实例化的几种方式
    为什么我们不应该使用微信或者 QQ 作为团队协作的 IM 工具?
    三值 bool? 进行与或运算后的结果
    Slack 开发入门之 Incoming Webhooks:往 Slack 的 Channel 中发消息
    int? 竟然真的可以是 null!.NET/C# 确定可空值类型 Nullable 实例的真实类型
    .NET 中使用 Mutex 进行跨越进程边界的同步
    UWP 在 WebView 中执行 JavaScript 代码(用于模拟用户输入等)
    谨慎使用 FileInfo.Exists 实例方法,而是使用 File.Exists 静态方法替代
    只需 5 秒钟,你就能取到 WPF 程序的超高分辨率超高清截图
  • 原文地址:https://www.cnblogs.com/jiaoluo/p/3574941.html
Copyright © 2011-2022 走看看