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

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. 【白话科普】聊聊 DNS 的那些小知识

    你是否在上网时,遇到过这样的情况:QQ 能正常发送消息,但是网页却打不开,查看网络连接又正常显示.面对这种情况很多小伙伴都感到有些无措.那究竟要怎么处理,这究竟是怎么回事呢? 上网查询得知,一般情况下 ...

  2. MVVM架构

    一.MVVM架构和Jetpack MVVM即Model-View-ViewModel的缩写,它的出现是为了将图形界面和业务逻辑,数据模型进行解耦.在前面章节所学习的Jetpack组件,大部分是为了能够 ...

  3. Java ElasticSearch 操作

    pom 文件中添加: <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId ...

  4. AtCoder Beginner Contest 185 题解

    A - ABC Preparation 排序找出最小值 int main() { ios_base::sync_with_stdio(false), cin.tie(0); vector<int ...

  5. 牛客 | 一起来做题~欢乐赛2 (AK 题解)

    补题链接:Here A.新比赛,在眼前. 对于每次猜数和裁判的判断,可以确定一个区间内所有的数都有可能,比如对于样例中(8 +)来说,[ -INT_MIN, 7] 中所有的数都有可能,那么对于每次猜数 ...

  6. vue学习笔记 十三、路由介绍

    系列导航 vue学习笔记 一.环境搭建 vue学习笔记 二.环境搭建+项目创建 vue学习笔记 三.文件和目录结构 vue学习笔记 四.定义组件(组件基本结构) vue学习笔记 五.创建子组件实例 v ...

  7. C#设计模式09——组合模式的写法

    1. 什么是C#组合模式? 组合模式是一种结构型设计模式,它允许将对象组合成树形结构以表示"整体/部分"层次结构.使用此模式,客户端可以按相同的方式处理单个对象和对象集合,而不必关 ...

  8. 9 时序数据库M3DB架构与原理

    一.M3DB介绍 M3DB是Uber开源的一款分布式时序数据库,已在Uber内部使用多年.M3DB有以下特性: 分布式的时序数据库,可以水平扩展存储. 支持Pormetheus的查询语言PromQL, ...

  9. 【转】C语言表驱动法编程实践

    来源:C语言表驱动法编程实践(精华帖,建议收藏并实践) (qq.com) 数据压倒一切.如果选择了正确的数据结构并把一切组织的井井有条,正确的算法就不言自明.编程的核心是数据结构,而不是算法. --R ...

  10. 左值,右值,引用,指针,常量,auto如何组合?

    左值,右值,引用,指针,常量,auto如何组合? 左值引用:int &a = b; 左值引用是通过使用&符号来声明的,例如int &a. 左值引用用于绑定到左值(可标识的.持久 ...