【C++】POCO学习总结(十九):哈希、URL、UUID、配置文件、日志配置、动态库加载

本文主要是介绍【C++】POCO学习总结(十九):哈希、URL、UUID、配置文件、日志配置、动态库加载,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

【C++】郭老二博文之:C++目录

1、哈希

1.1 说明

std::map和std::set 的性能是:O(log n)
POCO哈希的性能比STL容器更好,大约快两;
POCO中对应std::map的是:Poco::HashMap;
POCO中对应std::set的是 Poco::HashSet;
使用方法、迭代器都和STL类似。

POCO哈希在执行插入或者删除操作时不会导致性能下降(当数据不足时不需要重新哈希)

HashMap 和 HashSet 使用线性哈希表LinearHashTable作为基础数据结构。
使用时还必须提供一个哈希函数:Poco/Hash.h中预定义了关于整数和std::string的函数

namespace Poco {
std::size_t hash(Int8 n);
std::size_t hash(UInt8 n);
std::size_t hash(Int16 n);
std::size_t hash(UInt16 n);
std::size_t hash(Int32 n);
std::size_t hash(UInt32 n);
std::size_t hash(Int64 n);
std::size_t hash(UInt64 n);
std::size_t hash(const std::string& str);
}
Poco::LinearHashTable<Key, Hash = Poco::Hash<Key>

1.2 示例

#include "Poco/LinearHashTable.h"
#include "Poco/HashMap.h"
#include <iterator>
#include <iostream>
using namespace Poco;
int main()
{const int N = 20;LinearHashTable<int, Hash<int> > ht;for (int i = 0; i < N; ++i)ht.insert(i);LinearHashTable<int, Hash<int> >::Iterator it = ht.begin();while (it != ht.end()){std::cout << "[" << *it << "]";++it;}
}

编译:

g++ hash.cpp -I ~/git/poco/install/include -L ~/git/poco/install/lib -lPocoFoundationd

输出

[0][17][2][19][4][5][6][7][8][9][10][11][12][13][14][15][16][1][18][3]

2、POCO::URI

2.1 说明

POCO提供了POCO::URI类,该类可用于构建和存储URI,并且解析、拆分URI。
URI结构:协议(scheme)、主机名(host)、用户(user)、端口号(port)、路径(path)、查询(query)、资源(Fragment)等
在这里插入图片描述
在这里插入图片描述

2.2 示例

#include "Poco/URI.h"
#include <iostream>
int main(int argc, char** argv)
{Poco::URI uri1("http://www.appinf.com:88/sample?example-query#frag");std::string scheme(uri1.getScheme()); // "http"std::string auth(uri1.getAuthority()); // "www.appinf.com:88"std::string host(uri1.getHost()); // "www.appinf.com"unsigned short port = uri1.getPort(); // 88std::string path(uri1.getPath()); // "/sample"std::string query(uri1.getQuery()); // "example-query"std::string frag(uri1.getFragment()); // "frag"std::string pathEtc(uri1.getPathEtc()); // "/sample?examplequery#frag"Poco::URI uri2;uri2.setScheme("https");uri2.setAuthority("www.appinf.com");uri2.setPath("/another sample");std::string s(uri2.toString()); // "https://www.appinf.com/another%20sample"std::string uri3("http://www.appinf.com");uri3.resolve("/poco/info/index.html");s = uri3.toString(); // "http://www.appinf.com/poco/info/index.html"uri3.resolve("support.html");s = uri3.toString(); // "http://www.appinf.com/poco/info/support.html"uri3.resolve("http://sourceforge.net/projects/poco");s = uri3.toString(); // "http://sourceforge.net/projects/poco"return 0;
}

3、UUID

3.1 说明

UUID(通用唯一标识符)是一种标识符,它在空间和时间上相对于所有UUID的空间都是唯一的。
Poco::UUID支持包括所有关系操作符在内的全值语义,和字符串之间进行转换。

3.2 示例

#include "Poco/UUID.h"
#include "Poco/UUIDGenerator.h"
#include <iostream>
using Poco::UUID;
using Poco::UUIDGenerator;
int main(int argc, char** argv)
{UUIDGenerator& generator = UUIDGenerator::defaultGenerator();UUID uuid1(generator.create()); // time basedUUID uuid2(generator.createRandom());UUID uuid3(generator.createFromName(UUID::uri(), "http://appinf.com");std::cout << uuid1.toString() << std::endl;std::cout << uuid2.toString() << std::endl;std::cout << uuid3.toString() << std::endl;return 0;
}

4、配置文件

4.1 说明

Poco::Util::AbstractConfiguration提供了一个公共接口,用于访问来自不同来源的配置信息。
配置设置基本上是键/值对,其中键和值都是字符串。
键具有层次结构,由以句点分隔的名称组成。
值可以转换为整数、双精度和布尔值。
一个可选的默认值可以在getter函数中指定。

4.2 用法

  • bool hasProperty(const std::string& key)
  • std::string getString(const std::string& key [, const std::string& default])
  • int getInt(const std::string& key [, int default])
  • getDouble()
  • getBool()
  • setString(),
  • setInt()
  • setDouble()
  • setBool()
  • keys()

4.3 Poco::Util::IniFileConfiguration ini配置文件

Poco::Util::IniFileConfiguration支持普通的旧INI格式文件,主要用于Windows。

  • 键名不区分大小写。
  • 从键和值中删除前导和尾随空格。
  • 只读

格式:

; comment
[MyApplication]
somePath = C:\test.dat
someValue = 123

解析:

using Poco::AutoPtr;
using Poco::Util::IniFileConfiguration;
AutoPtr<IniFileConfiguration> pConf(new IniFileConfiguration("test.ini"));
std::string path = pConf->getString("MyApplication.somePath");
int value = pConf->getInt("MyApplication.someValue");
value = pConf->getInt("myapplication.SomeValue");
value = pConf->getInt("myapplication.SomeOtherValue", 456);

4.4 Poco::Util::PropertyFileConfiguration 属性文件

格式:

# a comment
! another comment
key1 = value1
key2: 123
key3.longValue = this is a very \
long value
path = c:\\test.dat

解析:

using Poco::AutoPtr;
using Poco::Util::PropertyFileConfiguration;
AutoPtr<PropertyFileConfiguration> pConf;
pConf = new PropertyFileConfiguration("test.properties");
std::string key1 = pConf->getString("key1");
int value = pConf->getInt("key2");
std::string longVal = pConf->getString("key3.longValue");

4.5 Poco::Util::XMLConfiguration XML配置文件

格式:

<config><prop1>value1</prop1><prop2>123</prop2><prop3><prop4 attr="value3"/><prop4 attr="value4"/></prop3>
</config>

解析

using Poco::AutoPtr;
using Poco::Util::XMLConfiguration;
AutoPtr<XMLConfiguration> pConf(new XMLConfiguration("test.xml"));
std::string prop1 = pConf->getString("prop1"); 
int prop2 = pConf->getInt("prop2");
std::string prop3 = pConf->getString("prop3"); // ""
std::string prop4 = pConf->getString("prop3.prop4"); // ""
prop4 = pConf->getString("prop3.prop4[@attr]"); // "value3"
prop4 = pConf->getString("prop3.prop4[1][@attr]"); // "value4"

5、日志配置

5.1 说明

Poco::Util::LoggingConfigurator类使用来自Poco::Util::AbstractConfiguration的配置信息来设置和连接日志格式化、通道和记录器。
Poco::Util::Application自动初始化一个LoggingConfigurator及其配置。
所有用于日志记录的配置属性都是以“logging”为键值。

5.2 格式化配置

格式化配置以“logging.formatters”开头;
每个格式化都有一个内部名称,该名称仅用于配置目的,用于将格式化程序连接到通道。
该名称成为属性名称的一部分。其中class属性是必须的,它指定实现格式化程序的类。

logging.formatters.f1.class = PatternFormatter
logging.formatters.f1.pattern = %s: [%p] %t
logging.formatters.f1.times = UTC

5.3 通道配置

通道配置以“logging.channels”开头;class属性是必须的
“formatter”属性既可以用来引用已经定义的格式化,也可以用来指定“内联”格式化定义。在这两种情况下,当存在"formatter"属性时,通道将自动被"包装"在FormattingChannel对象

# External Formatter
logging.channels.c1.class = ConsoleChannel
logging.channels.c1.formatter = f1
# Inline Formatter
logging.channels.c2.class = FileChannel
logging.channels.c2.path = ${system.tempDir}/sample.log
logging.channels.c2.formatter.class = PatternFormatter
logging.channels.c2.formatter.pattern = %Y-%m-%d %H:%M:%S %s: [%p] %t
# Inline PatternFormatter
logging.channels.c3.class = ConsoleChannel
logging.channels.c3.pattern = %s: [%p] %t

5.4 日志记录器

日志记录器使用“logging.loggers”
与通道 channels 和格式化 formatters 一样,每个日志记录器都有一个内部名称,但是,该名称仅用于确保属性名称的唯一性。请注意,此名称与记录器的全名不同,后者用于在运行时访问记录器。
除了根记录器之外,每个记录器都有一个强制性的“name”属性,用于指定记录器的全名。

# External Channel
logging.loggers.root.channel = c1
logging.loggers.root.level = warning
# Inline Channel with PatternFormatter
logging.loggers.l1.name = logger1
logging.loggers.l1.channel.class = ConsoleChannel
logging.loggers.l1.channel.pattern = %s: [%p] %t
logging.loggers.l1.level = information
# SplitterChannel
logging.channels.splitter.class = SplitterChannel
logging.channels.splitter.channels = l1,l2
logging.loggers.l2.name = logger2
logging.loggers.l2.channel = splitter

6、动态库加载

6.1 说明

大多数现代平台都提供了在运行时以共享库(动态链接库)的形式加载程序模块的功能。
Windows提供了LoadLibrary()函数,大多数Unix平台都有dopen()。

6.2 用法

头文件: #include “Poco/SharedLibrary.h”
Poco::SharedLibrary是Poco与操作系统动态链接器/加载器的接口。
Poco::SharedLibrary提供了加载共享库、查找符号地址和卸载共享库的底层函数。

  • void load(const std::string& path):从给定的路径加载共享库
  • void unload():卸载共享库
  • bool hasSymbol(const std::string& name):如果库中包含具有给定名称的符号,则返回true
  • void* getSymbol(const std::string& name):返回给定名称的符号的地址。对于函数,这是函数的入口点。要调用函数,请强制转换为函数指针并通过它调用

6.3 示例

1)动态库:TestLibrary.cpp

#include <iostream>
#if defined(_WIN32)
#define LIBRARY_API __declspec(dllexport)
#else
#define LIBRARY_API
#endif
extern "C" void LIBRARY_API hello();
void hello()
{std::cout << "Hello, world!" << std::endl;
}

2)加载动态库:LibraryLoaderTest.cpp

#include "Poco/SharedLibrary.h"
using Poco::SharedLibrary;
typedef void (*HelloFunc)(); // function pointer type
int main(int argc, char** argv)
{std::string path("TestLibrary");path.append(SharedLibrary::suffix()); // adds ".dll" or ".so"SharedLibrary library(path); // will also load the libraryHelloFunc func = (HelloFunc) library.getSymbol("hello");func();library.unload();return 0;
}

6.4 Poco::ClassLoader 从共享库加载类

Poco::ClassLoader是Poco的高级接口,用于从共享库加载类。它非常适合实现典型的插件架构。
头文件: #include “Poco/ClassLoader.h”
Poco::ClassLoader所有类必须是公共基类的子类。Poco::ClassLoader是一个类模板,必须为基类实例化。

6.5 元对象

Manifest库维护一个包含在动态可加载类库中的所有类的列表。
它将这些信息作为元对象的集合进行管理。
MetaObject管理给定类的对象的生命周期。它用于创建类的实例,并删除它们。
作为一个特殊的特性,类库可以导出单例。

这篇关于【C++】POCO学习总结(十九):哈希、URL、UUID、配置文件、日志配置、动态库加载的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/510362

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

Zookeeper安装和配置说明

一、Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模式。 ■ 单机模式:Zookeeper只运行在一台服务器上,适合测试环境; ■ 伪集群模式:就是在一台物理机上运行多个Zookeeper 实例; ■ 集群模式:Zookeeper运行于一个集群上,适合生产环境,这个计算机集群被称为一个“集合体”(ensemble) Zookeeper通过复制来实现

CentOS7安装配置mysql5.7 tar免安装版

一、CentOS7.4系统自带mariadb # 查看系统自带的Mariadb[root@localhost~]# rpm -qa|grep mariadbmariadb-libs-5.5.44-2.el7.centos.x86_64# 卸载系统自带的Mariadb[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7

hadoop开启回收站配置

开启回收站功能,可以将删除的文件在不超时的情况下,恢复原数据,起到防止误删除、备份等作用。 开启回收站功能参数说明 (1)默认值fs.trash.interval = 0,0表示禁用回收站;其他值表示设置文件的存活时间。 (2)默认值fs.trash.checkpoint.interval = 0,检查回收站的间隔时间。如果该值为0,则该值设置和fs.trash.interval的参数值相等。

NameNode内存生产配置

Hadoop2.x 系列,配置 NameNode 内存 NameNode 内存默认 2000m ,如果服务器内存 4G , NameNode 内存可以配置 3g 。在 hadoop-env.sh 文件中配置如下。 HADOOP_NAMENODE_OPTS=-Xmx3072m Hadoop3.x 系列,配置 Nam

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

【C++ Primer Plus习题】13.4

大家好,这里是国中之林! ❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看← 问题: 解答: main.cpp #include <iostream>#include "port.h"int main() {Port p1;Port p2("Abc", "Bcc", 30);std::cout <<