彩票注册开户

  • <tr id='4wfwht'><strong id='4wfwht'></strong><small id='4wfwht'></small><button id='4wfwht'></button><li id='4wfwht'><noscript id='4wfwht'><big id='4wfwht'></big><dt id='4wfwht'></dt></noscript></li></tr><ol id='4wfwht'><option id='4wfwht'><table id='4wfwht'><blockquote id='4wfwht'><tbody id='4wfwht'></tbody></blockquote></table></option></ol><u id='4wfwht'></u><kbd id='4wfwht'><kbd id='4wfwht'></kbd></kbd>

    <code id='4wfwht'><strong id='4wfwht'></strong></code>

    <fieldset id='4wfwht'></fieldset>
          <span id='4wfwht'></span>

              <ins id='4wfwht'></ins>
              <acronym id='4wfwht'><em id='4wfwht'></em><td id='4wfwht'><div id='4wfwht'></div></td></acronym><address id='4wfwht'><big id='4wfwht'><big id='4wfwht'></big><legend id='4wfwht'></legend></big></address>

              <i id='4wfwht'><div id='4wfwht'><ins id='4wfwht'></ins></div></i>
              <i id='4wfwht'></i>
            1. <dl id='4wfwht'></dl>
              1. <blockquote id='4wfwht'><q id='4wfwht'><noscript id='4wfwht'></noscript><dt id='4wfwht'></dt></q></blockquote><noframes id='4wfwht'><i id='4wfwht'></i>
                您的位置: 首页 > 科学 > 释疑解惑

                axios跟ajax请求的结果不一∴致 数据请求中AjaxFetch以及Axios的区别

                作者:菜叶 时间:2023-05-18

                简介:数据请求中AjaxFetch以及Axios的区别作↓者:程序喵大家应该都知道C17引入了variant,这篇文章我们来研究下它究竟有啥用

                【菜叶百科解读】

                大家应该都知道C 17引入了variant,这篇文章我们来研究下它究竟有啥用。

                本期目录

                variant是什么?

                为什么要引ω 入variant?

                如何确定variant中当前存放的数据类型?

                variant为什么要搭配monostate?

                如何用variant实现多态?

                variant这货类似∩于union,可以存放※多种类型的数据,但任何时刻最多只能存放其中一种类型。

                这里大家可能有○些疑问,既然有了union,那为啥还要▅引入variant呢?

                那肯定是因为union有缺点呗。

                看这段union的基本用⊙法:

                复制union MyUnion { int a; float b; double c; }; void test_simple_union() { MyUnion u; u.a = 1; std::cout << u.a << "\n"; u.b = 1.32f; std::cout << u.b << "\n"; u.c = 2.32; std::cout << u.c << "\n"; }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.

                union貌似也只能这么使用,没有其【他方法。

                这里有一个很重要的点,我没法获取当前存储的→数据是什么类型,比如我当前存储的是float,但是却按int方式获取,这不就坏事了吗」。

                再看一段代码:

                复制struct A { A() = default; A(int aa) : a{aa} { std::cout << "A() \n"; } ~A() { std::cout << "~A() \n"; } int a; }; struct B { B() = default; B(float bb) : b{bb} { std::cout << "B() \n"; } ~B() { std::cout << "~B() \n"; } float b; }; union MyStructUnion { A a; B b; /** * @brief 在◤析构函数中我要做什么?不知道当前类型究竟是A还是B * 那调用 a.~A() 还是 b.~B() ? */ ~MyStructUnion() { std::cout << "~MyStructUnion() \n"; } }; /** * @brief 需要手动】调用析构函数 * */ void test_struct_union() { MyStructUnion u; new (&u.a) A(1); std::cout << u.a.a << "\n"; u.a.~A(); u.b = B(2.3f); std::cout << u.b.b << "\n"; u.b.~B(); }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.

                这里可以看到,union无法自动处理构造和析构等逻▆辑,它需要用户手动调用相关函数才行,这就导致使用它union存储自定义类型时特别麻烦。

                所以,variant诞生:

                复制struct C { C() = default; C(std::string cc) : c{cc} { std::cout << "C() \n"; } ~C() { std::cout << "~C() \n"; } std::string c; }; /** * @brief 使用variant完全不需要手动调用△构造和析构函数,它会自动处理好所有逻辑,非常方便 * */ void test_variant() { std::variant<std::monostate, A, C> u; ///< 下面很快就ㄨ会介绍monostate u = 1; std::cout << std::get<A>(u).a << "\n"; u = std::string("dsd"); std::cout << std::get<C>(u).c << "\n"; }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.

                使用variant完全不需要手动调用△构造和析构函数,它会自动处理好所有逻辑,非常方便。

                这里还遗留个问题,即如何判断variant内部当前存储的数□ 据是什么类型?别着急,后面会介绍。

                在这之前还需要介绍个知识点:monostate。

                首先,普通的variant使用方法如下:

                复制void test_variant() { std::variant<int, float> var; var = 12; std::cout << std::get<int>(var) << "\n"; var = 12.1f; std::cout << std::get<float>(var) << "\n"; }1.2.3.4.5.6.7.

                这也◆是常规的variant使用方法。那我如果存储个自定义类型呢?

                复制struct S { S(int i) : value{i} {} int value; }; void test_monostate2() { ///< 编译失败,S如果没◎有构造函数,需要加monostate std::variant<S> var; var = 12; std::cout << std::get<S>(var).value << "\n"; }1.2.3.4.5.6.7.8.9.10.11.

                这里会编译失败,因为S没有无参默认构造函数,无法默认直接声明,所以这里需要加¤个monostate,表示默认情况下它的存储类型就是monostate。

                然后可以这样使用:

                复制struct S { S(int i) : value{i} {} int value; }; void test_monostate() { std::variant<std::monostate, S> var; var = 12; std::cout << std::get<S>(var).value << "\n"; }1.2.3.4.5.6.7.8.9.10.

                那如何获取variant内部存储的类型卐呢?

                其实variant有一个index()方法可以做到。

                看这段代々码:

                复制void test_index() { std::variant<std::monostate, int, float, std::string> var; ///< 默认index是0 var = 1; std::cout << var.index() << "\n"; ///< 1 var = 2.90f; std::cout << var.index() << "\n"; ///< 2 var = std::string("hello world"); std::cout << var.index() << "\n"; ///< 3 }1.2.3.4.5.6.7.8.9.

                在定义variant结束后,我们就会知道内部类ぷ型的index,然后在运行时我们就可以动态的获取当前var的index,进而确№定内部数据的类型。

                难道我们每次都要手动记录下variant内部数据◥类型的index吗?如果将来有一天我们要在中间新增数据类型,岂不是之前建立∩的index都错乱了。

                这里可以使用可变参数模板 模板元编▼程的小技巧,看下面这段代码:

                #p#分页标题#e#

                复制template <typename T, typename> struct get_index; template <size_t I, typename... Ts> struct get_index_impl {}; template <size_t I, typename T, typename... Ts> struct get_index_impl<I, T, T, Ts...> : std::integral_constant<size_t, I> {}; template <size_t I, typename T, typename U, typename... Ts> struct get_index_impl<I, T, U, Ts...> : get_index_impl<I 1, T, Ts...> {}; template <typename T, typename... Ts> struct get_index<T, std::variant<Ts...>> : get_index_impl<0, T, Ts...> {}; template <typename T, typename... Ts> constexpr auto get_index_v = get_index<T, Ts...>::value; using variant_t = std::variant<std::monostate, int, float, std::string>; constexpr static auto kPlaceholderIndex = get_index_v<std::monostate, variant_t>; constexpr static auto kIntIndex = get_index_v<int, variant_t>; constexpr static auto kFloatIndex = get_index_v<float, variant_t>; constexpr static auto kStringIndex = get_index_v<std::string, variant_t>;1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.

                通过get_index_v,我就可以知道≡数据类型在variant中的index,以后即使有改动也不需要担心,它都会自动处★理。再贴一段它的测试代码:

                复制void test_using_index() { std::cout << "kPlaceholderIndex " << kPlaceholderIndex << "\n"; std::cout << "kIntIndex " << kIntIndex << "\n"; std::cout << "kFloatIndex " << kFloatIndex << "\n"; std::cout << "kStringIndex " << kStringIndex << "\n"; auto custom_visitor = [](const auto& value) { switch (value.index()) { case kPlaceholderIndex: std::cout << "placehodler value " << "\n"; break; case kIntIndex: std::cout << "int value " << std::get<int>(value) << "\n"; break; case kFloatIndex: std::cout << "float value " << std::get<float>(value) << "\n"; break; case kStringIndex: std::cout << "string value " << std::get<std::string>(value) << "\n"; break; } }; variant_t var; custom_visitor(var); var = 1; custom_visitor(var); var = 2.90f; custom_visitor(var); var = std::string("hello world"); custom_visitor(var); var = std::string("hello type"); } int main() { test_using_index(); }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.

                结果在这:

                复制kPlaceholderIndex 0 kIntIndex 1 kFloatIndex 2 kStringIndex 3 placehodler value int value 1 float value 2.9 string value hello world1.2.3.4.5.6.7.8.

                是不←是很方便?

                其实上面的代码,个人认为它也是一种多态,尽管它就是一个普通的switch-case,然而,我们可以使】用std::visit稍微改装一下。

                那std::visit怎么用?看№这段代码:

                复制struct Visitor { void operator()(int i) const { std::cout << "int " << i << "\n"; } void operator()(float f) const { std::cout << "float " << f << "\n"; } void operator()(std::string s) const { std::cout << "string " << s << "\n"; } }; void test_visitor_functor() { std::variant<int, float, std::string> var; var = 1; std::visit(Visitor(), var); var = 2.90f; std::visit(Visitor(), var); var = std::string("hello world"); std::visit(Visitor(), var); } // 输出 int 1 float 2.9 string hello world1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.

                visit内部会自动判断当前variant内部存储的类型,进而∑触发不同的行为。

                上面是使用仿函数搭配的visit,其实使用lambda表达式更方↓便:

                复制void test_visitor_lambda() { std::variant<int, float, std::string> var; var = 1; std::visit([](const auto& value) { std::cout << "value " << value << "\n"; }, var); var = 2.90f; std::visit([](const auto& value) { std::cout << "value " << value << "\n"; }, var); var = std::string("hello world"); std::visit([](const auto& value) { std::cout << "value " << value << "\n"; }, var); var = std::string("hello type"); std::visit( [](const auto& value) { using T = std::decay_t<decltype(value)>; if constexpr (std::is_same_v<T, int>) { std::cout << "int value " << value << "\n"; } else if constexpr (std::is_same_v<T, float>) { std::cout << "float value " << value << "\n"; } else if constexpr (std::is_same_v<T, std::string>) { std::cout << "string value " << value << "\n"; } }, var); } // 输出 value 1 value 2.9 value hello world string value hello type1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.

                看到这里大家应该也悟到了,可以使用std::visit搭配variant来实现多态。

                下面是我写的几个variant的多态√示例:

                #p#分页标题#e#

                复制struct A { void func() const { std::cout << "func A \n"; } }; struct B { void func() const { std::cout << "func B \n"; } }; struct CallFunc { void operator()(const A& a) { a.func(); } void operator()(const B& b) { b.func(); } }; void test_no_param_polymorphism() { std::variant<A, B> var; var = A(); std::visit(CallFunc{}, var); var = B(); std::visit(CallFunc{}, var); }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.

                上面的是没有参数的多态,那如果想为函数添加一些』参数怎么办?

                可以利用仿函数中的成员变量,即:

                复制struct C { void func(int value) const { std::cout << "func C " << value << "\n"; } }; struct D { void func(int value) const { std::cout << "func D " << value << "\n"; } }; struct CallFuncParam { void operator()(const C& c) { c.func(value); } void operator()(const D& d) { d.func(value); } int value; }; void test_param_polymorphism() { std::variant<C, D> var; var = C(); std::visit(CallFuncParam{1}, var); var = D(); std::visit(CallFuncParam{2}, var); }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.

                或者lambda表达式的捕获●方式,即:

                复制void test_param_lambda_polymorphism() { std::variant<C, D> var; int value = 1; auto caller = [&value](const auto& v) { v.func(value); }; std::visit(caller, var); value = 2; std::visit(caller, var); }1.2.3.4.5.6.7.8.

                到这里已经介绍了variant实现多态★的完整方案。

                认为继承是个洪水猛兽的朋友,其实也可以々考虑variant来实现多态的行为哈。

                那■同样是实现多态,是用继承好呢,还是用variant好呢?可∮以看这个图:

                axios跟ajax请求的结果不一♀致(数据请求中AjaxFetch以及Axios的区别)(1)

                图片来源☆于这个链接:。大家感兴趣的可以直接移步哈≡≡。

                另外大家应该也比较感兴趣variant是如何实现的。关◥于如何实现variant,我找到了这篇文章,写的很♂不错,大家可以看看:https://www.cnblogs.com/qicosmos/p/3416432.html

                下面是本文参考链≡接:

                ?h??ttps://www.cppstories.com/2020/04/variant-virtual-polymorphism.html/

                https://stackoverflow.com/questions/52296889/what-are-the-advantages-of-using-stdvariant-as-opposed-to-traditional-polymorp

                https://www.cppstories.com/2018/06/variant/

                ?

                打完收工。

                完整代码见:

                https://github.com/chengxumiaodaren/cpp-learning/tree/master/src/variant

                声明:本文内容仅代表作者个人观点,与本站立场无关。如有内容侵犯您的⊙合法权益,请及时与我们联系,我们将第一时间》安排处理

                相关推荐
                热门精选
                返回首页版权声明网站地图返回顶部

                本站为非赢利性站点,为书友提供一个分享与交流的☉平台。本站所收录的作品、社区话题、用户评论、用户上传内容或图片等均属用户个人行为。如前述内容侵害您的权益,欢迎举报投Ψ诉,一经核实,立即删除,本站不承担任何责任

                2022 菜叶科技 版权所有

                鄂ICP备17021050号-10