Boost source learning three [utilities] (4)

Source: Internet
Author: User

The fourth one is assign,

#include <std.hpp> using namespace std; #include <boost/assign.hpp>/*assign: In many cases we need to initialize or assign values to the container, filling in a large amount of data, such as the initial error code and error message, or some test data.
The STL container provides only the means to hold the data, but the filling step is rather cumbersome, and you must repeatedly call the insert () or push_back () member functions, which is why Boost.assign appears.
The Assign library overloads the assignment operators, operator + +, the comma operator operator, and the bracket operator operator (), which can be very handy to assign or initialize an STL container in an incredibly concise syntax, which is useful where you need to fill in a large number of initial values.
#include <boost/assign.hpp> using namespace boost::assign;
add element boost.assign to the container with the operator + +, because the operator = + and comma are overloaded, you can complete the work with a lot of code with simple to shocking syntax.
    You must use a using indicator when you use the Assign library, only so that the overloaded + + operator can take effect within the scope/void Case1 () {using namespace boost::assign;
    Vector<int> v;

    V + + 1,2,3,4,5, 6*6;
    for (auto& x:v) cout << x << ",";

    cout << Endl;
    Set<string> s;

    S + + "CPP", "Java", "C #", "Python";
    for (auto& x:s) cout << x << ",";

    cout << Endl;
Map<int, string> m;
  m + + make_pair (0, "empty"), Make_pair (1, "one"), Make_pair (2, "two");   COUT << m[0]<<endl;
  cout << m[1] << Endl;
  cout << m[2] << Endl;
}/* Use the operator () to add elements to the container: there are some small restrictions on the use of operator+=, and there is trouble handling the map container, assign library uses the operator operator () to provide a more general solution.
Instead of using operator () directly, you should use the Assign library to provide three auxiliary function inserts (), Push_front (), push_back ().
These functions can be used to hold containers with member functions of the same name, accept container variables as arguments, and return a proxy object List_inserter, which overloads the operator (), = and other operators to implement the function of filling data into the container.
    * * #include <forward_list> void Case2 () {using namespace boost::assign;
    Vector<int> v;

    Push_back (v) (1) (2) (3) (4) (5);
    List<string> l;

    Push_front (L) ("CPP") ("Java") ("C #") ("Python");
    forward_list<string> FL;

    Push_front (L) ("Matrix") ("Reload");
    Set<double> s;

Insert (s) (3.14) (0.618) (1.732);
  Map<int, string> m;
  Map <int, String>::iterator t;
  Insert (m) (1, "Hello") (2, "How are You?") (3, "Are You really okay");    for (t = M.begin (); t!= M.end (); ++t)    cout << t->first << "," << T->second
 << Endl;   cout << Endl;

    The/* Bracket operator can also be used with the comma and other operators to use the */void Case3 () {using namespace boost::assign;
    Vector<int> v;
    Push_back (v), 1,2,3,4,5;

    Push_back (v) (6), 7,64/8, (9), 10;
    for (auto& x:v) cout << x << ",";

    cout << Endl;
    Deque<string> D;
    Push_front (d) () = "cpp", "Java", "C #", "Python";

    ASSERT (D.size () ==5);
    for (auto& x:d) cout << x << ",";

cout << Endl; /* Initialize container elements: operator = = and () solves the problem of assigning to the container, but it is sometimes necessary to fill the data when the container is constructed, which is more efficient than the assignment.
 The arrays and standard string classes built in C + + support this, but the STL container does not.
The Assign library uses list_of (), map_list_of ()/pair_list_of () and tuple_list_of () three functions to solve the problem.
  The use of the list_of:list_of () function is similar to the previous insert (), push_back (), and also overloads the parentheses, the comma operator.
It's smart, and returns an anonymous list that can be assigned to any container.
    */void Case4 () {using namespace boost::assign;
    Vector<int> v = list_of (1) (2) (3) (4) (5);
    v = [1, 2, 3, 4, 5] deque<string> d = (list_of ("Power") ("Bomb"), "Phazon", "suit"); d = [Power Bomb phaZon Suit] set<int> s = (list_of (10), 20,30,40,50);
    s = {Map<int, string> m = list_of (Make_pair (1, "one")) (Make_pair (2, "two"));
    M = [(1, "one") (2, "two")] map<int, int> m1 = map_list_of (1, 2) (3, 4) (5, 6);
    m1 = [(1, 2) (3, 4) (5, 6)] map<int, string> m2 = map_list_of (1, "one") (2, "two");
  M2 = [(1, "one") (2, "two")]}/* Reduce duplicate Input: Assign library provides repeat (), Repeat_fun (), and range () three functions to reduce duplicate input; The repeat () function repeats the second argument as the value to be filled in, repeating the number of times specified by the first argument, similar to the constructor of a container like Vector,deque, where the Repeat_fun () function repeats the number of the first argument, but the second argument is a function or function object that is not a parameter.
  It returns the value that is filled in, and the range () function inserts a sequence of all or part of the elements into another sequence.

    * * #include <cstdlib>//for rand () void Case5 () {using namespace boost::assign;
    Vector<int> v = list_of (1). Repeat (3, 2) (3) (4) (5);
    v = 1,2,2,2,3,4,5 for (auto& x:v) cout << x << ",";

    cout << Endl;
    Multiset<int> MS; Insert (ms). Repeat_fun (5, &rand). Repeat (2, 1), 10;
    ms = x,x,x,x,x,1,1,10 for (auto& x:ms) cout << x << ",";

    cout << Endl;
    Deque<int> D;
    Push_front (d). Range (V.begin (), V.begin () + 5);
    D = 3,2,2,2,1 for (auto& x:d) cout << x << ",";

cout << Endl; }/* Work with non-standard containers: The Assign library not only supports all eight STL standard containers (VECTOR,STRING,DEQUE,LIST,SET,MULTISET,MAP,MULTIMAO), but also provides appropriate support for the container adapters in the STL.
  including Stack,queue and Priority_queue. Because a container adapter such as stack does not conform to the definition of a container, there is no member function such as Insert,push_back, so you cannot populate the element with an assignment, you can only use initialization, and then use the To_adapter () in list_of expression. member functions to fit into non-standard containers.
  If you use the comma operator you also need to enclose the entire expression in parentheses to invoke To_adapter () using the dot number.

    * * #include <stack> #include <queue> void Case6 () {using namespace boost::assign;
    stack<int> Stk = (list_of (1), 2, 3). To_adapter ();
    STK + + 4, 5, 6; for (;!
    Stk.empty ();)
        {cout << stk.top () << "";
    Stk.pop ();

    } cout << Endl;
 queue<string> q = (list_of ("a") ("Us") ("UK").       Repeat (2, "Russia"). To_adapter ();
    Push (Q) ("Germany"); for (;!
    Q.empty ();)
        {cout << q.front () << "";
    Q.pop ();

    } cout << Endl;
    Priority_queue<double> PQ = (list_of (1.414), 1.732, 2.236). To_adapter ();
    Push (PQ), 3.414, 2.71828; for (;!
    Pq.empty ();)
        {cout << pq.top () << "";
    Pq.pop ();
}/* Reference initialization list: There are two similar ref_list_of () and cref_list_of () in the Assign library outside List_of, which accept the reference of the variable as a parameter to create an initialization anonymous list that is more efficient than the list_of ().

    */void Case7 () {using namespace boost::assign;

    int a = 1, b = 2, c = 3;
    Vector<int> v = ref_list_of<3> (a) (b) (c);
ASSERT (v.size () = = 3);
    int main () {case1 ();
    Case2 ();
    CASE3 ();
    Case4 ();
    CASE5 ();
    Case6 ();
Case7 ();
 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.