由于内容较少,所以,不想把它放在我的本地博客中了,暂时保存在这里,代码有一部分来源于网络,比较经典的案例,同样收藏起来。

Stack 栈容器

Stack容器适配器中的数据是以LIFO的方式组织的,它是一种先进后出的数据结构,栈允许对元素进行新增,移除,获取栈顶,等操作,但栈不允许对内部元素进行遍历,只能访问栈顶部的元素,只有在移除栈顶部的元素后,才能访问下方的元素.

Stack 压栈/出栈:

#include <iostream>
#include <stack>
using namespace std; int main(int argc, char* argv[])
{
stack<int> st; st.push(1); // 入栈
st.push(2); while (!st.empty() || st.size()!=0)
{
cout << "pop -> " << st.top() << endl;
st.pop(); // 出栈
}
system("pause");
return 0;
}

heap 堆容器

入堆

#include <iostream>
#include <algorithm>
#include <vector> using namespace std; void print(int x){ cout << x << " "; } int main(int argc, char* argv[])
{
vector<int> v; v.push_back(1);
v.push_back(2);
v.push_back(3); push_heap(v.begin(), v.end()); // 入堆
for_each(v.begin(), v.end(), print);
system("pause");
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector> using namespace std; void print(int x){ cout << x << " "; } int main(int argc, char* argv[])
{
vector<int> v; v.push_back(1);
v.push_back(2);
v.push_back(3); make_heap(v.begin(), v.end()); // 创建堆 push_heap(v.begin(), v.end()); // 入堆 for_each(v.begin(), v.end(), print); pop_heap(v.begin(), v.end()); for_each(v.begin(), v.end(), print);
system("pause");
return 0;
}

常用的代码片段

数组实现逆序排列: 所谓数组逆序就是讲一个正向数组反向排列,并输出排序后的结果.

#include <stdio.h>

void Swap_Array(int Array[], int Number)
{
int x = 0;
int y = Number - 1;
while (x < y)
{
int tmp;
tmp = Array[x];
Array[x] = Array[y];
Array[y] = tmp;
x++; y--;
}
} int main(int argc, char* argv[])
{
int Array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; Swap_Array(Array, 10); for (int x = 0; x < 10; x++)
{
printf("%d ", Array[x]);
} system("pause");
return 0;
}

用数组冒泡排序: 冒泡排序是经典的算法,也是学习数组必学知识点,这里总结一份冒泡排序.

#include <stdio.h>
#include <stdlib.h>
#include <string.h> void bubble(int *arr, int len)
{
int flag = 1;
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
flag = 0;
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
if (flag)
return;
flag = 1;
}
} int main(int argc, char* argv[])
{
int Array[] = {1,2,3,4,5,6,7,8,9,10}; int len = sizeof(Array) / sizeof(int);
bubble(Array, len); for (int x = 0; x < len; x++)
printf("%d \n", Array[x]); system("pause");
return 0;
}

打印乘法口诀表:

#include <stdio.h>

int main(int argc, char* argv[])
{
int i, j;
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d*%d=%d\t", j, i, i*j);
}
printf("\n");
}
system("pause");
return 0;
}

输出随机数与字母: 通过使用<time.h>模块中的rand()函数实现生成随机数.

#include <stdio.h>
#include <time.h> int main(int argc, char* argv[])
{
srand((unsigned int)time(NULL)); // 设置随机种子
for (int x = 0; x <= 20; x++)
{
int rand_num = rand() % 100 + 1; // 产生 1 - 100 范围内的随机数
printf("1-100以内随机数: %d \n", rand_num); rand_num = rand() % 25 + 66; // 产生 65 - 90 范围内的随机数
printf("A-Z以内的随机数: %c \n", rand_num); rand_num = rand() % 25 + 98; // 产生 97 - 122 范围内的随机数
printf("a-z以内的随机数: %c \n", rand_num);
}
system("pause");
return 0;
}

生成随机数字串: 通过循环实现生成随机的字符串,可以用于秘钥计算.

#include <iostream>
#include <time.h> int GetRandom(int bit){
for (int x = 0; x < bit; x++)
{
int rand_number = 0;
int rand_range = rand() % 4 + 1; // 取 1-3 之间的随机数
switch (rand_range)
{
case 1: // 得到一个整数
rand_number = rand() % 10 + 1;
printf("%d", rand_number);
case 2: // 得到一个小写字母
rand_number = rand() % 25 + 66;
printf("%c", rand_number);
case 3: // 得到一个大写字母
rand_number = rand() % 25 + 98;
printf("%c", rand_number);
default:
continue;
}
}
return 1;
} int main(int argc, char* argv[])
{
for (int x = 0; x < 3; x++)
{
GetRandom(5);
printf("-");
}
printf("\n");
system("pause");
return 0;
}

随机数去重

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h> int main(int argc, char* argv[])
{
srand((unsigned int)time(NULL)); int ran[10]; for (int x = 0; x < 10; x++)
{
ran[x] = rand() % 33 + 1;
// 实现去重
for (int y = 0; y < x; y++)
{
if (ran[x] == ran[y])
{
x--; continue;
}
}
} for (int x = 0; x < 10; x++)
{
printf("%d ", ran[x]);
} system("pause");
return 0;
}

时间日期格式化: 将时间与日期格式化成想要的样子然后输出到屏幕上.

#include <iostream>
#include <time.h> int main(int argc, char* argv[])
{
time_t curtime;
struct tm *info;
char buffer[80]; time(&curtime);
printf("当前时间: %s\n", ctime(&curtime)); info = localtime(&curtime);
info->tm_year = info->tm_year + 1; // 年份向后延期一年
info->tm_mon = info->tm_mon - 2; // 月份向前推2个月 strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", info);
printf("格式化本地时间: %s\n", buffer); time_t seconds;
seconds = time(NULL);
printf("自 1970-01-01 起的小时数 = %ld\n", seconds / 3600); system("pause");
return 0;
}

输入日期判断是周几:

#include <iostream>
#include <time.h> int main(int argc, char* argv[])
{
time_t rawtime;
struct tm *timeinfo;
int year = 2013, month = 12, day=21;
const char *weekday[] = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" }; time(&rawtime);
timeinfo = localtime(&rawtime);
timeinfo->tm_year = year - 1900;
timeinfo->tm_mon = month - 1;
timeinfo->tm_mday = day;
mktime(timeinfo); printf("%d-%d-%d 是 => %s \n", year,month,day,weekday[timeinfo->tm_wday]);
system("pause");
return 0;
}

计算时间差:

#include <time.h>
#include <iostream> int main(int argc, char* argv[])
{
clock_t start_t, end_t;
double total_t;
start_t = clock();
for (int i = 0; i< 10000000; i++)
{
}
end_t = clock();
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf("CPU 占用的总时间:%f\n", total_t);
system("pause");
return(0);
}

Popen 执行命令

#include <iostream>
#include <stdio.h> int main(int argc, char* argv[])
{
char buffer[4096];
FILE *fi = _popen("ipconfig", "r");
while (fgets(buffer, 4096, fi) != NULL){
fprintf(stdout, "%s", buffer);
}
system("pause");
return 0;
}

隐藏控制台

#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
#include <stdio.h> int main()
{
while (1)
printf("a");
return 0;
}

猴子吃桃

#include <stdio.h>
int main(int argc, char *argv[])
{
int day,x1,x2;
day=9;
x2=1;
while(day>0)
{
x1=(x2+1)*2;
x2=x1;
day--;
}
printf ("the total is %d\n",x1);
return 0;
}

穷举灯塔数量

#include <stdio.h>
int main(int argc, char *argv[])
{
int n=1,m,sum,i;
while(1)
{
m=n;
sum=0;
for (i=1; i<8; i++)
{
m=m*2;
sum+=m;
}
sum+=n;
if (sum==765)
{
printf ("the first floor has %d\n",n);
printf ("the eight floor has %d\n",m);
break;
}
n++;
}
return 0;
}

小球下落问题

#include <stdio.h>
int main(int argc, char *argv[])
{
float i,h=100,s=100;
for (i=1; i<=9; i++)
{
h=h/2;
s+=h*2;
}
printf ("the total length is %f\n",s);
printf ("the lenght of tenth time is %f\n",h/2);
return 0;
}

彩球问题

#include <stdio.h>
int main(int argc, char *argv[])
{
int i,j,count;
puts("the result is:\n"); //向屏幕上输出提示信息
printf ("time red ball white ball black ball\n");
count = 1;
for(i=0;i<=3;i++)
for(j=0;j<=3;j++)
if((8-i-j)<=6)
printf ("%3d%7d%12d%12d\n",count++,i,j,8-i-j);
return 0;
}

打印杨辉三角

#include <stdio.h>
int main(int argc, char *argv[])
{
int i,j,a[11][11];
for (i=1; i<11; i++)
{
a[i][i]=1;
a[i][1]=1;
}
for (i=3; i<11; i++)
{
for (j=2; j<=i-1; j++)
{
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
for (i=1; i<11; i++)
{
for (j=1; j<=i; j++)
printf ("%4d\t",a[i][j]);
printf ("\n");
}
return 0;
}

打印乘法口决表

#include <stdio.h>
int main(int argc, char *argv[])
{
int i,j;
for (i=1; i<=9; i++)
{
for (j=1; j<=i; j++)
{
printf ("%d*%d=%d p",i,j,i*j);
}
printf ("\n");
}
return 0;
}

评定成绩等级

#include <stdio.h>
int main(int argc, char *argv[])
{
int score;
printf ("please enter score(score<=10):");
scanf("%d",&score);
if (score==100)
{
score =90;
}
score = score/10;
switch(score)
{
case 9:
printf ("the grade is A\n");
break;
case 8:
printf ("the grade is B\n");
break;
case 7:
printf ("the grade is C\n");
break;
case 6:
printf ("the grade is D\n");
break;
default:
printf ("the grade is E\n");
}
return 0;
}

对调数问题

#include <stdio.h>
int main(int argc, char *argv[])
{
int x,y,z,x1,y1,z1,i,k,n,j=0;
while(1)
{
printf ("please input an integer\n");
scanf("%d",&n);
if (n<=10||n>=100)
{
printf ("data error\n");
continue;
}
else if (n%10==0)
{
printf ("data error\n");
continue;
}
else
{
x=n/10;
y=n%10;
z=10*y+x;
break;
}
}
for (i=11; i<100; i++)
{
if (i%10==0)
continue; //结束本次循环
else
{
x1=i/10;
y1=i%10;
z1=10*y1+1;
//判断是否满足等式条件
if (n+i==z+z1&&n!=z1)
{
printf ("%d+%d=%d+%d\n",n,i,z,z1);
j++;
}
else
continue;
}
}
if(j==0)
printf ("inexistince\n");
return 0;
}

判断润年

#include <stdio.h>
int main(int argc, char *argv[])
{
int year;
printf ("please input the year:\n");
scanf("%d",&year);
if((year%4==0&&year%100!=0)||year%400==0)
printf ("%d is a leap year\n",year);
else
printf ("%d is not a leap year\n",year);
return 0;
}

阶梯问题

#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
for (i=100; i<1000; i++)
{
if(i%2==1&&i%3==2&&i%5==4&&i%6==5&&i%7==0)
printf ("the number of the stairs is %d\n",i);
}
return 0;
}

IP地址形式输出

#include <stdio.h>
int bin_dec(int x,int n) //将而进制转换成十进制
{
if(n==0)
return 1;
return x*bin_dec(x,n-1);
}
int main(int argc, char *argv[])
{
int i;
int ip[4]={0};
char a[33];
printf ("please input binary number:\n");
scanf("%s",a);
for (i=0; i<8; i++)
{
if (a[i]=='1')
{
ip[0]+=bin_dec(2,7-i);
}
}
for (i=8; i<16; i++)
{
if (a[i]=='1')
{
ip[1]+=bin_dec(2,15-i);
}
}
for (i=16; i<24; i++)
{
if (a[i]=='1')
{
ip[2]+=bin_dec(2,23-i);
}
}
for (i=24; i<32; i++)
{
if (a[i]=='1')
{
ip[3]+=bin_dec(2,31-i);
}
if (a[i]=='\0')
{
break;
}
}
printf ("ip:");
printf ("%d.%d.%d.%d\n",ip[0],ip[1],ip[2],ip[3]);
return 0;
}

N进制转换为十进制

#include <stdio.h>
#include <string.h>
main()
{
long t1;
int i,n,t,t3;
char a[100];
printf ("please input a number string:\n");
gets(a); //输入N进制数存到数组a中
strupr(a); //将a中的小写字母转换成大写字母
t3=strlen(a);
t1=0;
printf ("please input n(2or8or16):\n");
scanf("%d",&n);
for (i=0; i<t3; i++)
{
if (a[i]-'0'>=n&&a[i]<'A'||a[i]-'A'+10>=n)//判断输入的数据和进制数是否相等
{
printf ("data error!!");
exit(0); //推出程序
}
if (a[i] >= '0'&&a[i] <= '9') //判断是否为数字
t=a[i]-'0';
else if(n>=11&&(a[i]>='A'&&a[i]<='A'+n-10)) //判断是否为字母o
t=a[i]-'A'+10;
t1=t1*n+t; //求出最终转换成十进制的值
}
printf ("the decimal is %ld\n",t1);
return 0;
}

求同学的平均身高

#include <stdio.h>
float average(float array[],int n)
{
int i;
float aver,sum=0;
for(i=0;i<n;i++)
sum+=array[i];
aver=sum/n;
return(aver);
}
int main(int argc, char *argv[])
{
float average(float array[],int n);
float height[100],aver;
int i,n;
printf ("please input the number of students:\n");
scanf("%d",&n);
printf ("please input student's height:\n");
for(i=0;i<n;i++)
scanf("%f",&height[i]);
printf ("\n");
aver=average(height,n);
printf ("average height is %6.2f\n",aver);
return 0;
}

读取进程中的PEB环境块:

#include <stdio.h>
#include <windows.h>
#include <winternl.h> typedef NTSTATUS(NTAPI *typedef_NtQueryInformationProcess)(
IN HANDLE ProcessHandle,
IN PROCESSINFOCLASS ProcessInformationClass,
OUT PVOID ProcessInformation,
IN ULONG ProcessInformationLength,
OUT PULONG ReturnLength OPTIONAL
); PEB Get_PEB(DWORD dwPid)
{
typedef_NtQueryInformationProcess NtQueryInformationProcess = NULL;
PROCESS_BASIC_INFORMATION pbi = { 0 };
RTL_USER_PROCESS_PARAMETERS Param = { 0 };
PEB peb = { 0 }; HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid); // 需要通过 LoadLibrary、GetProcessAddress 从 ntdll.dll 中获取地址
NtQueryInformationProcess = (typedef_NtQueryInformationProcess)::GetProcAddress(
::LoadLibrary("ntdll.dll"), "NtQueryInformationProcess");
if (NULL != NtQueryInformationProcess)
{
// 获取指定进程的基本信息
NTSTATUS status = NtQueryInformationProcess(hProcess, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
} // 获取指定进程进本信息结构中的PebBaseAddress
::ReadProcessMemory(hProcess, pbi.PebBaseAddress, &peb, sizeof(peb), NULL);
// 获取指定进程环境块结构中的ProcessParameters, 注意指针指向的是指定进程空间中
// ::ReadProcessMemory(hProcess, peb.ProcessParameters, &Param, sizeof(Param), NULL); printf("是否被调试: %d \n", peb.BeingDebugged);
return peb;
} int main(int argc, char * argv[])
{
Get_PEB(GetCurrentProcessId());
system("pause");
return 0;
}

C/C++ 常用开发代码片段的更多相关文章

  1. IOS开发效率之为Xcode添加常用的代码片段

    IOS开发效率之为Xcode添加常用的代码片段 原文地址:http://blog.csdn.net/pingchangtan367/article/details/30041285 tableview ...

  2. IOS开发-OC学习-常用功能代码片段整理

    IOS开发-OC学习-常用功能代码片段整理 IOS开发中会频繁用到一些代码段,用来实现一些固定的功能.比如在文本框中输入完后要让键盘收回,这个需要用一个简单的让文本框失去第一响应者的身份来完成.或者是 ...

  3. 常用JS代码片段

    1.隐藏部分数字,如手机号码,身份证号码 1 2 3 function (str,start,length,mask_char){ return str.replace(str.substr(star ...

  4. js/jquery/html前端开发常用到代码片段

    1.IE条件注释 条件注释简介 IE中的条件注释(Conditional comments)对IE的版本和IE非IE有优秀的区分能力,是WEB设计中常用的hack方法.条件注释只能用于IE5以上,IE ...

  5. visual studio code开发代码片段扩展插件

    背景 visual studio code编辑器强大在于可以自己扩展插件,不仅可以去插件市场下载,也可以按照官方的API很方便的制作适合自己的插件: 自己最近在开发一个手机端网站项目,基于vant项目 ...

  6. 记录C#常用的代码片段

    时间一久,常用的代码会有点忘记,还是贴在这里方便查找! 1.将信息写入文件中 //将字符串写入到文本中 void writeToText(string msg) { try { msg = DateT ...

  7. WebApp 开发中常用的代码片段

    其实这里面的多数都是 iOS 上面的代码.其他平台的就没有去验证了. HTML, 从HTML文档的开始到结束排列: <meta name=”viewport” content=”width=de ...

  8. Java开发常用的代码片段

    1. 字符串有整型的相互转换 String a = String.valueOf(2);   //integer to numeric string  int i = Integer.parseInt ...

  9. 常用torch代码片段合集

    PyTorch常用代码段整理合集 本文代码基于 PyTorch 1.0 版本,需要用到以下包 import collections import os import shutil import tqd ...

  10. TornadoFx学习笔记(1)——常用的代码片段

    Tornadofx是基于JavaFx的一个kotlin实现的框架 之后看情况补充.. 1.读取resources文件夹中的文件 如图 想要读取config.properties文件,有两种方法 在cl ...

随机推荐

  1. WPF 实现窗体鼠标事件穿透

    一.窗体变透明,需要加三个属性: AllowsTransparency="True"Background="Transparent"WindowStyle=&q ...

  2. UVA - 1594 :Ducci Sequence (set应用)

    给定n元组(a1,a2,...,an),ai均为整数,得到下一个序列为(|a1-a2|,|a2-a3|,...,|an-a1|),如此循环下去,必定会出现全零序列或重复序列. 现要求判断给定序列是全零 ...

  3. 用ArcGIS模型构建器生成、导出Python转换空间坐标系的代码

      本文介绍在ArcMap软件中,通过创建模型构建器(ModelBuilder),导出地理坐标系与投影坐标系之间相互转换的Python代码的方法.   在GIS领域中,矢量.栅格图层的投影转换是一个经 ...

  4. 传统与现代可视化 PK:再生水厂二维工艺组态系统

    前言 随着可视化技术的进步与发展,传统再生水厂组态系统所展示的组态页面已逐渐无法满足当前现阶段多样化的展示手段.使得系统对污泥处理处置及生产运行成本方面的监控.分析方面较为薄弱,急需对信息化应用成果和 ...

  5. python爬虫-豆瓣电影top250

    一.python爬虫简介1.什么是爬虫:网络爬虫,是一种按照一定规则,自动抓取互联网信息的程序或者脚本.由于互联网数据的多样性和资源的有限性,根据用户需求定向抓取相关网页并分析已成为如今主流的爬取策略 ...

  6. http-自签证书

    1. 背景 证书需要向云服务提供商购买,是需要付费,但用在应用开发场景是不合适的,需要开发者自己自签证书进行测试 2. 工具包 Cygwin a large collection of GNU and ...

  7. Go-值传递&引用传递

    值类型和引用类型 值类型关注其值 引用类型关注其指针 值类型和引用类型区别在于传递值的时候拷贝的对象不同,值传递拷贝是变量的值,引用传递拷贝的是变量的指针 拷贝 -- 传递值 赋值 函数调用 初始化 ...

  8. [转帖]GC日志解读,这次别再说看不懂GC日志了

    https://juejin.cn/post/7029130033268555807   测试环境:机器内存16G,JDK 8,12核CPU 测试用例,从网上找的示例,视情况修改即可:   java ...

  9. [转帖]TiDB 使用 dumpling 导出数据,并使用 lightning 导入到另一个 TiDB 库

    本文介绍从 TiDB-A 库导出数据到 TiDB-B 库: 导出 Dumpling 包含在 tidb-toolkit 安装包中,可在此下载. 从 TiDB/MySQL 导出数据 需要的权限 SELEC ...

  10. [转帖]【SOP】最佳实践之 TiDB 业务写变慢分析

    https://zhuanlan.zhihu.com/p/647831844 前言 在日常业务使用或运维管理 TiDB 的过程中,每个开发人员或数据库管理员都或多或少遇到过 SQL 变慢的问题.这类问 ...