
C++
文章平均质量分 60
柠檬山楂荷叶茶
这个作者很懒,什么都没留下…
展开
-
9-位运算与常用库函数
位运算符号运算&与|或~非……异或>>右移<<左移常用操作:求x的第k位数字 x >> k & 1lowbit(x) = x & -x,返回x的最后一位1常用库函数reverse翻转翻转一个vector:reverse(a.begin(), a.end());翻转一个数组,元素存放在下标1 ~ n:reverse(a + 1, a + n + 1);uniq原创 2022-04-20 19:34:50 · 124 阅读 · 0 评论 -
8-STL(C++)
STL是提高C++编写效率的一个利器。#include < vector >vector是变长数组,支持随机访问,不支持在任意位置 O(1)O(1) 插入。为了保证效率,元素的增删一般应该在末尾进行。声明#include <vector> // 头文件vector<int> a; // 相当于一个长度动态变化的int数组vector<int> b[233]; // 相当于第一维长233,第二位长度动态变化的int数组struct转载 2022-04-18 11:44:31 · 212 阅读 · 0 评论 -
7-类、结构体、指针、引用
类与结构体类的定义:class Person{ private: int age, height; double money; string books[100]; public: string name; void say() { cout << "I'm " << name << endl; }转载 2022-04-18 11:43:24 · 111 阅读 · 0 评论 -
6-C++函数
函数基础一个典型的函数定义包括以下部分:返回类型、函数名字、由0个或多个形参组成的列表以及函数体。编写函数我们来编写一个求阶乘的程序。程序如下所示:int fact(int val){ int ret = 1; while (val > 1) ret *= val -- ; return ret;}函数名字是fact,它作用于一个整型参数,返回一个整型值。return语句负责结束fact并返回ret的值。调用函数int main(){转载 2022-04-18 11:42:23 · 102 阅读 · 0 评论 -
5-字符串
字符与整数的联系——ASCII码每个常用字符都对应一个-128 ~ 127的数字,二者之间可以相互转化:#include <iostream>using namespace std;int main(){ char c = 'a'; cout << (int)c << endl; int a = 66; cout << (char)a << endl; return 0;}常用ASCI转载 2022-04-18 11:40:56 · 165 阅读 · 0 评论 -
4-C++数组
一维数组数组的定义数组的定义方式和变量类似。#include <iostream>#include <algorithm>using namespace std;int main(){ int a[10], b[10]; float f[33]; double d[123]; char c[21]; return 0;}数组的初始化在main函数内部,未初始化的数组中的元素是随机的。#include <ios转载 2022-04-18 11:39:15 · 222 阅读 · 0 评论 -
3-循环结构
循环语句只需要抓住一点——代码执行顺序!while循环可以简单理解为循环版的if语句。if语句是判断一次,如果条件成立,则执行后面的语句;while是每次判断,如果成立,则执行循环体中的语句,否则停止。#include <iostream>using namespace std;int main(){ int i = 0; while (i < 10) { cout << i << endl;转载 2022-04-18 11:36:29 · 1065 阅读 · 0 评论 -
2-printf语句与判断结构
printf输出格式注意:使用printf时最好添加头文件 #include 。#include <iostream>#include <cstdio>using namespace std;int main(){ printf("Hello World!"); return 0;}Int、float、double、char等类型的输出格式(1) int:%d(2) float: %f, 默认保留6位小数(3) double: %lf,转载 2022-04-18 11:34:54 · 513 阅读 · 0 评论 -
1-变量、输入输出、表达式和顺序语句
编写一个简单的C++程序——手速练习#include <iostream>using namespace std;int main(){ cout << "Hello World" << endl; return 0;}语法基础变量的定义变量必须先定义,才可以使用。不能重名。变量定义的方式:#include <iostream>using namespace std;int main(){ int a转载 2022-04-18 11:32:53 · 150 阅读 · 0 评论