因为要参加ACM比赛,所以将自己之前所遇到的,用到的,一些常见的错误,常用的函数都写出来。
输出超限
到遇到此问题时,不要着急,你的答案很可能是对的,但是输出格式不对,或者可能是下面这种情况:
while ( true ){ cin >> n ; }
和
while (cin >> n) {}
是有很大区别的,本质上利用的是 IO缓冲的特点
在输入没有回车之前,程序的 printf 输出不会显示(ggc和vc也有区别)。
按标准的做法应该保存输入的所有字符直至回车,再一次转换输出。
运行出错
在c++11中,unsigned 和 int 是有区别的
例:
for (unsigned i = 9; i >= 0; i--) {
cout << num[i]<<" ";
}
是一个死循环,因为,unsigned中没有 -1
所以会一直执行下去,而下一位就是
(32位操作系统 16位 0 至 65,535 ,64位操作系统 32位 0至4294967295)最大位数65535.
JAVA 因为外部库多,功能丰富,所以常在ACM中使用,其具体使用格式位:
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner cin = new Scanner (new BufferedInputStream(System.in));
}
}
java常见的类型转换
- 如何将字串 String 转换成整数 int?
方法一:
int i = Integer.parseInt([String]);
i = Integer.parseInt([String],[int radix]);
方法二:
int i = Integer.valueOf(my_str).intValue();
-
注:
- 字串转成 Double, Float, Long 的方法大同小异.
- 如何将整数 int 转换成字串 String ?
方法一:
String s = String.valueOf(i);
方法二:
String s = Integer.toString(i);
方法三:
String s = "" + i;
以下函数是C++ STL中函数
- 设置输出位数
#include <iomanip>
cout << setiosflags(ios::fixed) << setprecision(site)<< num << endl;
- 求绝对值
#include <iostream>
abs(a-b);
- 截取字符串-splite
#include <string>
const char *spilt_char="/";
char *p;
p.strtok(str,spilt_char);
- 排序 sort函数
#include<algorithm>
//默认排序
sort(Rs.begin(),Rs.end());
//模板排序
#include<vector>
sort<vector>(Rs.begin(),Rs.end(),compare);
7 关于排序
bool cmp(int a,int b)
{
return abs(a)>abs(b);
}
sort(vec.begin(),vec.end(),cmp);
8 求字符串长度
strlen(str)
9//cin.getline(字符指针,字符个数N,结束符);
//结束符(默认的是以'\n'结束)
while(cin.getline(a,100))
10 字符串比较 //strcmp(字符串1,字符串2)
//s1<s2 <0 ; s1=s2 0 ;s1>s2 >0
- string对象取地址
&str.at();
- string怎么通过cin获取一行
string str;
getline(cin, str);
cout << str << endl;
或
string str;
get(str,length); //按指定长度获取
- string对象中可存放的最大字符串的长度
int max_size();
- 返回当前字符串的大小
int size();
- 返回当前字符串的长度
int length();
- 当前字符串是否为空
bool empty();
- 把字符串当前大小置为len,并用字符c填充空白的部分
void resize(int len,char c);
string的比较:
- 比较两个字符串是否相等
- 运算符”>”,”<”,”>=”,”<=”,”!=”均被重载用于字符串的比较;
bool operator==(const string &s1,const string &s2);
- 比较当前字符串和s的大小
int compare(const string , &s) ;
- 比较当前字符串从pos开始的n个字符组成的字符串与s的大小
int compare(int pos, int n , const string &s)
- 比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小
int compare(int pos, int n , const string &s,int pos2 , int n2);
- compare函数在>时返回1,<时返回-1,==时返回0
int compare(const char *s);
int compare(int pos, int n,const char *s);
int compare(int pos, int n,const char *s, int pos2) const;
string的交换:
- 交换当前字符串与s2的值
void swap(string &s2);
string类的查找函数:
查找成功时返回所在位置,失败返回string::npos的值
- 从pos开始查找字符c在当前字符串的位置
int find(char c, int pos = 0);
- 从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos = 0);
- 从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const char *s, int pos, int n) ;
- 从pos开始查找字符串s在当前串中的位置
int find(const string &s, int pos = 0) ;
反向查找函数
从pos开始从后向前查找字符c在当前串中的位置
其余与正向相同
int rfind(char c, int pos = npos) ;
int rfind(const char *s, int pos = npos);
int rfind(const char *s, int pos, int n = npos);
int rfind(const string &s,int pos = npos) const;