C++ 常见头文件学习

  • #include + using namespace std;

set st

添加:insert();

删除:erase();

查找 用迭代器,set::iterator it=st.begin()

for(set::iterator it=st.begin();it!=st.end();it++){....}

数量:size()

清零:clear()

  • #include (注意不是string.h)+ using namespace std;

string str="abcdefg";

单个元素,作为字符访问:printf("%c",str[i]);//注意是%c

长度:str.length();

读和输出string,用下的cin和cout:cin>>str;

转为字符数组:str.c_str();

子串:substr(pos,len)

查找子串:str.find(str2),找不到时返回string::npos

替换:str.replace(pos,len,str2),把str的pos开始的len个长度,替换为str2,注意str2的长度和len无关!!!! 

  • #include+using namespace std;

queue q;

队首:q.front();

队尾:q.back();

入队:q.push();

出队:q.pop();

判空:empty();

  • #include+using namespace std;

stack stk;

栈顶元素:stk.top();

入栈:stk.push();

出栈:stk.pop();

判空:stk.empty();

元素个数:stk.size();

  • #include+using namespace std;

min()、max()、swap()

逆序:reverse(it1,it2);逆序,将[it1,it2)之间的元素反转,it1和it2是迭代器的数组指针,例如reverse(a,a+4);//a是数组名

全排列:next_permutation(it1,it2);是原地进行,达到最后一种全排列时,返回false

#include
#include
using namespace std;

int main(void){
    int a[10]={1,2,3};
    do{
        printf("%d%d%d",a[0],a[1],a[2]);
    }while(next_permutation(a,a+3));//输出123,132,213,231,312,321
    
    return 0;
}

填充:fill();//与memset不同,可以赋任意值!!!!!

#include
#include
using namespace std;

int main(void){
    int a[5]={1,2,3,4,5};
    fill(a,a+3,233);
    return 0;//则,a变为:233,233,233,4,5
}

排序:sort(元素首地址,尾地址的下一元素地址,比较函数(选填) );

比较函数:bool cmp(类型 a,类型 b);

#include
#include
using namespace std;
struct node {
	int x;
	int y;
}nodes[10];
bool cmp(node a, node b) {
	if (a.x != b.x) return a.x > b.x;//a.x>b.x时,a在b的前面
	else return a.y < b.y;//a.y

 

你可能感兴趣的:(C++ 常见头文件学习)