专栏文章
我的代码库
生活·游记参与者 1已保存评论 0
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @miq8h2yw
- 此快照首次捕获于
- 2025/12/04 00:40 3 个月前
- 此快照最后确认于
- 2025/12/04 00:40 3 个月前
变量(string).size() :将一个变量的每一个长度分为一个数组中的一个模块
b :二进制数
o :八进制数
d :十进制数
h :十六进制数
scanf("%o",&x) :输入不同进制的数
printf("%d",x) :输出不同进制的数
ASCII码 char(整数变量)
int(字符变量)
开头:
#include
#include<bits/stdc++.h>
#include
#include <math.h>
#include
#include
using namespace std;
变量(可写、可不写)
int main(){
}
反向输出简单代码:
int y;
cin>>y;
int a[y];
for(int u=1;u<=y;u++){
cin>>a[u];
}
for(int o=y;o>=1;o--){
cout<<a[o]<<" ";
}
颜色定义:#define NONE "\033[m"
红色#define RED "\033[0;32;31m" printf(RED" "NONE);
亮红色#define LIGHT_RED "\033[1;31m" printf(LIGHT_RED" "NONE);
绿色#define GREEN "\033[0;32;32m" printf(GREEN" "NONE);
亮绿色#define LIGHT_GREEN "\033[1;32m" printf(LIGHT_GREEN" "NONE);
蓝色#define BLUE "\033[0;32;34m" printf(BLUE" "NONE);
亮蓝色#define LIGHT_BLUE "\033[1;34m" printf(LIGHT_BLUE" "NONE);
灰色#define DARY_GRAY "\033[1;30m" printf(DARY_GRAY" "NONE);
青色#define CYAN "\033[0;36m" printf(CYAN" "NONE);
亮青色#define LIGHT_CYAN
"\033[1;36m" printf(LIGHT_CYAN" "NONE); 紫色#define PURPLE "\033[0;35m" printf(PURPLE" "NONE); 亮紫色#define LIGHT_PURPLE "\033[1;35m" printf(LIGHT_PURPLE" "NONE); 棕色#define BROWN "\033[0;33m" printf(BROWN" "NONE); 黄色#define YELLOW "\033[1;33m" printf(YELLOW" "NONE); 亮灰色#define LIGHT_GRAY "\033[0;37m" printf(LIGHT_GRAY" "NONE); 白色#define WHITE "\033[1;37m" printf(WHITE" "NONE);
"\033[1;36m" printf(LIGHT_CYAN" "NONE); 紫色#define PURPLE "\033[0;35m" printf(PURPLE" "NONE); 亮紫色#define LIGHT_PURPLE "\033[1;35m" printf(LIGHT_PURPLE" "NONE); 棕色#define BROWN "\033[0;33m" printf(BROWN" "NONE); 黄色#define YELLOW "\033[1;33m" printf(YELLOW" "NONE); 亮灰色#define LIGHT_GRAY "\033[0;37m" printf(LIGHT_GRAY" "NONE); 白色#define WHITE "\033[1;37m" printf(WHITE" "NONE);
1秒 约等于 重复执行输出4000次
清屏:system("cls");
输出月份时间(月份和日期间隔一个“-”)简单代码:
if(a<10)cout<<"0"<<a;
else cout<<a;
cout<<"-";
if(b<10)cout<<"0"<<b;
else cout<<b;
二进制转十进制公式:
1 0 0 0 1 0 0 0 1
8 7 6 5 4 3 2 1 0
1 0 0 0 1 0 0 0 1
十进制 800040001
数组从0开始
数组终端输入while(cin>>a[p])p++;
静态数据成员包括静态数据和静态函数成员访问权限和普通成员一样静态数据成员用于描述类的总体信息必须在类的体外定义并初始化。Union不允许定义静态成员union数据成员共享存储空间,而静态成员各自独立分配存储单元。
静态数据成员脱离对象独立存在存储单元不属于任何对象的一部分在计算类活对象的空间大小时不包括静态数据成员局部类不允许定义静态数据成员
■▄▉▊▋▌▍▎▏█▇▆▅▃▂▁▄▉▊▋▌▍▎▏█▇▆▅▃▂▁
素数自定义函数
bool p(int x){
int f=1;
for(int i=2;ii<=x;i++){
if(x%i==0)f=0;
}
return f;
}
回文数自定义函数
bool y(int x){
int s=0,t=x;
while(x>0){
s=s10+x%10;
x/=10;
}
return s==t;
}
上下左右代码:
char ch;
while(true)
{
ch = getch();
if (ch == -32)
{
ch = getch();
switch (ch)
{
case 72:
cout << "上" << endl;
break;
case 80:
cout << "下" << endl;
break;
case 75:
cout << "左" << endl;
break;
case 77:
cout << "右" << endl;
break;
default:
break;
}
}
}
从文件中读取:
freopen("文件名.in","r",stdin);
往文件中导入:
CPPfreopen("文件名.out","w",stdout);
二进制转换为二进制格雷码保留二进制最高位作为各类码的最高位,最高位和次高位异或为格雷码次高位以此类推……
Int 2147483647位
bool 2B
int 4B
char 4B
flout 4B
string 8B
double 8B
long long 8B
Sleep(毫秒);计时
常量名 对应按键 取值
VK_LBUTTON 鼠标左键 0x01
VK_RBUTTON
鼠标右键 0x02 VK_CANCEL
Ctrl + Break 0x03 VK_MBUTTON
鼠标中键 0x04 VK_BACK Backspace 键 0x08 VK_TAB Tab键 0x09 VK_RETURN 回车键 0x0D VK_SHIFT Shift 键 0x10 VK_CONTROL
Ctrl 键 0x11 VK_CAPITAL
Caps Lock 键 0x14 VK_MENU
Alt 键 0x12 VK_PAUSE
Pause 键 0x13 VK_ESCAPE
Esc 键 0x1B VK_SPACE 空格键 0x20 VK_NEXT Page Down 键 0x22 VK_END
End 键 0x23 VK_HOME
Home 键 0x24 VK_LEFT
左箭头键 0x25 VK_UP
上箭头键 0x26 VK_RIGHT
右箭头键 0x27 VK_DOWN
下箭头键 0x28 VK_SNAPSHOT Print
Screen 键 0x2C VK_Insert
Insert 键 0x2D VK_Delete
Delete 键 0x2E '0' – '9' 数字 0 - 9
0x30 - 0x39 'A' – 'Z' 字母 A - Z
0x41 - 0x5A VK_LWIN
左WinKey(104键盘才有) 0x5B VK_RWIN
右WinKey(104键盘才有) 0x5C VK_APPS
AppsKey(104键盘才有) 0x5D VK_NUMPAD0
小键盘 0 键 0x60 VK_NUMPAD1
小键盘 1 键 0x61 VK_NUMPAD2
小键盘 2 键 0x62 VK_NUMPAD3
小键盘 3 键 0x63 VK_NUMPAD4
小键盘 4 键 0x64 VK_NUMPAD5 小键盘 5 键 0x65 VK_NUMPAD6
小键盘 6 键 0x66 VK_NUMPAD7
小键盘 7 键 0x67 VK_NUMPAD8
小键盘 8 键 0x68 VK_NUMPAD9
小键盘 9 键 0x69 VK_F1 - VK_F24
功能键F1 – F24 0x70 - 0x87 VK_NUMLOCK Num
Lock 键 0x90 VK_SCROLL
Scroll Lock 键 0x91 VK_PRIOR Page Up 键 0x21 If(KEY_DOWN(上表常量名一栏)) 如此程序为检测鼠标左键: #include <windows.h> //GetAsyncKeyState所需头文件 #include #define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0) //用来检测按键的点击事件
鼠标右键 0x02 VK_CANCEL
Ctrl + Break 0x03 VK_MBUTTON
鼠标中键 0x04 VK_BACK Backspace 键 0x08 VK_TAB Tab键 0x09 VK_RETURN 回车键 0x0D VK_SHIFT Shift 键 0x10 VK_CONTROL
Ctrl 键 0x11 VK_CAPITAL
Caps Lock 键 0x14 VK_MENU
Alt 键 0x12 VK_PAUSE
Pause 键 0x13 VK_ESCAPE
Esc 键 0x1B VK_SPACE 空格键 0x20 VK_NEXT Page Down 键 0x22 VK_END
End 键 0x23 VK_HOME
Home 键 0x24 VK_LEFT
左箭头键 0x25 VK_UP
上箭头键 0x26 VK_RIGHT
右箭头键 0x27 VK_DOWN
下箭头键 0x28 VK_SNAPSHOT Print
Screen 键 0x2C VK_Insert
Insert 键 0x2D VK_Delete
Delete 键 0x2E '0' – '9' 数字 0 - 9
0x30 - 0x39 'A' – 'Z' 字母 A - Z
0x41 - 0x5A VK_LWIN
左WinKey(104键盘才有) 0x5B VK_RWIN
右WinKey(104键盘才有) 0x5C VK_APPS
AppsKey(104键盘才有) 0x5D VK_NUMPAD0
小键盘 0 键 0x60 VK_NUMPAD1
小键盘 1 键 0x61 VK_NUMPAD2
小键盘 2 键 0x62 VK_NUMPAD3
小键盘 3 键 0x63 VK_NUMPAD4
小键盘 4 键 0x64 VK_NUMPAD5 小键盘 5 键 0x65 VK_NUMPAD6
小键盘 6 键 0x66 VK_NUMPAD7
小键盘 7 键 0x67 VK_NUMPAD8
小键盘 8 键 0x68 VK_NUMPAD9
小键盘 9 键 0x69 VK_F1 - VK_F24
功能键F1 – F24 0x70 - 0x87 VK_NUMLOCK Num
Lock 键 0x90 VK_SCROLL
Scroll Lock 键 0x91 VK_PRIOR Page Up 键 0x21 If(KEY_DOWN(上表常量名一栏)) 如此程序为检测鼠标左键: #include <windows.h> //GetAsyncKeyState所需头文件 #include #define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0) //用来检测按键的点击事件
int main() {
CPP//----------移除快速编辑模式(对于win10用户)----------
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin, &mode);
mode &= ~ENABLE_QUICK_EDIT_MODE;
SetConsoleMode(hStdin, mode);
//----------循环检测----------
while(1) { //循环检测
if(KEY_DOWN(VK_LBUTTON)) { //鼠标左键按下
printf("key down!\n");
}
Sleep(20); //等待20毫秒,减少CPU占用
}
return 0;
}
鼠标位置:
#include <windows.h> //所需头文件
#include
POINT p;
int main() {
//----------循环检测----------
while(1) {
GetCursorPos(&p); //获取鼠标在屏幕上的位置
printf("(%d,%d)\n",p.x,p.y);
Sleep(20); //等待20毫秒,减少CPU占用
}
return 0;
}
鼠标点击位置:
#include <windows.h> //GetAsyncKeyState所需头文件
#include
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0) //用来检测按键的点击事件
POINT p;
int main() {
CPP//----------移除快速编辑模式(对于win10用户)----------
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin, &mode);
mode &= ~ENABLE_QUICK_EDIT_MODE;
SetConsoleMode(hStdin, mode);
//----------循环检测----------
while(1) { //循环检测
if(KEY_DOWN(VK_LBUTTON)) { //鼠标左键按下
GetCursorPos(&p); //获取鼠标在屏幕上的位置
printf("key down (%d,%d)\n",p.x,p.y);
}
Sleep(100); //等待100毫秒,减少CPU占用
}
return 0;
}
鼠标控制绘图:
#include <windows.h> //GetAsyncKeyState所需头文件
#include
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0) //用来检测按键的点击事件
POINT p;
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
HWND h=GetForegroundWindow();
CONSOLE_FONT_INFO consoleCurrentFont;
//----------移动光标----------
void gotoxy(int x, int y) {
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
SetConsoleCursorPosition(hOut, pos);//两个参数分别是指定哪个窗体,具体位置
}
//----------隐藏光标----------
void HideTheCursor() {
CONSOLE_CURSOR_INFO cciCursor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CPPif(GetConsoleCursorInfo(hStdOut, &cciCursor)) {
cciCursor.bVisible = FALSE;
SetConsoleCursorInfo(hStdOut, &cciCursor);
}
}
int main() {
HideTheCursor(); //隐藏光标
CPP//----------移除快速编辑模式(对于win10用户)----------
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin, &mode);
mode &= ~ENABLE_QUICK_EDIT_MODE;
SetConsoleMode(hStdin, mode);
//----------循环检测----------
while(1) { //循环检测
if(KEY_DOWN(VK_LBUTTON)) { //鼠标左键按下
POINT p;
GetCursorPos(&p);
ScreenToClient(h,&p); //获取鼠标在窗口上的位置
GetCurrentConsoleFont(hOutput, FALSE, &consoleCurrentFont); //获取字体信息
int x=p.x/=consoleCurrentFont.dwFontSize.X;
int y=p.y/=consoleCurrentFont.dwFontSize.Y;
gotoxy(x,y); //移动光标置鼠标位置
printf("@"); //在鼠标位置输出
}
Sleep(10); //延时
}
return 0;
}
全员变色:
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_颜色 | FOREGROUND_INTENSITY);
可以改变字的颜色又能输出菱形:
#include<bits/stdc++.h>
#include<windows.h>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
using namespace std;
int main(){
cout<<"请输入你要的颜色\n1.blue(蓝色) 2.red(红色) 3.white(白色) 4.green(绿色) 5.grey(灰色)\n";
int n;
cin>>n;
system("cls");
if(n==1){
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
}
if(n==2){
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
}
if(n==4){
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
}
if(n==5){
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_INTENSITY);
}
/for(int i=1;i<=133;i++){
cout<<"";
}/
cout<<"按下回车关闭";
Sleep(3000);
system("cls");
bool flag=0;
for(int i=0;i>=0;i++){
if(KEY_DOWN(VK_RETURN)){
system("cls");
cout<<"y";
Sleep(300);
system("cls");
cout<<"o";
system("cls");
Sleep(300);
system("cls");
cout<<"u";
Sleep(300);
system("cls");
cout<<"游";
Sleep(300);
system("cls");
cout<<"游x";
Sleep(300);
system("cls");
cout<<"游戏";
Sleep(300);
system("cls");
cout<<"游戏ov";
Sleep(300);
system("cls");
cout<<"游戏over";
Sleep(20);
return 0;
}
if(i%2==0){
for(int j=1;j<=i;j++){
cout<<"";
Sleep(0.02);
}
for(int j=1;j<=120-i-i;j++){
cout<<" ";
Sleep(0.02);
}
for(int j=1;j<=i;j++){
cout<<"";
Sleep(0.02);
}
}
else{
for(int i=1;i<=120;i++){
cout<<"";
}
}
cout<<"\a";
if(i+i==120)flag=1;
if(flag==1)i-=2;
if(i==0)flag=0;
cout<<endl;
}
CPPreturn 0;
}
约瑟夫问题:
#include<bits/stdc++.h>
using namespace std;
bool a[105];
int main(){
int n,m,i=0;
cin>>n>>m;
int k=n,o=0;
while(k>0){
i++;//报的数
o++;//报数的人的编号
if(a[o]==1){
i--;
}
if(a[o]==0){
if(i==m){
a[o]=1;
cout<<o<<" ";
k--;
i=0;
}
}
if(o==n){
o=0;
}
}
return 0;
}
vector
名称 作用
push_back()
是在容器尾部添加新的元素
push_front()
它是在容器开头增加新的元素
emplace_back()
是在容器尾部添加新的元素
insert()
用于可以在任意位置插入数据
accumulate
在0的基础上计算和,
size
计算总空间个数
…… ……
见c++一本通 P224页
字样大全:
CPPcout << "\033[2m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[3m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[4m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[6m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[7m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[8m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[9m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[21m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[30m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[31m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[32m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[33m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[34m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[35m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[36m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[41m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[42m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[43m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[44m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[45m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[46m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[47m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[53m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[90m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[91m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[92m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[93m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[94m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[95m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[96m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[100m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[101m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[102m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[103m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[104m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[105m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[106m" << "■宋氏集团■" << "\033[0m" << endl;
cout << "\033[107m" << "■宋氏集团■" << "\033[0m" << endl;
全部头文件:#pragma once
//网站
//算法
#include
#include
//原子操作
#include
//C 库包装器
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//概念
#include
//序列容器
#include
#include
#include<forward_list>
#include
#include
//无序的关联容器
#include<unordered_map>
#include<unordered_set>
//容器视图
#include
//错误和异常处理
#include
#include
#include
#include<system_error>
//常规实用工具
#include
#include
#include
#include
#include
#include
#include
#include<memory_resource>
#include
#include
#include<scoped_allocator>
#include
#include<type_traits>
#include
#include
#include
//I/O 和格式设置
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//迭代器
#include
//语言支持
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include<initializer_list>
#include
#include
#include
#include
//数学和数字
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//内存管理
#include
#include
#include<memory_resource>
#include
#include<scoped_allocator>
//多线程处理
#include
#include<condition_variable>
#include
#include
#include<shared_mutex>
#include
//范围
#include
//正则表达式
#include
//字符串和字符数据
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include<string_view>
//时间
#include
#include
C++标准库提供了大量的头文件,涵盖了各种功能,包括输入输出、字符串处理、数学运算、内存管理、多线程等。以下是一些常用的C++头文件及其功能:
输入输出相关:
:用于数据流的输入和输出操作,包含了很多用于控制台输入和输出的类和函数。
:用于文件输入输出操作。
:用于字符串流的操作。
字符串处理相关:
:定义了std::string类,用于处理字符串。
:提供了一系列字符串处理函数。
数学运算相关:
:包含各种数学函数,如三角函数、对数函数等4。
:提供浮点数处理函数2。
内存管理相关:
:提供智能指针和内存分配工具3。
:提供动态数组的功能,可以存储任意类型的元素,并自动管理内存。
多线程相关:
:提供多线程编程的基本支持。
:提供互斥锁等同步机制3。
其他常用头文件:
:字符处理。
:定义错误码。
:定义各种数据类型最值的常量。
:定义本地化函数。
:处理日期和时间。
:定义杂项函数及内存分配函数。
:字符串处理函数。
:提供函数对象、函数指针、以及标准库函数适配器的支持。
这些头文件为C++编程提供了丰富的功能支持,帮助开发者高效地实现各种功能需求。
二进制转换为十进制:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,x=0,cnt=0;
cin>>n;
if(n==0){
cout<<0;
return 0;
}
while(n>0){
if(n%2==0){
cnt++;
}
else{
break;
}
n/=2;
}
while(n>0){
x=x*10+n%2;
n/=2;
}
cout<<x;
for(int i=1;i<=cnt;i++){
cout<<0;
}
return 0;
}
相关推荐
评论
共 0 条评论,欢迎与作者交流。
正在加载评论...