boost中的实用工具assign和uuid

assign

  • assign重载‘+’=和‘,’实现连续赋值
  • assign不仅支持全部8个STL标准容器(vector、string、deque、list、set、multiset、map、multimap),也对stl中的容器适配器提供了适当的支持,包括queue和priority_queue
  • 示例代码:
int testAssign()
{
    using namespace boost::assign;

    // overload operator '+=' & ','
    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");

    // overload '()'
    vector<int> v1;
    push_back(v)(1)(2)(3)(4)(5);

    return 0;
}

uuid

  • 生成各种各样的uuid
  • 在用string_generator时,需要添加宏定义_SCL_SECURE_NO_WARNINGS;
  • 在使用cout << uuid 和 to_string(uuid)时,需要包含uuid_io.hpp
  • 应用实例:
int testUuid()
{
    boost::uuids::string_generator sgen;
    boost::uuids::uuid u = sgen("01234567-89ab-cdef-0123-456789abcdef");
    string str = to_string(u); 

    cout << "u : " << u << endl;
    cout << "str : " << str << endl;

    using namespace boost::uuids::detail;
    sha1 sha;

    char *szMsg = "a short message";
    sha.process_byte(0x10);
    sha.process_bytes(szMsg, strlen(szMsg));
    sha.process_block(szMsg, szMsg + strlen(szMsg));

    unsigned int digest[5];
    sha.get_digest(digest);

    for (int i = 0; i < 5; ++i)
    {
        cout << hex << digest[i];
    }

    return 0;
}

你可能感兴趣的:(boost中的实用工具assign和uuid)