21xrx.com
2025-06-20 09:27:42 Friday
登录
文章检索 我的文章 写文章
Dev C++ 常用代码汇总
2023-07-05 06:08:50 深夜i     15     0
Dev C++ 常用代码 汇总

Dev C++是一款常用的C++集成开发环境,由于其简单易用的特点,受到了很多开发者的青睐。在开发过程中,我们经常会使用一些常用代码来加快开发速度。下面是一些常用的Dev C++代码汇总,供大家参考使用。

1. 输入输出:

使用iostream库进行输入输出

#include <iostream>
using namespace std;
int main()
  int a;
  cin >> a;
  cout << a << endl;
  return 0;

2. 常量定义:

使用const关键字定义常量

#include <iostream>
using namespace std;
const int MAX_NUM = 100;
int main()
  cout << MAX_NUM << endl;
  return 0;

3. 数组:

使用数组来存储一组数据

#include <iostream>
using namespace std;
const int ARRAY_SIZE = 4;
int main()
{
  int array[ARRAY_SIZE] = 4;
  for(int i = 0; i < ARRAY_SIZE; i++)
  {
    cout << array[i] << " ";
  }
  cout << endl;
  return 0;
}

4. 条件判断:

使用if、else语句进行条件判断

#include <iostream>
using namespace std;
int main()
{
  int a = 10;
  if(a > 5)
  
    cout << "a大于5" << endl;
  
  else
  
    cout << "a小于等于5" << endl;
  
  return 0;
}

5. 循环:

使用for循环进行循环操作

#include <iostream>
using namespace std;
const int ARRAY_SIZE = 4;
int main()
{
  int array[ARRAY_SIZE] = 3;
  for(int i = 0; i < ARRAY_SIZE; i++)
  {
    cout << array[i] << " ";
  }
  cout << endl;
  return 0;
}

6. 函数:

使用函数进行代码封装和重用,提高代码的可维护性和复用性

#include <iostream>
using namespace std;
int add(int a, int b)
{
  return a + b;
}
int main()
{
  int a = 1, b = 2;
  cout << add(a, b) << endl;
  return 0;
}

7. 结构体:

使用结构体将多个数据封装为一个整体

#include <iostream>
using namespace std;
struct person
  string name;
  int age;
;
int main()
{
  person p = "张三";
  cout << p.name << "的年龄是" << p.age << endl;
  return 0;
}

8. 指针:

使用指针进行内存地址的访问和操作

#include <iostream>
using namespace std;
int main()
{
  int a = 10;
  int* p = &a;
  cout << *p << endl;
  return 0;
}

以上是常用的Dev C++代码汇总,希望对大家有所帮助。在实际开发中,代码的使用更多还是需要根据需求进行灵活的变通。

  
  

评论区