zoukankan      html  css  js  c++  java
  • 8.boost_array_any

     1 #include <iostream>
     2 #include <string>
     3 #include <boost/array.hpp>
     4 //异构的容器
     5 #include <boost/any.hpp>
     6 #include <vector>
     7 #include <typeinfo>
     8 #include <algorithm>
     9 using namespace std;
    10 using namespace boost;
    11 
    12 void main()
    13 {
    14     boost::array<string, 5>mystr = { "12","ab","34","ef","334" };
    15     boost::array<string, 5>::iterator ib = mystr.begin();
    16     boost::array<string, 5>::iterator ie = mystr.end();
    17     for (; ib != ie; ib++)
    18     {
    19         cout << *ib << endl;
    20     }
    21 
    22     //返回数组首地址
    23     string *p = mystr.data();
    24     cout << *p << endl;
    25     mystr[3] = "90";
    26     mystr.at(3) = "123";
    27 
    28     //异构容器
    29     std::vector<boost::any> s_values;
    30     s_values.push_back(123);
    31     s_values.push_back('A');
    32     s_values.push_back(19.8);
    33     s_values.push_back("12345");
    34     //cout << boost::any_cast<double>(s_values[2]) << endl;
    35 
    36     for_each(s_values.begin(),s_values.end(),
    37         [](const boost::any &anydata)
    38     {
    39         //获取类型
    40         const std::type_info &ti = anydata.type();
    41         //根据类型执行相应的操作
    42         if (ti == typeid(int))
    43         {
    44             cout << boost::any_cast<int>(anydata) << endl;
    45         }
    46         else if (ti == typeid(double))
    47         {
    48             cout << boost::any_cast<double>(anydata) << endl;
    49         }
    50         else if (ti == typeid(const char *))
    51         {
    52             cout << boost::any_cast<const char *>(anydata) << endl;
    53         }
    54         else if (ti == typeid(char))
    55         {
    56             cout << boost::any_cast<char>(anydata) << endl;
    57         }
    58     });
    59 
    60     cin.get();
    61 }
  • 相关阅读:
    syslog远程日志存储/514端口【转】
    干掉僵尸进程
    以太网,IP,TCP,UDP数据包分析【转】
    suricata 的安装编译
    libpcap详解【转】
    蛋疼的经历--wireshark不能启动的问题
    Ubuntu中root用户和user用户的相互切换[转载自93度的饼干]
    OSI七层与TCP/IP五层网络架构详解
    __cplusplus的用法(转)
    IPsec ISAKMP(转)
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8653414.html
Copyright © 2011-2022 走看看