Skip to content

Instantly share code, notes, and snippets.

@littleblacklb
Last active August 13, 2021 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save littleblacklb/824989964309e7f07adfe1de4a29b11c to your computer and use it in GitHub Desktop.
Save littleblacklb/824989964309e7f07adfe1de4a29b11c to your computer and use it in GitHub Desktop.
Cpp常用函数&基本知识点

C++ 常用函数&基本知识点

《信息学奥赛》上所做的笔记,不完整,没写完,~~就是不想写太费劲了,这点破东西就写了七八个小时。~~主要当复习或者当手册或者缓冲下,如果笔记有啥问题或者需求可以随时找我:)

更新地址(当然我知道是几乎不可能有人会看我这东西的**:(** ): https://gist.github.com/LittleBlacklb/824989964309e7f07adfe1de4a29b11c

char

Input

  • char getchar()
    • 从console中获取一个字符
    • 输入数字也按照字符处理: 3 -> char c=getchar(); -> c: '3'
    • char c = getchar();
  • while(cin>>char_varible)
    • 每次读取一个字符
    • cin >> noskipws: 读取空白字符(\0) (默认不返回 空格\n)

Output

  • void putchar(char ch)
    • ch可以为十六进制,整数,都输出为ASCII字符,也可以用转义符('x42'输出为'B')

string

Input

  • void gets(string v)
    • 读取一行字符直到\n
    • "hello world" -> gets(s1) -> s1: "hello world"
  • int scanf("%s%s...", string_variable, string_variable, ... )
    • 读取到空格or换行符,
    • 返回值: 如果读取完毕(\n)则返回0,否则返回1
      • ==一个读取行的方法:== "hello world" -> while(scanf("%s", str) == 1)
        1. loop: str: "hello"
        2. loop: str: "world"
    • 字符串前不加&取地址符,因为string默认就是个数组,本身就是地址 (反例scanf("%s", &str))
    • "hello world" -> scanf("%s", s1) -> s1: "hello"
  •   std::cin >> string_varible
    • 读取一行文本,或者读取到空格

Output

  • int printf(%s, string_variable)
    • %s只能为字符串(字符数组)类型的变量,输出数组元素应为%d
    • 返回输出字符的字节数, 如果输出失败则返回EOF==(untested)==
  • int	puts(string_variable)
    • 等价于printf("%s\n", string_variable)
    • 返回输出字符的字节数, 如果输出失败则返回EOF==(untested)==
  •   std::cout << string;
    •   endl; // 换行符
        

String handler

function explanation return
strcat(str1, str2) str1 + str2 str1 + str2
strncat(str1, str2, int n) connect the first n characters of str2 to str1 str1 + str2[:n]
strcopy(str1, str2) copy str2 to str1 str1
strncopy(str1, str2,int n) copy the first n characters of str2 to str1 str1
strcmp(s1, s2) cmp a and b (sum all characters' ascii) s1>s2: positive integer; s1=s2: zero; s1<s2:negative integer
strncmp(str1, str2, int n) cmp a and the first n characters of b (sum all characters' ascii) ditto
strlen(str) get the lens of str (without \n) size of str
strlwr(str) make str to lowercase str after lowercase
strupr(str) make str to uppercase str after uppercase

Array

Initialization

  1. int a[100];
    • 申请长度为100的连续空间

    • 如果大小必须为一个常量(const N = 100; int a[N])

    • 该语句放在不同的位置,数组的值也不相同

      • 放在main方法外则默认所有元素初始化为0
      • 放在main方法内,所有元素的数值为随机
  2. int a[3] = {1, 2, 3};
  3. int a[3] = {1, 2}; //最后一个元素初始化为0
  4. int a[3] = {}; //默认初始化所有元素为0

Function

  • void memset(void *s, int c, size_t n)
    • 将某一内存块的区域填充指定的值,它对较大的结构体或数组进行清零操作的一种最快方法
    • 参数说明:
      • s: 内存块地址
      • int: 欲填充字符
      • n: 覆盖长度
    • int a[100]; memset(a, 0, sizeof(a));

FILE

Redirect

  • FILE * freopen(const char * filename, const char * mode, FILE * stream);
    • 参数说明:
      • filename: 文件名(如: "ans.out")
      • mode: 文件打开模式(如: "r", "w" )
      • stream: 欲重定向流(如: "stdin", "stdout", "stderr"(错误流))
  • 可以通过fclose(stdin); fclose(stdout);来关闭文件流

  • 可以通过freopen("CON", "r", stdin); freopen("CON", "w", stdout);来恢复到控制台输入&输出

  • 专门用于重定向输入流(包括 scanf()、gets() 等)和输出流(包括 printf()、puts() 等)。值得一提的是,该函数也可以对 C++ 中的 cin 和 cout 进行重定向。

fropen()

FILE *fin, *fout;// FILE对象申请空间
fin = fopen("filename", "rb");
fout = fopen("filename", "wb");
 
// 输入 & 输出
int varible;
fscanf(fin, "%d", varible)
fprintf(fout, "%d", varible);

// 关闭文件流
fclose(fin);
fclose(fout);

File stream

库文件: #include<fstream> (已经导入stdc++.h可以无视)

// 声明流
ifstream fin("filename");
ofstream fout("filename");

// 输入 & 输出
string str;
fin>>str;
fout<<str<<endl;

// 关闭文件流
fin.close();
fout.close();

在调试的时候,希望从控制台输入输出数据,可以先将声明语句注释,然后写入以下代码:

#include<...>

#define fin cin
#define fout cout

int main() {...}

Pointer

指针本质上就是用于储存内存地址的变量,从而进行间接寻址.

Definition

symbol explanation
* 指针标记符
& 取地址标识符
NULL 零指针 相当于0

Variables

Initialization

  1. int a;
    int *p = &a; // int* p is also valid
  2. int a;
    int *p;
    p = &a;
  3. int *p = NULL;
  4. int *p = new(int); // allocate a space to p, *p's content can't be confirmed.

operation

int a=0, b=3;
int *pa = &a, *pb = &b;

/* Assignment */
// p == 100 is invalid, because p is a address, so it need to use '*' to access the value
*p = 100;
// equivalent to
a=100;

/* Calculation */
int sum = *pa * *pb;
// equivalent to
sum = a * b;

*可以理解为告诉编译器,当前这个变量是个地址,要通过这个地址一次才能访问到(间接寻址),所以如果某个变量前有N个*,也就是要寻址N次才能访问到数据,否则还是地址(在后面的Multiple Pointer会提到)

Pointer's + - calculation

指针变量内容的是内存地址, 加减运算一般是配合数组操作的.

int arr[] = {2, 2, 3, 3};
int *p = &a[0];
for (int i=0; i<sizeof(arr)/sizeof(int); i++) {
    std::cout<<*p<<" ";
    p++;
}
std::cout<<std::endl;
for (int i=0; i<sizeof(arr)/sizeof(int); i++) {
    std::cout<<*p<<" ";
    p--;
}

因为数组是个连续空间, 所以这里的p++, p--, 不是单纯的地址+1, 而是通过增加sizeof(int)来跳过整个整数空间,进入下一个整数空间.

None type pointer

有时候,一个指针根据不同的情况,指向的内容是不同类型的值,所以可以定义一个无类型(void)指针

int i = 22;
float f = 3.3;
void *p;
p=&a;
std::cout<<*(int*)p<<std:endl;
p=&b;
std::cout<<*(float*)p<<std:endl;

P.S. 请务必明确pointer指向空间的数据类型, 因为不同数据类型不仅大小不相同,而且存储格式也不相同。如果格式搞错会导致结果怪异.

Multiple pointer

指针本身也是一个type

C++ 允许递归地指向指针的指针——多重指针

// 地址为无中生有的 用于方便理解
int a = 10; // 0x1
int *p; // 0x2
int **pp; // 0x3
p = &a; // 0x2 -> 0x1
pp = &p; // 0x3 -> 0x2 -> 0x1
printf("%d=%d=%d", a, *p, **pp); // opt: 10=10=10

这样应该就可以更好的理解*的意义了8

P.S. 在OI上主要的应用是动态多维数组.

Other

ASCII

[0-9]: 48-57

[A-Z]: 65-90

[a-z]: 97-122

Input

  • int scanf("%d%d...", v1, v2, ... )
    • 读取文本
    • ==返回值==: 如果读取完毕(\n)则返回0,否则返回1
    • 关于字符串详见string
    • 格式符: P34 -> table: formatter

Universal header file

#include <bits/stdc++.h>

stdc++.h

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment