IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++进阶:#define与typedef、函数返回数组指针怎么写、greater<>()、less<>()、substr()用法、reverse函数、位运算 -> 正文阅读

[C++知识库]C++进阶:#define与typedef、函数返回数组指针怎么写、greater<>()、less<>()、substr()用法、reverse函数、位运算

1 #define与typedef

define是直接替换

#define a(x, y) x + y
cout << a(3, 5) * a(3, 5) << endl;
// 3 + 5 * 3 + 5 = 23

#define a(x, y) (x+y)
cout << a(3, 5) * a(3, 5) << endl;
// (3 + 5) * (3 + 5) = 64

typedef:类型别名

typedef double* p;
cout << typeid(ptr).name() << endl; / p是double *类型
typedefconst是修饰p的,而非修饰p所指的对象。              
const p ptr = 0; / ptr是double * const类型,是指向double的常量指针。
const p *a; / a是一个指针,所指对象是“指向double的常量指针”

2 函数返回数组指针怎么写

①使用类型别名

typedef int arrT[10]; /array是一个类型别名,表示的类型是含有10个整数的数组,arrT相当于int [10]
using arrT = int[10]; /arrT的等价声明,同第一条
arrT* func(int i);  /func返回一个指向含有10个整数的数组的指针

②普通表示法

int arr[10];
int (*p)[10] = &arr; /p是指向含有10个整数的数组的指针
int (*(func (int i)) [10]; /函数func的返回类型是“指向含有10个整数的数组的指针”
Primer P205

③尾置返回类型 C++11
任何函数定义都可以使用尾置类型返回,但是这种形式对于返回类型比较复杂的函数最有效,比如返回类型是数组的指针或引用。 注意要在原位置放置auto

auto func(int i) -> int (*) [10];

④使用decltype

int odd[] = {1,3,5,7,9};
decltype(odd) *func(int i); /decltype的结果是一个长度为5int数组,加一个*表示该函数返回值是“指向含有5个整数的数组的指针”

3 greater<>()、less<>()

对于priority_queue,更优先的元素在前端,默认情况下与less<>()相同,更大的数更优先。
但如果第三个参数为greater<>() 或者重载了()、或者重载了 < 、更小的的数更优先。

方法1:
// 按pair的第二个元素降序排列
 static bool cmp(pair<int,int> a, pair<int, int> b){
    return a.second > b.second;
}
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(&cmp)> q(cmp);

方法2:
 struct cmp1{
    bool operator()(pair<int,int> a, pair<int, int> b){
        return a.second > b.second;
    }
};
priority_queue<pair<int,int> , vector<pair<int,int>>, cmp1> q;



priority_queue<int> q;//默认是从大到小

priority_queue<int, vector<int> ,less<int> >q;//从大到小排序

priority_queue<int, vector<int>, greater<int> >q;//从小到大排序
————————————————
版权声明:本文为CSDN博主「页图」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_32159463/article/details/114273899
#include <iostream>
#include <algorithm>
#include <functional>
#include <queue>
using namespace std;

int main() {
	int a[] = { 3, 5, 6, 2, 1, 4 };
	sort(a, a + 6);
	cout << "sort默认:";
	for (auto e : a)
		cout << e << "  ";
	cout << endl;

	sort(a, a + 6, greater<int>());
	cout << "sort_greater:";
	for (auto e : a)
		cout << e << "  ";
	cout << endl;

	sort(a, a + 6, less<int>());
	cout << "sort_less:";
	for (auto e : a)
		cout << e << "  ";
	cout << endl;

	priority_queue<int> mypriqueue;
	int b[] = { 3, 5, 6, 2, 1, 4 };
	for (auto e : b) {
		mypriqueue.push(e);
	}
	cout << "优先级队列默认:";
	while (!mypriqueue.empty()) {
		cout << mypriqueue.top() << "  ";
		mypriqueue.pop();
	}
	cout << endl;


	priority_queue<int, vector<int>, greater<int>> q1;
	for (auto e : b)
		q1.push(e);
	cout << "优先级队列_greater:";
	while (!q1.empty()) {
		cout <<q1.top() << "  ";
		q1.pop();
	}
	cout << endl;

	priority_queue<int, vector<int>, less<int>> q2;
	for (auto e : b)
		q2.push(e);
	cout << "优先级队列_less:";
	while (!q2.empty()) {
		cout << q2.top() << "  ";
		q2.pop();
	}
	return 0;
}
输出:
sort默认:1  2  3  4  5  6
sort_greater:6  5  4  3  2  1
sort_less:1  2  3  4  5  6
优先级队列默认:6  5  4  3  2  1
优先级队列_greater:1  2  3  4  5  6
优先级队列_less:6  5  4  3  2  1

4 substr()用法

  1. 形式:s.substr(pos, n)

  2. 解释:返回一个string,包含s中从pos开始的n个字符的拷贝(pos的默认值是0,n的默认值是s.size() - pos,即不加参数会默认拷贝整个s)

  3. 补充:若pos的值超过了string的大小,则substr函数会抛出一个out_of_range异常;若pos+n的值超过了string的大小,则substr会调整n的值,只拷贝到string的末尾

5 reverse

包含在< alogotithm > 中
reverse函数用于反转在[first,last)范围内的顺序(包括first指向的元素,不包括last指向的元素),reverse函数没有返回值

6 位运算

位运算规则及常用实例
注意&运算的优先级比 << >> 低

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-09-29 10:03:43  更:2021-09-29 10:04:28 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/8 8:09:44-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码