各种使用时新晋语法函数

new int[m]

例:p[n]为struct,有*time,p[n].time=new int[m],time变成int 数组,有m项

auto

for(auto t:res)cout<<t<<’ ‘;
auto res=get_di(X);【vector get_di(int n)】

for(int i=n-1;~i;i–)

在for循环中,i的作用是判断i是否为-1。因为-1的二进制表示是全1,所以(-1)就是全0,也就是0。所以当i等于-1时,~i就为0,循环就会终止。这样可以避免使用==或!=运算符来比较i和-1。

const double eps=1e-6;

判断浮点数是否为零或者小于零时由于浮点数特性需要判断它是否小于一个很小的数

struct结构体内比较格式

1
2
3
4
5
6
struct node{
int l,r;
bool operator<(const node &w)const{
return r<w.r;
}
}

* &的传递区别

vector传递 高精度计算

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
vector<int> mul(vector<int> &A, int b)
{
vector<int> C;
int t = 0;
for (int i = 0; i < A.size() || t; i ++ )
{
if (i < A.size()) t += A[i] * b;
C.push_back(t % 10);
t /= 10;
}
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
int main()
{
string a;
int b;
cin >> a >> b;
vector<int> A;
for (int i = a.size() - 1; i >= 0; i -- ) A.push_back(a[i] - '0');
auto C = mul(A, b);
for (int i = C.size() - 1; i >= 0; i -- ) printf("%d", C[i]);
return 0;
}

*lower_bound(stk.begin(), stk.end(), arr[i]) = arr[i]

它返回一个指向范围 [first, last) 中第一个不小于 val 的元素的迭代器
可以替代二分查找

priority_queue<int,vector,greater> heap;

优先队列是一种特殊的队列,它的元素被赋予优先级,当访问元素时,具有最高级优先级的元素先被访问12。在C++中,priority_queue是一个容器适配器,它提供了常数时间查找默认情况下最大(或最小)元素的功能,但以对数时间为代价进行插入和提取。
empty() 如果队列为空返回真

    pop()       删除队顶元素

    push()     入队一个元素

    size()      返回优先队列中拥有的元素个数

    top()        返回优先队列队顶元素

默认情况下,priority_queue是一个大根堆,这意味着根节点的值大于或等于子节点的值,
如果想要使用小根堆,可以将priority_queue的第三个参数设置为greater

大根堆是一种完全二叉树,其中所有父节点的值都比左右孩子的值大。小根堆与大根堆相反,其中所有父节点的值都比左右孩子的值小。
大根堆例如:
10
/
8 7
/ \ /
6 4 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
是默认的大根堆实现,top()是当前优先队列的最大值

#include <iostream>
#include <queue>
using namespace std;
int my_array[10] = {3,5,6,2,1,-8,10,4,-7,-6};
int main()
{
priority_queue<int> q;

for (int i=0;i<10;i++)
{
q.push(my_array[i]);
}

for (int i = 0; i < 10; i++)
{
cout << "order: " << q.top() << endl;
q.pop();
}

小根堆例如
1
/
2 3
/ \ /
4 5 6

cout<<fixed<<setprecison(15)<<dp[a]

精确输出小数点后几位

while (cin >> a >> b >> c, a || b || c)w[a][b] = c;

用于输入多组数据且全为0时表示结束的输入

lower_bound()、upper_bound()、equal_range() 以及 binary_search()

lower_bound()

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
/*
在 [first, last) 区域内查找不小于 val 的元素
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
const T& val);
//在 [first, last) 区域内查找第一个不符合 comp 规则的元素
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
const T& val, Compare comp);
*/

#include <iostream> // std::cout
#include <algorithm> // std::lower_bound
#include <vector> // std::vector
using namespace std;
//以普通函数的方式定义查找规则
bool mycomp(int i,int j) { return i>j; }

//以函数对象的形式定义查找规则
class mycomp2 {
public:
bool operator()(const int& i, const int& j) {
return i>j;
}
};

int main() {
int a[5] = { 1,2,3,4,5 };
//从 a 数组中找到第一个不小于 3 的元素
int *p = lower_bound(a, a + 5, 3);
cout << "*p = " << *p << endl;

vector<int> myvector{ 4,5,3,1,2 };
//根据 mycomp2 规则,从 myvector 容器中找到第一个违背 mycomp2 规则的元素
vector<int>::iterator iter = lower_bound(myvector.begin(), myvector.end(),3,mycomp2());
cout << "*iter = " << *iter;
return 0;
}

upper_bound()

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
/*
//查找[first, last)区域中第一个大于 val 的元素。
ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,
const T& val);
//查找[first, last)区域中第一个不符合 comp 规则的元素
ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,
const T& val, Compare comp);
*/

#include <iostream> // std::cout
#include <algorithm> // std::upper_bound
#include <vector> // std::vector
using namespace std;
//以普通函数的方式定义查找规则
bool mycomp(int i, int j) { return i > j; }
//以函数对象的形式定义查找规则
class mycomp2 {
public:
bool operator()(const int& i, const int& j) {
return i > j;
}
};
int main() {
int a[5] = { 1,2,3,4,5 };
//从 a 数组中找到第一个大于 3 的元素
int *p = upper_bound(a, a + 5, 3);
cout << "*p = " << *p << endl;
vector<int> myvector{ 4,5,3,1,2 };
//根据 mycomp2 规则,从 myvector 容器中找到第一个违背 mycomp2 规则的元素
vector<int>::iterator iter = upper_bound(myvector.begin(), myvector.end(), 3, mycomp2());
cout << "*iter = " << *iter;
return 0;
}

equal_range()

http://c.biancheng.net/view/7531.html

1
2
3
/*

*/

http://c.biancheng.net/view/7537.html

1
2
3
/*

*/

增加堆栈空间

int main() {
int size(512<<20); // 512M
asm ( “movq %0, %%rsp\n”::”r”((char*)malloc(size)+size)); // YOUR CODE

exit(0);

}

c++引用

  1. 引用的本质:

引用是一个变量的别名。 可以把它想象成对一个已存在变量的另一个称呼。

引用不是一个独立的变量,它不占用额外的内存空间来存储自身的值。 它是直接指向原始变量的内存地址。

对引用的操作等同于对原始变量的操作。

  1. 引用的声明和初始化:

语法: 类型 &引用名 = 变量名;

类型: 引用所引用的变量的类型。

&: 引用声明符。 它是区分引用和普通变量声明的关键。

引用名: 你为引用选择的名称。

变量名: 引用所引用的已存在的变量的名字。

必须初始化: 引用在声明时必须立即初始化,即必须指定它所引用的变量。 这是因为引用一旦创建,就永远绑定到那个变量,不能再改变。 尝试不初始化引用会导致编译错误。

1
2
3
int num = 10;
int &refNum = num; // 正确: refNum 是 num 的引用,并且初始化了
// int &ref; // 错误: 引用 ref 没有被初始化
  1. 引用的使用:

使用引用就像使用原始变量一样。 你可以读取引用的值,修改引用的值,将引用传递给函数等等。 所有这些操作都会直接影响原始变量。

1
2
3
4
5
6
7
8
9
10
11
12
13

int num = 10;
int &refNum = num;

cout << refNum; // 输出 10 (读取引用 refNum 的值)
refNum = 20; // 修改引用 refNum 的值(同时修改了 num 的值)
cout << num; // 输出 20 (num 的值已经被 refNum 修改)
IGNORE_WHEN_COPYING_START
content_copy
download
Use code with caution.
C++
IGNORE_WHEN_COPYING_END
  1. 引用的特性:

不能重新绑定: 引用一旦绑定到一个变量,就不能再绑定到另一个变量。

不存在空引用: 引用必须始终引用一个有效的对象。 不允许有空引用(即引用不指向任何东西)。

不能引用字面常量: 通常情况下,你不能直接引用字面常量(例如数字、字符串)。 如果你想引用字面常量,需要先将字面常量赋值给一个变量,然后引用这个变量。 但是,使用 const 关键字可以引用常量。

//int &ref = 10; //错误
const int &ref = 10; //正确,相当于创建了一个临时变量 const int temp = 10; int &ref = temp;
IGNORE_WHEN_COPYING_START
content_copy
download
Use code with caution.
C++
IGNORE_WHEN_COPYING_END

引用与指针的区别:

引用必须初始化,指针可以不初始化。

引用不能重新绑定,指针可以重新指向。

引用不存在空引用,指针可以为空指针 (nullptr)。

引用更安全:由于引用必须初始化且不能重新绑定,因此使用引用可以避免一些空指针和野指针的问题。

引用更容易使用:使用引用时不需要像指针那样使用 * 解引用操作符。

  1. 引用的应用场景:

函数参数传递: 通过引用传递参数可以避免复制实参,提高效率,并且允许函数修改实参的值。

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
void increment(int &x) {
x++; // 修改了实参的值
}

int main() {
int num = 5;
increment(num); // 通过引用传递 num
cout << num; // 输出 6 (num 的值被 increment 函数修改)
return 0;
}
IGNORE_WHEN_COPYING_START
content_copy
download
Use code with caution.
C++
IGNORE_WHEN_COPYING_END

函数返回值: 函数可以返回引用,允许调用者直接修改函数内部的变量。

int& getElement(int arr[], int index) {
return arr[index]; // 返回数组元素的引用
}

int main() {
int numbers[] = {1, 2, 3};
getElement(numbers, 1) = 10; // 修改了 numbers[1] 的值
cout << numbers[1]; // 输出 10
return 0;
}
IGNORE_WHEN_COPYING_START
content_copy
download
Use code with caution.
C++
IGNORE_WHEN_COPYING_END

运算符重载: 在运算符重载中,可以使用引用来提高效率并允许修改对象的状态。

示例总结:

#include

int main() {
int x = 5;
int &y = x; // y 是 x 的引用

std::cout << "x: " << x << std::endl;  // 输出:x: 5
std::cout << "y: " << y << std::endl;  // 输出:y: 5

y = 10; // 修改 y 的值

std::cout << "x: " << x << std::endl;  // 输出:x: 10  (x 的值也被修改了,因为 y 是 x 的引用)
std::cout << "y: " << y << std::endl;  // 输出:y: 10

return 0;

}
IGNORE_WHEN_COPYING_START
content_copy
download
Use code with caution.
C++
IGNORE_WHEN_COPYING_END

希望这个详细的解释能够帮助你理解 C++ 中引用的相关语法和用法。 记住,理解引用就是理解它是原始变量的另一个名字,并且对它的操作会直接影响原始变量。

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2023-2025 mieopm
  • Visitors: | Views:

有打赏功能?用一下

支付宝
微信