【C++14算法】exchange和quoted


前言

C++是一种功能强大的编程语言,提供了丰富的标准库来支持各种编程任务。在C++14中引入的新特性中,我们介绍两个有用的算法:std::exchange和std::quoted。


一、exchange函数

1.1 exchange函数简介:

exchange是一个模板函数,用于将给定的值替换一个对象,并返回该对象的旧值。它的作用在于提供了一种简洁且线程安全的方式来更新对象的值。

1.2 函数原型

template< class T, class U = T >
T exchange( T& obj, U&& new_value );

在这里插入图片描述

1.3 示例代码

下面是一些使用std::exchange的示例代码:

示例1:替换整数对象

#include <iostream>
#include <utility>

int main() {
    int value = 42;
    int new_value = 100;
    
    int old_value = std::exchange(value, new_value);
    
    std::cout << "Old value: " << old_value << std::endl;
    std::cout << "New value: " << value << std::endl;
    
    return 0;
}

在这里插入图片描述

输出:

Old value: 42
New value: 100

示例2:替换字符串对象

#include <iostream>
#include <utility>
#include <string>

int main() {
    std::string name = "Alice";
    std::string new_name = "Bob";
    
    std::string old_name = std::exchange(name, new_name);
    
    std::cout << "Old name: " << old_name << std::endl;
    std::cout << "New name: " << name << std::endl;
    
    return 0;
}

在这里插入图片描述

输出:

Old name: Alice
New name: Bob

示例3:替换自定义类型对象

#include <iostream>
#include <utility>

struct Point {
    int x;
    int y;
};

int main() {
    Point p1{10, 20};
    Point p2{30, 40};
    
    Point old_point = std::exchange(p1, p2);
    
    std::cout << "Old point: (" << old_point.x << ", " << old_point.y << ")" << std::endl;
    std::cout << "New point: (" << p1.x << ", " << p1.y << ")" << std::endl;
    
    return 0;
}

在这里插入图片描述

输出:

Old point: (10, 20)
New point: (30, 40)

示例4:替换指针对象

#include <iostream>
#include <utility>

int main() {
    int* ptr1 = new int(42);
    int* ptr2 = new int(100);
    
    int* old_ptr = std::exchange(ptr1, ptr2);
    
    std::cout << "Old pointer value: " << *old_ptr << std::endl;
    std::cout << "New pointer value: " << *ptr1 << std::endl;
    
    delete old_ptr;
    delete ptr1;
    delete ptr2;
    
    return 0;
}

在这里插入图片描述

输出:
Old pointer value: 42
New pointer value: 100

二、quoted函数

2.1 简介:

std::quoted是一个库函数,在C++11中引入的,用于将字符串括起来,以便后续输出时可以包含引号。它提供了一种简单的方式来格式化输出字符串。

2.2 示例代码:

下面是一个使用std::quoted的示例代码:

#include <iostream>
#include <iomanip>
#include <string>

int main() {
    std::string name = "John Doe";
    int age = 30;
    
    std::cout << "Name: " << std::quoted(name) << std::endl;
    std::cout << "Age: " << age << std::endl;
    
    return 0;
}

在这里插入图片描述

输出:

Name: “John Doe”
Age: 30

总结

通过本文,我们了解了两个有用的C++14算法:std::exchange和std::quoted。

首先,我们学习了std::exchange函数,它提供了一种简洁且线程安全的方式来交换对象的值,并返回旧值。我们看到了它的函数原型和几个示例代码,包括替换整数、字符串、自定义类型和指针对象。

其次,我们学习了std::quoted函数,它将字符串括起来,以便在输出时包含引号。这可以提供更好的可读性,并在格式化输出时非常有用。我们通过一个示例代码演示了如何使用std::quoted格式化输出字符串。

这两个算法在C++编程中都非常有用,可以提高代码的可读性和效率。它们是C++14标准库的重要组成部分,值得开发者们深入了解和使用。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
JSON++ Build Status Introduction JSON++ is a light-weight JSON parser, writer and reader written in C++. JSON++ can also convert JSON documents into lossless XML documents. Contributors http://github.com/hjiang http://github.com/elanthis http://github.com/r-lyeh If you've made substantial contribution, please add your link here. Why another JSON parser? Perhaps because web service clients are usually written in dynamic languages these days, none of the existing C++ JSON parsers fitted my needs very well, so I wrote one that I used in another project. My goals for JSON++ were: Efficient in both memory and speed. No third party dependencies. JSON++ only depends on the standard C++ library. Cross platform. Robust. Small and convenient API. Most of the time, you only need to call one function and two function templates. Easy to integrate. JSON++ only has one source file and one header file. Just compile the source file and link with your program. Able to construct documents dynamically. JSON writer: write documents in JSON format. Other contributors have sinced added more functionalities: XML writer: convert documents to JSONx format. See http://goo.gl/I3cxs for details. XML writer: convert documents to JXML format. See https://github.com/r-lyeh/JXML for details. XML writer: convert documents to JXMLex format. See https://github.com/r-lyeh/JXMLex for details. XML writer: convert documents to tagged XML format. See https://github.com/hjiang/jsonxx/issues/12 for details. Compiler version You need a modern C++ compiler. For older compilers, please try legacy branch. Configuration Strict/permissive parsing JSONxx can parse JSON documents both in strict or permissive mode. When jsonxx::Settings::Parser is set to Strict, JSONxx parser will accept: Fully conformant JSON documents only. When jsonxx::Settings::Parser is set to Permissive, JSONxx parser will accept: Fully conformant JSON documents Ending commas in arrays and objects: { "array": [0,1,2,], } Single quoted strings: ['hello', "world"] C++ style comments: { "width": 320, "height": 240 } //Picture details Default value is Permissive. When jsonxx::Settings::UnquotedKeys is set to Enabled, JSONxx parser will accept: Unquoted keys: {name: "world"} Default value is Disabled. Assertions JSONxx uses internally JSONXX_ASSERT(...) macro that works both in debug and release mode. Set jsonxx::Settings::Assertions value to Disabled to disable assertions. Default value is Enabled. Usage The following snippets are from one of the unit tests. They are quite self-descriptive. using namespace std; using namespace jsonxx; string teststr( "{" " \"foo\" : 1," " \"bar\" : false," " \"person\" : {\"name\" : \"GWB\", \"age\" : 60,}," " \"data\": [\"abcd\", 42]," "}" ); // Parse string or stream Object o; assert(o.parse(teststr)); // Validation. Checking for JSON types and values as well assert(1 == o.get<Number>("foo")); assert(o.has<Boolean>("bar")); assert(o.has<Object>("person")); assert(o.get<Object>("person").has<Number>("age")); assert(!o.get<Object>("person").has<Boolean>("old")); assert(o.get<Object>("person").get<Boolean>("old", false)); assert(o.has<Array>("data")); assert(o.get<Array>("data").get<Number>(1) == 42); assert(o.get<Array>("data").get<String>(0) == "abcd"); assert(o.get<Array>("data").get<String>(2, "hello") == "hello"); assert(!o.has<Number>("data")); cout << o.json() << endl; // JSON output cout << o.xml(JSONx) << endl; // JSON to XML conversion (JSONx subtype) cout << o.xml(JXML) << endl; // JSON to XML conversion (JXML subtype) cout << o.xml(JXMLex) << endl; // JSON to XML conversion (JXMLex subtype) // Generate JSON document dynamically using namespace std; using namespace jsonxx; Array a; a << 123; a << "hello world"; a << 3.1415; a << 99.95f; a << 'h'; a << Object("key", "value"); Object o; o << "key1" << "value"; o << "key2" << 123; o << "key3" << a; cout << o.json() << endl; To do Custom JSON comments (C style /**/) when permissive parsing is enabled.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人才程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值