STL基础
vector:
1.头文件#include<vector>
2.声明vector对象,vector<int> vec;
3.尾部插入a:vec.push_back(a);
4.使用下标访问元素,cout<<vec[0]<<endl;
5.使用迭代器访问元素:
for( vector<int>::iterator it=vec.begin();it!=vec.end();it++)
cout<<*it<<endl;
6.插入元素: vec.insert(vec.begin()+i,a);在第i+1个元素前面插入a;
7.删除元素: vec.erase(vec.begin()+2);删除第3个元素
vec.erase(vec.begin()+i,vec.end()+j);删除区间[i,j-1];区间从0开始
8.向量大小:vec.size();
9.清空:vec.clear();
10.重载<
1 struct MyObject 2 { 3 int x; 4 int y; 5 6 bool operator< (const MyObject &a) const 7 { 8 if(x!=a.x) 9 return x<a.x; 10 else 11 return y<a.y; 12 } 13 };
tip:关于结构体定义的几种情况:
1 struct Student 2 { 3 string name; 4 }Stu; 5 6 struct 7 { 8 string name; 9 }Stu; 10 11 typedef struct Student 12 { 13 string name; 14 }Stu; 15 16 typedef struct 17 { 18 string name; 19 }Stu;
set: set<int> s;
1.元素插入:insert()
2.遍历:
set<int>::iterator it;
for(it=s.begin();it!=s.end();it++)
4.元素删除
s.erase(2); //删除键值为2的元素
s.clear();
5.元素检索:find(),若找到,返回该键值迭代器的位置,否则,返回最后一个元素后面一个位置。
set<int>::iterator it;
it=s.find(5); //查找键值为5的元素
if(it!=s.end()) //找到
cout<<*it<<endl;
else //未找到
cout<<"未找到";
6.自定义比较函数
(1)元素不是结构体:
//自定义比较函数myComp,重载“()”操作符
1 struct myComp 2 3 { 4 5 bool operator()(const your_type &a,const your_type &b) 6 7 [ 8 9 return a.data-b.data>0; 10 11 } 12 13 }
set<int,myComp>s;
set<int,myComp>::iterator it;
(2)如果元素是结构体,可以直接将比较函数写在结构体内。
1 struct Info 2 3 { 4 5 string name; 6 7 float score; 8 9 bool operator < (const Info &a) const 10 11 { 12 13 return a.score<score; 14 15 } 16 17 }
set<Info> s;
set<Info>::iterator it;
stack:
1.入栈:s.push(x);
2.出栈:s.pop();注意,出栈操作只是删除栈顶元素,并不返回该元素。
3.访问栈顶:s.top()
4.判断栈空:s.empty(),当栈空时,返回true
5.访问栈中的元素个数:s.size()
queue:
1.入队:q.push(x); 将x 接到队列的末端。
2.出队:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
3.访问队首元素:q.front(),即最早被压入队列的元素。
4.访问队尾元素:q.back(),即最后被压入队列的元素。
5.判断队列空:q.empty(),当队列空时,返回true。
6.访问队列中的元素个数:q.size();
1.元素插入:insert()
2.中序遍历:类似vector遍历(用迭代器)
3.反向遍历:利用反向迭代器reverse_iterator。
例:
set<int> s;
......
set<int>::reverse_iterator rit;
for(rit=s.rbegin();rit!=s.rend();rit++)
4.元素删除:与插入一样,可以高效的删除,并自动调整使红黑树平衡。
set<int> s;
s.erase(2); //删除键值为2的元素
s.clear();
5.元素检索:find(),若找到,返回该键值迭代器的位置,否则,返回最后一个元素后面一个位置。
set<int> s;
set<int>::iterator it;
it=s.find(5); //查找键值为5的元素
if(it!=s.end()) //找到
cout<<*it<<endl;
else //未找到
cout<<"未找到";
6.自定义比较函数
(1)元素不是结构体:
例:
//自定义比较函数myComp,重载“()”操作符
struct myComp
{
bool operator()(const your_type &a,const your_type &b)
[
return a.data-b.data>0;
}
}
set<int,myComp>s;
......
set<int,myComp>::iterator it;
(2)如果元素是结构体,可以直接将比较函数写在结构体内。
例:
struct Info
{
string name;
float score;
//重载“<”操作符,自定义排序规则
bool operator < (const Info &a) const
{
//按score从大到小排列
return a.score<score;
}
}
set<Info> s;
......
set<Info>::iterator it;
优先队列:
1.priority_queue<int> qi; //普通的优先级队列,按从大到小排序
2.priority_queue<int, vector<int>, greater<int> > qi2; //从小到大的优先级队列,可将greater改为less,即为从大到小
其他内容:
c语言常用函数:
#include <math.h>
int abs( int x)
double fabs(double x)
double pow(double x,double y)
double sin(double x)
double sqrt(double x)
double log(double x) //Ln
double log10(double x) //Log10
#include <ctype.h>
int isalnum(int ch) //是否字母或数字
int isalpha(int ch) //是否字母
int isdigit(int ch) //是否数字
int islower(int ch)
int tolower(int ch)
int toupper(int ch)
#include <string.h>
char *strcat(char *s1,char *s2) //把字符串s2接到s1后面
char *strchr(char *s,int ch) //在s所指字符串中,找出第一次出现字符ch的位置
int strcmp(char *s1,char *s2) //对s1和s2所指字符串进行比较
char *strcpy(char *s1,char *s2) //把s2指向的串复制到s1指向的空间
unsigned strlen(char *s)
char *strstr(char *s1,char *s2) //在s1所指字符串中,找出字符串s2第一次出现的位置,返回找到的字符串的地址,找不到返回NULL
#include <stdio.h>
FILE *fopen(char *filename,char *mode)
int fclose(FILE *fp)
int getchar(void)
int putchar(char ch)
int fread(char *pt,unsigned size,unsigned n, FILE *fp) //从fp所指文件中读取长度size为n个数据项存到pt所指文件
int fwrite(char *pt,unsigned size,unsigned n, FILE *fp) //把pt所指向的n*size个字节输入到fp所指文件
#include <stdlib.h>
void *malloc(unsigned size)
void *realloc(void *p,unsigned size) // 把p所指内存区的大小改为size个字节
C++常用函数:
algorithm
sort(a,a+20,cmp)
bool cmp( int a, int b )
{ return a>b; }
string _strrev(string str) //返回一个逆置字符串的指针
int test1(){
char s[100];
int len;
while((len=getline_(s,100))>0)
printf("%s\n",s);
return 0;
}
浙公网安备 33010602011771号