LOADING...

加载过慢请开启缓存(浏览器默认开启)

loading

PTA知识点精讲

2022/3/26 竞赛/算法

带空格的字符串输入(C++)

cin.get(str, len) 函数可以接收空格,遇回车结束输入,将换行符保留。

str用来存储输入行的数组名称

len是要读取的字符数

#include <iostream>
using namespace std;
int main(){
    char a[50];
    cin.get(a,50);
    cout << a <, endl;
    return 0;
}
输入:I love China回车结束输入,输出结果为I love China。


cin.getline(str, len) 函数可以接收空格,遇回车结束输入,将换行符丢弃。

#include <iostream>
using namespace std;
int main(){
    char a[50];
    cin.getline(a,50);
    cout << a << endl;
    return 0;
}
输入:I love China回车结束输入,输出结果为I love China。


gets() 函数以无限读取,以回车结束读取(在c++中运行会产生bug)

#inlcude <iostream>
#inlcude <cstdio>
using namespace std;
int main(){
    char a[50];
    cin >> a;
    gets(a);
    cout << a << endl;
    return 0;
}
输入:I love China回车结束输入,输出结果为love China。首字符自动丢弃。


④ getline() 函数若定义变量为string类型,则要考虑此函数

#include <iostream>
#include <cstring>
using namespace std;
int main(){
    string a;
    getline(cin,a);
    cout << a << endl;
    return 0;
}
输入:I love China回车并未结束输入,需回车两次才能结束输入,输出结果为:I love China.


cin 是C++中最常用的输入语句,当遇到空格或者回车键即停止

#include <iostream>
#include <cstring>
using namespace std;
int main(){
    char a[50];
    cin >> a;
    cout << a << endl;
    return 0;
}
输入:abcd遇回车输出abcd
缺点:只能输入没有空格的字符串,当输入中含有空格,则只能输出空格之前的字符
输入:I love China输入空格时输入并未停止,遇回车输入停止,输出I,空格后面的均未输出。


⑥ 用得到的函数

#include <isotream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include <sstream>
using namespace std;

⑦ 常用的知识点

  • string

    1. erase函数删掉指定位置的字符

      (1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
      (2)erase(position);删除position处的一个字符(position是个string类型的迭代器)
      (3)erase(first,last);删除从first到last之间的字符(first和last都是迭代器)

    2. 截取子串

      s.substr(pos, n) 截取s中从pos开始(包括0)的n个字符的子串,并返回

    3. replace(int i, int num, string s)从第i个位置以后的num个字符替换为s

    4. find

      (1)s.find(string s1) 返回s1在s中第一次出现的下一个位置

      (2)s.find(string s1,int num) 从下标num开始查找s1返回在s中的下标位置

    5. char转string
      char str[10];
      string s(str);//或者是s = str;

    6. string转char

      const char *str;
      string s;
      str = s.c_str();

  • 四舍五入

    double a = 11/2.0;
    int b = (int)(a+0.5);

sort 头文件 #include

默认是从小到大
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    int arr[] = {3,4,7,2,1};
    
    sort(arr,arr+5);
    for(int i = 0; i < 5; i++){
    cout << arr[i];
    }
    return 0;
}

从大到小仅需把第八行改为:
    sort(arr,arr+5,greater<int>());

也可以自造比较函数
    bool cmp(int x, int y){
    return x > y;
}
    sort(arr,arr+5,cmp);
  • 保留两位小数 -> %.2f

  • 比如要将‘8’转换为数字8,在语句中这样写就可以了,“ 8+‘0’ “

  • 字符到数字的转化可以利用s[i] - ‘0’

  • 字符串长度 -> int len = (int)strlen(str); 字符串记得结束符 ‘0’

  • “%”是取尾,“/”是留头 ==> a=1234567 ( a%10 = 7 ,a/10 = 123456)

  •     int a = 123456789;
        for(int i = 0; i < 1; i++)
            printf("%d\n",a/1%10);    =>9
             printf("%d\n",a/10%10);    =>8
            printf("%d\n",a/100%10); =>7
            printf("%d\n",a/1000%10); =>6
        输出每个数字:cout << a %= 10; a /= 10;
    
  • 字符串连接strcat() 字符串复制strcpy()

  • 循环输入终止模板:while(scanf(“%s”, s) != EOF)

PTA空格输出模板

for(int i = 0; i < N; i++){
    if(i != N - 1){
        printf("%lf", num[i]);
    }else{
        printf("%lf\n", num[i]);
    }
}
return 0;