一.杂乱语法
1.struct里不让定义函数,因此不能不直接定义cmp
1)结构体内嵌比较函数:
bool operator <(const node &a)const// operator的目的就是重载"<"或者其他符号。
每次往结构体a[i]里赋值,struct自动进行比较,排序,最后得到一个有序的结构体
2)struct自动从小到大排序 :
bool operator < (const node &a)const{
return r < a.r;
}
struct从大到小排序:
bool operator > (const node &a)const{
return r > a.r;
}
注:operator后符号与下面return后符号一致
(const node &a)const :不能改变原有的值,只是换顺序
2. sort函数
①如果不传入第三个参数,则默认是升序排列。
sort(a, a + 10); // 10为元素个数
②降序排列,需传入第三个参数-比较函数greater<type>()
sort(a, a + 10, greater<int>());
3.大根堆priority_queue<Type, Container, Functional>//默认按照从小到大排列
Type为数据类型,Container为保存数据的容器,Functional为元素比较方式。
小根堆priority_queue <int ,vector<int>, greater<int> >//加上greater<>后,数据从大到小排列
4.heap:代表声明了一个用vector存的int类型的小根堆
Eg: heap.pop()
5.memset():是一个初始化函数,作用是将某一块内存全部设置为指定的值
格式:memset(s, c, sizeof n);//长度为n的s初始化为c
s指向要填充的内存块;c是要被设置的值;n是被设置的长度
注:memset函数是按照字节对内存块进行初始化,所以不能将int数组出初始化为0和-1之外的其他值
6.auto关键字
auto是作为一个新的类型指示符来指示编译器
使用auto定义变量可让编译器自动推导变量类型;
和for使用来遍历数组;
加引用和for一起使用来修改数值。
auto定义变量时必须初始化,auto变量不能作为参数
因此auto并非是一种“类型”的声明,而是一个类型声明时的“占位符”,编译器在编译期会将auto替换为变量实际的类型。
注:不能使用auto和指针结合来修改数组。
7.C++头文件
#include<iostream>:常见用途输入流 " cin>> " 和 输出流 " cout<< "
#include<algorithm>:C++的标准算法库
常见用途求最大值 "max()" , 求最小值 "min()" ,排序函数 "sort()" 等
#include<cstdio.h>:格式化输入 "scanf()" 和格式化输出 "printf()" ,功能丰富,但是相比
起"cin"和"cout"略显繁琐
#include<cmath>:包含了大量的数学公式,方便计算.
常见用途求绝对值 "abs()" ,正弦值"sin()" 等
#include<windows.h>: 常见用途 "GetAsyncKeyState()()"判断某个键是否被按下.
"system("cls")" 清空屏幕.
"Sleep()" 延迟(单位:毫秒)
8.数组初始化函数 "memset()" 插入字符串"insert()" , 去除字符串"erase()
9.using namespace std:释放std命名空间中的变量名,函数名以及类型名,能让编译器准确的找到相应的函数或者对象
using是正在使用的意思。
namespace 的引用是为了解决不同space中命名相同导致命名冲突的问题。
std 是个名称空间标识符,C++标准库中的函数或者对象都是在命名空间std中定义的
eg:cin,cout是预定义在std命名空间的一种类对象名,只有在std命名空间下,编译器才能正常执行输入输出作用
同一个域中不能出现两个重名的变量名/函数名,命名空间就是对标识符的名称进行本地化,以此解决命名污染的问题,其作用就是定义一个新的作用域
10.unordered_map和map
map 容器中存储的数据是有序的,
unordered_map 容器中是无序的,只是根据key的哈希值,将元素存在指定位置
unordered_map<key,value>
//根据单个key值查找对应的value,key值是唯一的,key和value的数据类型可以不相同。
11.C++中常见编译错误:
fatal error C1003: error count exceeds number; stopping compilation 中文对照:错误太多,停止编译 fatal error C1004: unexpected end of file found error C2001: newline in constant error C2006: #include expected a filename, found 'identifier' error C2007: #define syntax error C2008: 'xxx' : unexpected in macro definition error C2009: reuse of macro formal 'identifier' error C2010:'character' : unexpected in macro formal parameter list error C2014: preprocessor command must start as first nonwhite space error C2015: too many characters in constant error C2017: illegal escape sequence error C2018: unknown character '0xhh' error C2019: expected preprocessor directive, found 'character' error C2021: expected exponent value, not 'character' error C2039: 'identifier1' : is not a member of 'idenifier2' error C2048: more than one default error C2050: switch expression not integral error C2051: case expression not constant error C2052: 'type' : illegal type for case expression error C2057: expected constant expression error C2058: constant expression is not integral error C2059: syntax error : 'xxx' error C2064: term does not evaluate to a function error C2065: 'xxx' : undeclared identifier error C2078: too many initializers error C2082: redefinition of formal parameter 'xxx' error C2084: function 'xxx' already has a body error C2086: 'xxx' : redefinition error C2087: '' : missing subscript error C2100: illegal indirection error C2105: 'operator' needs l-value error C2106: 'operator': left operand must be l-value error C2110: cannot add two pointers error C2118: negative subscript or subscript is too large error C2124: divide or mod by zero error C2133: 'xxx' : unknown size error C2137: empty character constant。 error C2143: syntax error : missing 'token1' before 'token2' error C2146: syntax error : missing 'token1' before identifier 'identifier' error C2144: syntax error : missing ')' before type 'xxx' error C2181: illegal else without matching if error C2196: case value '0' already used error C2296: '%' : illegal, left operand has type 'float' error C2297: '%' : illegal, right operand has type 'float' error C2371: 'xxx' : redefinition; different basic types error C2440: '=' : cannot convert from 'char ' to 'char' error C2447: missing function header (old-style formal list?) error C2448: '' : function-style initializer appears to be a function definition error C2450: switch expression of type 'xxx' is illegal error C2466: cannot allocate an array of constant size 0 error C2105: 'operator' needs l-value error C2106: 'operator': left operand must be l-value error C2110: cannot add two pointers error C2118: negative subscript or subscript is too large error C2124: divide or mod by zero error C2133: 'xxx' : unknown size error C2137: empty character constant。 error C2143: syntax error : missing 'token1' before 'token2' error C2146: syntax error : missing 'token1' before identifier 'identifier' error C2144: syntax error : missing ')' before type 'xxx' error C2181: illegal else without matching if error C2196: case value '0' already used error C2296: '%' : illegal, left operand has type 'float' error C2297: '%' : illegal, right operand has type 'float' error C2371: 'xxx' : redefinition; different basic types error C2440: '=' : cannot convert from 'char ' to 'char' error C2447: missing function header (old-style formal list?) error C2448: '' : function-style initializer appears to be a function definition error C2450: switch expression of type 'xxx' is illegal error C2466: cannot allocate an array of constant size 0 error C2601: 'xxx' : local function definitions are illegal error C2632: 'type1' followed by 'type2' is illegal error C2660: 'xxx' : function does not take n parameters error C2676: binary '>>' : 'class ostream_withassign' does not define this operator or a conversion to a type acceptable to the predefined operator error C4716: 'xxx' : must return a value fatal error LNK1104: cannot open file "Debug/Cpp1.exe" fatal error LNK1168: cannot open Debug/Cpp1.exe for writing fatal error LNK1169: one or more multiply defined symbols found error LNK2001: unresolved external symbol _main error LNK2005: _main already defined in Cpp1.obj warning C4067: unexpected tokens following preprocessor directive - expected a newlin 中文对照:预处理命令后出现意外的符号 - 期待新行 warning C4091: '' : ignored on left of 'type' when no variable is declared warning C4101: 'xxx' : unreferenced local variable warning C4244: '=' : conversion from 'type1' to 'type2', possible loss of data warning C4305: 'initializing' : truncation from 'const double' to 'float' warning C4390: ';' : empty controlled statement found; is this the intent? warning C4508: 'xxx' : function should return a value; 'void' return type assumed warning C4553: '==' : operator has no effect; did you intend '='? warning C4700: local variable 'xxx' used without having been initialized warning C4715: 'xxx' : not all control paths return a value 中文对照:函数xxx定义非法 error C2632: 'type1' followed by 'type2' is illegal error C2660: 'xxx' : function does not take n parameters error C2676: binary '>>' : 'class ostream_withassign' does not define this operator or a conversion to a type acceptable to the predefined operator error C4716: 'xxx' : must return a value fatal error LNK1104: cannot open file "Debug/Cpp1.exe" fatal error LNK1168: cannot open Debug/Cpp1.exe for writing fatal error LNK1169: one or more multiply defined symbols found error LNK2001: unresolved external symbol _main error LNK2005: _main already defined in Cpp1.obj warning C4067: unexpected tokens following preprocessor directive - expected a newline 中文对照:预处理命令后出现意外的符号 - 期待新行 warning C4091: '' : ignored on left of 'type' when no variable is declared warning C4101: 'xxx' : unreferenced local variable warning C4244: '=' : conversion from 'type1' to 'type2', possible loss of data warning C4305: 'initializing' : truncation from 'const double' to 'float' warning C4390: ';' : empty controlled statement found; is this the intent? warning C4508: 'xxx' : function should return a value; 'void' return type assumed warning C4553: '==' : operator has no effect; did you intend '='? warning C4700: local variable 'xxx' used without having been initialized warning C4715: 'xxx' : not all control paths return a value 此段装载:http://blog.csdn.net/zml_2015/article/details/46049381 |
12.count函数可以用来统计字符串中某个字符的个数,或数组中某个元素的数量。
使用方法是count(begin,end,‘a’),其中begin指的是起始地址,end指的是结束地址,第三个参数指的是需要查找的字符。
13. pair 类
也是标准库的一部分,它使得我们可以在单个对象内部把相同类型或不同类型的两个值关联起来。为了使用pair 类,我们必须包含的头文件#include <utility>
使用
法一:pair <int, int> a[11000];//创建了一个pair 对象author ,它包含两个整数
eg:pair< string, string > author( "James", "Joyce" );//两个字符串,分别被初始化为 James 和 Joyce 可以用成员访问符号 member access notation 访问pair 中的单个元素,它们的名字为first 和second
法二:typedef pair<int,int> Pii;//定义大量相同pair 类型的对象
eg:pair 一个元素持有对象的名字, 另一个元素持有指向其符号表入口的指针
typedef pair< string, EntrySlot* > SymbolEntry;
eg1:string firstBook;
if ( author.first == "James" && author.second == "Joyce" )
firstBook = "Stephen Hero";
eg2:typedef pair< string, string > Authors;
Authors proust( "marcel", "proust" );
Authors joyce( "james", "joyce" );
Authors musil( "robert", "musil" );
14.setprecision(n)
功能:控制浮点数显示的有效数字个数。
使用控制符,在程序开头加iomanip
#include<iomanip>
15.typedef:用户自定义数据类型名称
typedef 类型名称 类型标识符;
类型名称包括基本数据类型和用户自定义数据类型(eg结构体、枚举类型)
typedef应用:
1>同时声明指针类型的多个变量。
char* a,b;//声明了一个指针a,变量b
char *a,*b;//声明了两个指针
typedef char* ABC;//用大写
ABC a,b;//同时声明了两个指针
2>声明struct对象
C语言中:
struct tag1
{
int x;
int y;
};
struct tag1 p1;
C++中
typedef struct tag2
{
int x;
int y;
}tag2;
tag2 p2;
3>定义与操作系统无关的内容
4>为复杂声明定义一个简单别名