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

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. leaflet 绘制 点 线 面 圆 椭圆 线缓冲区

    leaflet有个绘图插件Leaflet.draw,但是我不想要它的控件,只想用它的绘制功能,控件我自己提供,当时不知道如何使用,就自己写了个绘制点线面圆和椭圆的工具,代码如下: /// <re ...

  2. 如何优雅的在 Word 中添加漂亮的代码?

    Step 01 第一步,在编程软件里找到你想要放进Word文档里的代码,复制下来. Step 02 第二步,打开Notepad++,将代码直接粘贴. Step 03 第三步,这个时候的代码是没有任何格 ...

  3. Educational Codeforces Round 102 Personal Editorial(A~C,max Rating 1500)

    1473A. Replacing Elements Rating 800 对数组排序,一旦数组中最大的数即a[n-1]是一个小于或等于d的数,直接输出YES即可,否则运用数组中最小的两个数加和替换最大 ...

  4. 前端科普系列(4):Babel —— 把 ES6 送上天的通天塔

    本文首发于 vivo互联网技术 微信公众号 链接: https://mp.weixin.qq.com/s/plJewhUd0xDXh3Ce4CGpHg作者:Morrain 一.前言 在上一节 < ...

  5. 【驱动】SPI驱动分析(七)-SPI驱动常用调试方法

    用户态 用户应用层使用spidev驱动的步骤如下: 打开SPI设备文件:用户可以通过打开/dev/spidevX.Y文件来访问SPI设备,其中X是SPI控制器的编号,Y是SPI设备的编号. 配置SPI ...

  6. 《深入理解计算机系统》(CSAPP)实验四 —— Attack Lab

    这是CSAPP的第四个实验,这个实验比较有意思,也比较难.通过这个实验我们可以更加熟悉GDB的使用和机器代码的栈和参数传递机制. @ 目录 实验目的 准备工作 内容简介 代码注入攻击 Level 1 ...

  7. Java虚拟机——内存区域及内存溢出异常

    一.Java内存区域 1.概述 对于java程序员来说,在虚拟机的自动内存管理机制的帮助下,不需要为每一个new操作去写delete/free代码,而且不容易出现内存泄漏和内存溢出问题.但是把内存控制 ...

  8. P1047 [NOIP2005 普及组] 校门外的树

    1.题目介绍 [NOIP2005 普及组] 校门外的树 题目描述 某校大门外长度为 \(l\) 的马路上有一排树,每两棵相邻的树之间的间隔都是 \(1\) 米.我们可以把马路看成一个数轴,马路的一端在 ...

  9. 【面试题精讲】你知道MySQL中有哪些隔离级别吗

    有时博客内容会有变动,首发博客是最新的,其他博客地址可能未同步,请认准https://blog.zysicyj.top 首发博客地址 系列文章地址 脏读(Dirty Read)是指一个事务读取到了另一 ...

  10. [转帖]聊聊TPS、QPS、CPS概念和区别

    https://cloud.tencent.com/developer/article/1859053 TPS 概念 TPS:是TransactionsPerSecond的缩写,也就是事务数/秒.它是 ...