c++ array容器 传参_在编程竞赛中高效地编写C/C ++代码

1eed6c15254cb07f05057fd5492767cf.png

首先,您需要了解模板,宏和向量,然后再进行下一阶段!

  • 模板是通用编程的基础,它涉及以独立于任何特定类型的方式编写代码。
  • 宏是已命名的代码片段。每当使用该名称时,它就会被宏的内容替换。
  • 向量与动态数组相同,具有在插入或删除元素时自动调整自身大小的能力,并且容器自动处理其存储。

我们可以使用这些强大的工具以有效的方式编写代码。

编程竞赛中可以使用的一些很酷的技巧如下:

1. 使用基于范围的for循环:这是C ++ 11中非常酷的功能,如果您要从头到尾进行迭代,则最好使用。 这段代码显示了如何使用range循环遍历数组和向量:

// C++ program to demonstrate range based for // loops for accessing vector and array elements #include #include  using namespace std;   int main() {     // Create a vector object that     // contains 5 elements     vector vec = {0, 1, 2, 3, 4};       // Type inference by reference using auto.     // Range based loops are preferred when no     // modification is needed in value     for (const auto &value : vec)         cout << value << ' ';       cout << '';       // Basic 5 element integer array     int array[]= {1, 2, 3, 4, 5};     for (const auto &value: array)         cout << value << " ";       return 0; } 

输出:

0 1 2 3 41 2 3 4 5

2. 初始化列表:此类型用于访问C ++初始化列表中的值。在这里,这种类型的对象由编译器根据初始化列表声明自动构造,该列表是用大括号括起来的逗号分隔元素的列表。

#include   template void printList(std::initializer_list text) {     for (const auto & value: text)         std::cout << value << " "; }   // Driver program int main() {     // Initialization list     printList( {"One", "Two", "Three"} );     return 0; } 

输出:

One Two Three

3. 分配最大值或最小值:这一点有助于避免在编写max()或min()函数时花费过多的精力。

#include   // Call by reference is used in x template static inline void amin(T &x, U y) {     if (y < x)         x = y; }   // call by reference is used in x template static inline void amax(T &x, U y) {     if (x < y)         x = y; }   // Driver program to find the Maximum and Minimum value int main() {     int max_val = 0, min_val = 1e5;     int array[]= {4, -5, 6, -9, 2, 11};       for (auto const &val: array)           // Same as max_val = max (max_val, val)         // Same as min_val = min (min_val,val)         amax(max_val, val), amin (min_val, val);         std::cout << "Max value = " << max_val << ""              << "Min value = " << min_val;     return 0; } 

输出:

Max value = 11Min value = -9

4. C / C ++中的快速输入/输出:在编程竞赛中,您必须尽可能快地阅读输入/输出,以节省宝贵的时间。

#include    template void scan(T &x) {     x = 0;     bool neg = 0;     register T c = getchar();       if (c == '-')         neg = 1, c = getchar();       while ((c < 48) || (c > 57))         c = getchar();       for ( ; c < 48||c > 57 ; c = getchar());       for ( ; c > 47 && c < 58; c = getchar() )         x= (x << 3) + ( x << 1 ) + ( c & 15 );       if (neg) x *= -1; }   template void print(T n) {     bool neg = 0;       if (n < 0)         n *= -1, neg = 1;       char snum[65];     int i = 0;     do    {         snum[i++] = n % 10 + '0';         n /= 10;     }       while (n);     --i;       if (neg)         putchar('-');       while (i >= 0)         putchar(snum[i--]);       putchar(''); }   // Driver Program int main() {     int value;       // Taking input     scan(value);       // Printing output     print(value);     return 0; } 
输入:756输出:756

5. 使用宏作为循环:也许,使用这样的宏不太好,因为它会降低代码的可读性,但是对于编写快速的代码,您可以冒这个风险!

#include  using namespace std;   #define rep(i,n) for (i = 0; i < n; ++i) #define REP(i,k,n) for (i = k; i <= n; ++i) #define REPR(i,k,n) for (i = k; i >= n; --i)     // Driver program to test above Macros int main() {     int i;     int array[] = {4, 5, 6, 9, 22, 11};     int size= sizeof(array)/sizeof(array[0]);           // Default 0 index based loop     rep(i, size)              cout << array[i] << " ";     cout<

输出:

4 5 6 9 22 115 6 9 22 1111 22 9 6 5 4

一些更重要的要点可以进一步缩短您的时间:

6. 使用“ bits/stdc++.h ”:无需添加大量的#include行,而只需使用#include 文件就包含了编程竞赛中所需的所有头文件,从而节省了很多时间 。

7. 容器:使用各种容器,如vector、list、map等,使人们能够使用预定义的功能并显着减少代码的大小。

8. 快速cin和cout:如果将cin和cout用于I / O,只需在main()之后添加以下行。

std::ios_base::sync_with_stdio(false);

9. auto:使用自动声明数据类型可以节省编程比赛期间的大量时间。将变量定义为auto时,编译器会在编译时确定其类型。

10. 库和预定义函数:在任何适用的地方使用内置函数,例如__gcd(A,B),swap,_builtin_popcount(R),_ builtin_clz(R)等。尝试学习C++算法库中可用的不同函数,它们在程序中大多数时候都很有用。

最终,通过使用这些巧妙的技巧,您可以轻松地用最少的时间和代码行来编写代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值