zoukankan      html  css  js  c++  java
  • STL

    MapTest.cpp

    #include <map>
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include "MapTest.h"
    
    using namespace std;
    
    void MapTest::simpleEnumeration()
    {
        map<string,double> coll { 
            { "tim", 9.9 },                          
            { "struppi", 11.77 }
        } ;
    
        // for range-based enumeration
        cout << "for range-based enumeration: " << endl;
        for (auto elem : coll)
        {
            cout << elem.first << ": " << elem.second << endl;
        }
    
        // iterating
        cout << "iterating: " << endl;
        map<string, double>::iterator pos;
        for (pos = coll.begin(); pos != coll.end(); ++pos)
        {
            cout << pos->first << ": " << pos->second << endl;
        }
    
        // square the value of each element:
        for_each (coll.begin(), coll.end(),
                  [] (pair<const string,double>& elem) {
                        elem.second *= elem.second;
                  });
    
        // print each element:
        cout << "for_each lambda enumeration: " << endl;
        for_each (coll.begin(), coll.end(),
                  [] (const map<string,double>::value_type& elem) {
                        cout << elem.first << ": " << elem.second << endl;
                  });
    }
    
    void MapTest::run()
    {
        printStart("simpleEnumeration()");
        simpleEnumeration();
        printEnd("simpleEnumeration()");
    }

    运行结果:

    --------------- simpleEnumeration(): Run Start ----------------
    for range-based enumeration:
    truppi: 11.77
    im: 9.9
    terating:
    truppi: 11.77
    im: 9.9
    or_each lambda enumeration:
    truppi: 138.533
    im: 98.01
    --------------- simpleEnumeration(): Run End ----------------

  • 相关阅读:
    Python内置函数(55)——round
    Python内置函数(54)——reversed
    Python内置函数(53)——repr
    Python内置函数(52)——range
    Python内置函数(51)——property
    Python内置函数(50)——print
    Python内置函数(49)——pow
    Python内置函数(48)——ord
    Python内置函数(47)——open
    Python内置函数(46)——oct
  • 原文地址:https://www.cnblogs.com/davidgu/p/4917182.html
Copyright © 2011-2022 走看看