目录
[toc]
编译器和预处理器(回到目录)
- 编译器就是把源代码翻译成目标代码的工具,目标代码可以是机器码,也可以是其他代码。
- 预处理器就是在代码交给编译器处理前,预先进行一些处理,比如包含头文件,宏展开等等。
typedef使用
基本解释
typedef为C语言的关键字,作用是为一种数据类型定义一个新名字。这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等)。
在编程中使用typedef目的一般有两个,一个是给变量一个易记且意义明确的新名字,另一个是简化一些比较复杂的类型声明。
用法
(1)用typedef声明一个新类型名来代替已有的类型名。如:
这样下面代码等价:
(2)用typedef对数组类型起新名:
(3)对一个结构体类型声明一个新名字:
新声明的新类型名TIME代表上面指定的一个结构体类型,这样就可以用TIME定义该结构体变量,如:
注意点
(1)用typedef只是对已经存在的类型增加一个类型名,而没有创造一个新的类型。只是增加了一个新名字,可以用该名字定义变量,比如使用上文中的Status定义变量i;则i变量的类型为int型。
(2)可以用typedef声明新类型名。但是不能用来定义变量
优点
使用typedef类型名,有利于程序的移植性。有时程序会依赖硬件特性。比如在某个Cpp系统用2个字节存一个int类型变量,用4个字节存一个long类型变量。而在另一个Cpp系统中以4个字节存放int类型变量。则把一个Cpp程序从一个用2个字节存一个int类型变量的C++系统移植到以4个字节存放int类型变量的Cpp系统时,如果原来用typedef声明int类型,则例如:
如果不是用typedef声明的,那每一处定义int类型的地方都要改,程序越大,工作量越大。
c++中常见数据结构的基本操作
stack
stack 的基本操作有:
入栈,如例:s.push(x);
出栈,如例:s.pop();注意,出栈操作只是删除栈顶元素,并不返回该元素。
访问栈顶,如例:s.top()
判断栈空,如例:s.empty(),当栈空时,返回true。
访问栈中的元素个数,如例:s.size()。
queue
queue 的基本操作有:
入队,如例:q.push(x); 将x 接到队列的末端。
出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
访问队首元素,如例:q.front(),即最早被压入队列的元素。
访问队尾元素,如例:q.back(),即最后被压入队列的元素。
判断队列空,如例:q.empty(),当队列空时,返回true。
访问队列中的元素个数,如例:q.size()
vector
vector的六种初始化方式
C++的初始化方法很多,各种初始化方法有一些不同。
vector<int> ilist;
默认初始化,vector为空, size为0,表明容器中没有元素,而且 capacity 也返回 0,意味着还没有分配内存空间。这种初始化方式适用于元素个数未知,需要在程序中动态添加的情况。
vector<int> ilist2(ilist);
vector<int> ilist2 = ilist;
两种方式等价 ,ilist2 初始化为ilist 的拷贝,ilist必须与ilist2 类型相同,也就是同为int的vector类型,ilist2将具有和ilist相同的容量和元素.
vector<int> ilist = {1,2,3.0,4,5,6,7};
vector<int> ilist {1,2,3.0,4,5,6,7};
ilist 初始化为列表中元素的拷贝,列表中元素必须与ilist的元素类型相容,本例中必须是与整数类型相容的类型,整形会直接拷贝,其他类型会进行类型转换。
vector<int> ilist3(ilist.begin()+2,ilist.end()-1);
ilist3初始化为两个迭代器指定范围中元素的拷贝,范围中的元素类型必须与ilist3 的元素类型相容,在本例中ilist3被初始化为{3,4,5,6}。注意:由于只要求范围中的元素类型与待初始化的容器的元素类型相容,因此迭代器来自不同的容器是可能的,例如,用一个double的list的范围来初始化ilist3是可行的。另外由于构造函数只是读取范围中的元素进行拷贝,因此使用普通迭代器还是const迭代器来指出范围并没有区别。这种初始化方法特别适合于获取一个序列的子序列。
vector<int> ilist4(7);
默认值初始化,ilist4中将包含7个元素,每个元素进行缺省的值初始化,对于int,也就是被赋值为0,因此ilist4被初始化为包含7个0。当程序运行初期元素大致数量可预知,而元素的值需要动态获取的时候,可采用这种初始化方式。
vector<int> ilist5(7,3);
指定值初始化,ilist5被初始化为包含7个值为3的int
基本操作
尾部插入数字:vec.push_back(a);
使用下标访问元素,cout<<vec[0]<<endl;记住下标是从0开始的。
使用迭代器访问元素.
插入元素:vec.insert(vec.begin()+i,a);
在第i+1个元素前面插入a;
删除元素:vec.erase(vec.begin()+2);
删除第3个元素
vec.erase(vec.begin()+i,vec.end()+j);
删除区间[i,j-1];区间从0开始
向量大小:vec.size()
;
vector也有empty()操作size()等价
清空:vec.clear();
使用reverse将元素翻转:需要头文件#include<algorithm>
reverse(vec.begin(),vec.end());
将元素翻转(在vector中,如果一个函数中需要两个迭代器,
一般后一个都不包含.)
使用sort排序:需要头文件#include<algorithm>
,
sort(vec.begin(),vec.end());
(默认是按升序排列,即从小到大).
可以通过重写排序比较函数按照降序比较,如下:
定义排序比较函数:
调用时:sort(vec.begin(),vec.end(),Comp)
,这样就降序排序。
c++ map基本操作
pair类型
pair类型的定义和初始化
pair类型是在有文件utility中定义的,pair类型包含了两个数据值,通常有以下的一些定义和初始化的一些方法:
上述第一种方法是定义了一个空的pair对象p,第二种方法是定义了包含初始值为v1和v2的pair对象p。第三种方法是以v1和v2值创建的一个新的pair对象。
pair对象的一些操作
除此之外,pair对象还有一些方法,如取出pair对象中的每一个成员的值:
- p.first
- p.second
- 例如:12345678910111213#include <stdio.h>#include <string.h>#include <string>#include <utility>using namespace std;int main(){pair<int, string> p1(0, "Hello");printf("%d, %s\n", p1.first, p1.second.c_str());pair<int, string> p2 = make_pair(1, "World");printf("%d, %s\n", p2.first, p2.second.c_str());return 0;}
map对象的定义和初始化
map是键-值对的组合,有以下的一些定义的方法:
上述第一种方法定义了一个名为m的空的map对象;第二种方法创建了m2的副本m;第三种方法创建了map对象m,并且存储迭代器b和e范围内的所有元素的副本。
map
的value_type
是存储元素的键以及值的pair
类型,键为const
。
map的一些操作
map元素插入
在map中元素有两种插入方法:
使用下标
使用insert函数
在map中使用下标访问不存在的元素将导致在map容器中添加一个新的元素。
insert函数的插入方法主要有如下:
上述的e是一个value_type
类型的值。beg和end标记的是迭代器的开始和结束。
两种插入方法如下面的例子所示:
map中元素的查找和读取
注意:上述采用下标的方法读取map中元素时,若map中不存在该元素,则会在map中插入。
因此,若只是查找该元素是否存在,可以使用函数count(k),该函数返回的是k出现的次数;若是想取得key对应的值,可以使用函数find(k),该函数返回的是指向该元素的迭代器。
上述的两个函数的使用如下所示:
从map中删除元素
从map中删除元素的函数是erase(),该函数有如下的三种形式:
第一种方法删除的是m中键为k的元素,返回的是删除的元素的个数;第二种方法删除的是迭代器p指向的元素,返回的是void;第三种方法删除的是迭代器b和迭代器e范围内的元素,返回void。
如下所示:
c++万能头文件
#include <bits/stdc++.h>
C++的printf方法%f保留小数位处理
例如printf(“%7.2f”,a);结果保留2位,在f前面加上“整数位保留个数.小数位保留个数”
固定 m 位小数 如此: printf(“%.mf”,a);
例如 printf(“%.2f”,a); 保留小数点后两位
要保留1位小数点:printf(“%.1f”,a);
要保留2位小数点:printf(“%.2f”,a);
要保留3位小数点:printf(“%.3f”,a);
……
如此类推。
%8.3f。就是:占8个字符的位置,小数点后面有3位。注意小数点也算一位
c语言中计算字符串长度
- strlen():纯长度,不包括‘/0’
- sizeof():包括‘/0’
set集合
- 一个vector丢进set里面,set会把vector去重。set容器中只能存储键,是单纯的键的集合,其中键是不能重复的。
- set支持大部分的map的操作,但是set不支持下标的操作。
set容器的简单操作
1、set对象的定义和初始化
set对象的定义和初始化方法包括:
例如:
注意:键是不能重复的。
2、set中数据的插入
与map不同,set中数据只能通过insert()函数进行插入。
例如:
3、从set中查找和读取元素
从set中查找同样可以使用count()函数和find()函数,两者的区别在之前的map中已经总结。
例如:
4、从set中删除元素
从set中删除元素使用到的函数是erase()函数,主要有以下的几种形式:
其中,p表示的迭代器指向的元素,b和e分别是迭代器的开始和结束。
例如:
|
|
C++中substr函数的用法
|
|
上述代码获得字符串s中 从第0位开始的长度为5的字符串.默认时的长度为从开始位置到尾
输出结果为:12345
c++ 大小写转换&&字符转数字
|
|