c++杂记

C++17中的std::string_view

string_view用来获取一个字符串的视图,字符串视图并不真正的创建或者拷贝字符串,而只是拥有一个字符串的查看功能。std::string_view比std::string的性能要高很多,因为每个std::string都独自拥有一份字符串的拷贝,而std::string_view只是记录了自己对应的字符串的指针和偏移位置。当我们在只是查看字符串的函数中可以直接使用std::string_view

boost::format类用法


    #include <iostream>
    #include <string>
    #include <boost/format.hpp>
     
    using namespace std;
    int _tmain(int argc, _TCHAR* argv[])
    {
        // 使用%序号%的方式给出指示符, 后面用%连接对应的数据。
        cout << boost::format("writing %1%,  x=%2% : %3%-th try") % "toto" % 40.23 % 50 << endl;
        // 输出:writing toto,  x=40.23 : 50-th try
       
        // 可以延迟使用,顺序不必一致
        boost::format fmter("%2% %1%");
        fmter % 36;
        fmter % 77;
        cout << fmter << endl;
        // 输出:77 36
       
        // 可重用
        fmter % 12;
        fmter % 24;
        cout << fmter << endl;
        // 输出:24 12
     
        // 可直接转成字符串
        std::string s = fmter.str();
        std::string s2 = str( boost::format("%2% %1% %2% %1%")%"World"%"Hello");
     
        cout << s << endl << s2 << endl;
        // 输出:
        // 24 12
        // Hello World Hello World
     
        // 可以使用printf指示符
        cout << boost::format("%3.1f - %.2f%%") % 10.0 % 12.5  << endl;
        // 输出:10.0 - 12.50%
     
        // printf指示符里使用N$指定使用第几个参数
        cout << boost::format("%2$3.1f - %1$.2f%%") % 10.0 % 12.5  << endl;
        // 输出:12.5 - 10.00%
     
        cin.get();
        return 0;
    }

C/C++ 可变参数列表 “…” 的使用

一、可变参数模板

#include <iostream>
using namespace std;
 
template <typename... T>
void func1(T... args)
{
	cout << sizeof...(T) << endl;
	cout << sizeof...(args) << endl;
}
 
void func2()
{
}
 
template <typename T, typename... U>
void func2(T v, U... args)
{
	cout << v << endl;
	func2(args...);
}
 
int main()
{
	func2(1, 2, 3);
 
	system("pause");
	return 0;
}

二、c库 “stdarg.h”

   #include <stdarg.h>
   #include <iostream>
   using namespace std;
    
   void func(const char *fmt, ...) 
   {
   	va_list ap;
   	va_start(ap, fmt);
    
   	auto a = va_arg(ap, int);
   	auto b = va_arg(ap, double);
   	auto c = va_arg(ap, char*);
    
   	cout << a << ", " << b << ", " << c << endl;
    
   	va_end(ap);
   }
    
   int main() 
   {
   	func("%d %f %s\n", 1, 2.0f, "hello world");
   	system("pause");
   	return 0;
   }

C++中 =defaule 和 =delete 使用

编译器为c++类生成的默认函数:

默认构造函数
默认析构函数
默认拷贝构造函数
默认赋值函数
移动构造函数
移动拷贝函数
class DataOnly {
public:
    DataOnly ()                  // default constructor
    ~DataOnly ()                 // destructor

    DataOnly (const DataOnly & rhs)              // copy constructor
    DataOnly & operator=(const DataOnly & rhs)    // copy assignment operator

    DataOnly (const DataOnly && rhs)         // C++11, move constructor
    DataOnly & operator=(DataOnly && rhs)    // C++11, move assignment operator
};
=delete
  1. 禁止使用编译器默认生成的函数

假如上面的几个函数中,不想使用其中某个,可以将其定义为private,或者使用=delete。

#include <iostream>
using namespace std;

class DataOnly {
public:
    DataOnly () {}
    ~DataOnly () {}

    DataOnly (const DataOnly & rhs) = delete; //禁止使用该函数
    DataOnly & operator=(const DataOnly & rhs) = delete; //禁止使用该函数

    DataOnly (const DataOnly && rhs) {}
    DataOnly & operator=(DataOnly && rhs) {}
};

int main(int argc, char *argv[]) {
    DataOnly data1;
    DataOnly data2(data1); // error: call to deleted constructor of 'DataOnly'
    DataOnly data3 = data1; // error: call to deleted constructor of 'DataOnly'
    return 0;
}

  1. delete 关键字可用于任何函数,不仅仅局限于类的成员函数
= defaule

在程序员重载了自己上面提到的C++编译器默认生成的函数之后,C++编译器将不在生成这些函数的默认形式。
但是C++允许我们使用=default来要求编译器生成一个默认函数,如:

struct Point {
    Point()=default;
    Point(int _x, int _y) : x(_x), y(_y) {}
    int x = 0;
    int y = 0;
}

https://www.cnblogs.com/lzjsky/archive/2011/05/05/2037327.html
https://blog.csdn.net/wh1312142954/article/details/90243669
https://blog.csdn.net/lmb1612977696/article/details/80035487

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值