c++http服务收到中文乱码
http发送的body
c++http服务收到的
先使用boost解析json
1234567boost::json::error_code ec;boost::json::parse_options opt; // all extensions default to offopt.allow_comments = true; // permit C and C++ style comments to appear in whitespaceopt.allow_trailing_commas = true; // allow an additional trailing comma in object and array element listsopt.allow_invalid_utf8 = true; // skip utf-8 validation of keys and strin ...
asio的http服务
代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778#include <string>#include <memory>#include <iostream>#include "boost/asio.hpp"class HttpConnect : public std::enable_shared_from_this<HttpConnect>{public: HttpConnect(boost::asio::io_context& io) : m_socket(io) {} ~HttpConnect(){} void RunOnce() { auto sphc = shar ...
c++17 std::variant
说明类模板 std::variant表示一个类型安全的联合体。 std::variant 的一个实例在任意时刻要么保有其一个可选类型之一的值,要么在错误情况下无值(此状态难以达成,见 valueless_by_exception )。与联合体在聚合初始化中的行为一致, 若 variant 保有某个对象类型T的值,则直接于 variant的对象表示中分配 T 的对象表示。不允许 variant分配额外的(动态)内存。variant 不容许保有引用、数组,或类型 void 。空 variant亦为病式(可用 std::variant<std::monostate> 代替)。variant 容许保有同一类型多于一次,而且可保有同一类型的不同 cv 限定版本。同联合体,默认构造的 variant保有其首个选项的值,除非该选项不是可默认构造的(该情况下 variant亦非可默认构造:能用辅助类 std::monostate 使这种 variant 可默认构造)。模板形参Types - 可存储于此 variant 中的类型。所有类型必须满足可析构 (Destructible ...
c++17 launder
代码1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374#include <new>#include <cstddef>#include <cassert>#include <iostream>struct X { const int n; // 注意: X 拥有 const 成员 int m;};struct Y { int z;};struct A { virtual int transmogrify();};struct B : A { int transmogrify() override { new(this) A; return 2; }};int A::transmogrif ...
c++17 to_chars、from_chars、if、结构化绑定
说明std::to_chars通过成功填充范围 [first, last) ,转换 value 为字符串,要求 [first, last) 是合法范围。1) 整数格式化函数: value 以给定基底 base 转换成数位的字符串(无冗余的前导零)。范围 10..35 (含上下限)中的数字被表示成小写字母 a..z 。若值小于零,则表示以负号起始。库提供所有有符号及无符号整数和 char 类型作为参数 value 类型的重载。2) bool 的重载被删除。 to_chars 拒绝 bool 类型参数,因为假如允许则结果会是 “0”/“1” 而非 “false”/“true” 。3) 如同用 std::sprintf 于默认( “C” )本地环境转换值为字符串。转换指定符是 f 或 e ,根据最短表示方式的要求选择(两者相当时优先选择 f ):字符串表示由小数点(若存在)前至少有一位,且用对应的 std::from_chars 分析该表示能准确恢复值的,最小数量的字符组成。若有数个这种表示,则选择到 value 的差最小者,用根据 std::round_to_nearest 的舍入解决任何 ...
c++17 apply、tuple
说明 以参数的元组调用可调用(Callable) 对象 。 元组不必是 std::tuple ,可以为任何支持 std::get 和 std::tuple_size 的类型所替代;特别是可以用 std::array 和 std::pair 。
代码1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071#include <iostream>#include <tuple>#include <utility>#include <array>int add(int first, int second) { return first + second; }template<typename T>T add_generic(T first, T second) { return first + se ...
c++17中的any、for_each、and_eq
描述类 any 描述用于任何类型的单个值的类型安全容器。
1) 类 any 的对象存储任何满足构造函数要求的类型的一个实例或为空,而这被称为 any 类对象的状态。存储的实例被称作所含对象。若两个状态均为空,或均为非空且其所含对象等价,则两个状态等价。2) 非成员 any_cast 函数提供对所含对象的类型安全访问。鼓励实现避免小对象的动态分配,但这种优化仅可以应用于 std::is_nothrow_move_constructible 对其返回 true 的类型。
代码12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364#include <any>#include <iostream>#include <bitset>#include <vector>#include <algorithm>int main(){ std::c ...
c++17遍历目录
代码12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364//#define _CRT_SECURE_NO_WARNINGS#include <ctime>#include <iostream>#include <filesystem>// vs下选择c++语言标准选择 ISO C++17 标准 (/std:c++17)// 使用此库可能要求额外的编译器/链接器选项。 // 9.1 前的 GNU 实现要求用 -lstdc++fs 链接,而 LLVM 9.0 前的 LLVM 实现要求用 -lc++fs 链接。int main(){ namespace fs = std::filesystem; auto testdir = fs::path("f:/testdir"); if (!fs::exists(testdir) ...
c++标准中的时间函数
说明1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950// Time manipulation// clock// returns raw processor clock time since the program is started // (function)// time // returns the current time of the system as time since epoch// (function)// difftime// computes the difference between times// (function)// timespec_get(C++17)// returns the calendar time based on a given time base// (function)// Format conversions// ctime// converts a std::time_t object to ...
c++获取网络收发字节数
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220 ...