C++ LeetCode刷题常用函数


前言

C++中的algorithm、numeric库中有几个常用的模板函数,以下将其归纳总结一下(swap,reverse,sort,unique,accumulate)。


1.swap()

template <class T> void swap ( T& a, T& b )
{
  T c(a); a=b; b=c;
}

上面是swap函数的定义,实际上c就相当于我们平时写的temp临时变量,但实际上该方法并不是一个高效率的方法,因为该函数涉及到了一次复制构造和两次复制,特别是在交换的变量所占空间特别大的时候,最好采用其他方法实现(比如通过异或实现交换)。
举例如下:

#include <iostream>
using namespace std;

int main() {
  int n, m;
  n = 0;
  m = 1;
  swap(n, m);
  cout << n << " " << m << endl; //output: 1 0
  return 0;
}

使用异或实现两个变量交换,需要注意两个变量不能指向同一个内存空间

#include <iostream>
using namespace std;

int main() {
  int n, m;
  n = 0;
  m = 1;
  n = n ^ m;
  m = n ^ m;
  n = n ^ m;
  cout << n << " " << m << endl; //output: 1 0
  return 0;
}

2.reverse()

template <class BidirectionalIterator>
  void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
  while ((first!=last)&&(first!=--last)) {
    std::iter_swap (first,last);
    ++first;
  }
}

reverse函数反转[first, last)区间的数据,first和last都是迭代器。
举例如下:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  vector<int> a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  reverse(a.begin(), a.end());
  for (int i = 0; i < a.size(); i++) {
    cout << a[i] << " "; // output: 10 9 8 7 6 5 4 3 2 1 
  }
  return 0;
}

3.sort()

template <class RandomAccessIterator, class Compare>
  void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

第三个参数comp可不写,不写第三个参数的话默认是升序排列。
如果想要降序排列:
第一种方法是sort之后再使用reverse。
第二种方法是将 greater() 添加到第三个参数中,注意greater函数是在funtional头文件中的,如果你要比较的对象是int,则尖括号中写int,如果不是则写你需要排序的元素的类型。
第三种方法则是自己写比较函数,特别是当你排序的元素是结构体或类的对象。
举例如下:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  vector<int> a = {1, 7, 10, 5, 2, 4, 6, 8, 9, 3};
  sort(a.begin(), a.end());
  for (int i = 0; i < a.size(); i++) {
    cout << a[i] << " "; // output: 1 2 3 4 5 6 7 8 9 10 
  }
  return 0;
}

参数cmp函数实现降序排列。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool cmp(int a, int b) {
  return a > b;
}
int main() {
  vector<int> a = {1, 7, 10, 5, 2, 4, 6, 8, 9, 3};
  sort(a.begin(), a.end(), cmp);
  for (int i = 0; i < a.size(); i++) {
    cout << a[i] << " "; // output: 10 9 8 7 6 5 4 3 2 1 
  }
  return 0;
}

4.unique()

从输入序列中“删除”所有相邻的重复元素,并将这些元素移到最后,返回一个迭代器指针,指针指向的正是最后那些重复元素的第一个元素


5.accumulate(first_iterator,last_iterator,求和的初始值)

对向量元素求和。

template <class InputIterator, class T>
   T accumulate (InputIterator first, InputIterator last, T init)
{
  while (first!=last) {
    init = init + *first;  // or: init=binary_op(init,*first) for the binary_op version
    ++first;
  }
  return init;
}

举例如下:

#include <iostream>
#include <vector>
#include <algorithm>
#include<numeric>

using namespace std;

int main() {
  vector<int> a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  cout << accumulate(a.begin(), a.end(), 0) << endl; // output: 55
  return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一些常用函数和数据结构在LeetCode刷题中的应用示例: 1. 栈(Stack):栈是一种后进先出(LIFO)的数据结构,常用于解决与括号匹配、逆波兰表达式等问题。 ```java import java.util.Stack; Stack<Integer> stack = new Stack<>(); stack.push(1); // 入栈 stack.push(2); stack.push(3); int top = stack.peek(); // 获取栈顶元素,但不删除 int pop = stack.pop(); // 弹出栈顶元素 boolean isEmpty = stack.isEmpty(); // 判断栈是否为空 ``` 2. 队列(Queue):队列是一种先进先出(FIFO)的数据结构,常用于解决与广度优先搜索(BFS)相关的问题。 ```java import java.util.Queue; import java.util.LinkedList; Queue<Integer> queue = new LinkedList<>(); queue.offer(1); // 入队 queue.offer(2); queue.offer(3); int front = queue.peek(); // 获取队首元素,但不删除 int poll = queue.poll(); // 弹出队首元素 boolean isEmpty = queue.isEmpty(); // 判断队列是否为空 ``` 3. 堆(Heap):堆是一种特殊的树形数据结构,常用于解决与优先队列相关的问题。 ```java import java.util.PriorityQueue; PriorityQueue<Integer> minHeap = new PriorityQueue<>(); // 小顶堆 minHeap.offer(3); // 入堆 minHeap.offer(1); minHeap.offer(2); int min = minHeap.peek(); // 获取堆顶元素,但不删除 int pollMin = minHeap.poll(); // 弹出堆顶元素 boolean isEmpty = minHeap.isEmpty(); // 判断堆是否为空 ``` 4. 位运算(Bit Manipulation):位运算是对二进制数进行操作的技术,常用于解决与位操作相关的问题,如位与、位或、位异或等。 ```java int a = 5; // 二进制表示为 101 int b = 3; // 二进制表示为 011 int andResult = a & b; // 位与运算,结果为 001,即 1 int orResult = a | b; // 位或运算,结果为 111,即 7 int xorResult = a ^ b; // 位异或运算,结果为 110,即 6 int complement = ~a; // 取反运算,结果为 11111111111111111111111111111010,即 -6 int leftShift = a << 1; // 左移运算,结果为 1010,即 10 int rightShift = a >> 1; // 右移运算,结果为 10,即 2 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值