C语言学习总结

输出加法程序

#include<stdio.h>

int main()
{
    printf("#include<stdio.h>

");
    printf("int main()
{
");
    printf("    int x,y;
");
    printf("    scanf("%%d%%d",&x,&y);
");
    printf("    printf("%%d\n",x+y);
");
    printf("    return 0;
}
");
    return 0;
}

 文件操作

#include<stdio.h>

int main()
{
    int a,b;
    FILE *fp;
    scanf("%d%d",&a,&b);
    fp=fopen("t1.txt","w");
    fprintf(fp,"%d %d
",a,b);
    fp=fopen("t1.txt","r");
    fscanf(fp,"%d%d",&a,&b);
    fp=fopen("t2.txt","w");
    fprintf(fp,"%d
",a+b);
    fclose(fp);
    return 0;
}

 STL使用

string增删改查

string s="abcde";

s=s.insert(1,"q");  //在1处插入q,"aqbcde"

s=s.erase(1,2);  //删除从1开始的2个字符,"ade"

s=s.replace(1,2,"q");  //替换从1开始的2个字符,"aqde"

cout<<s.find("de",1);  //从1开始查找子串在母串中首次出现的位置,找到后返回第一个字符的下标,"3"
//找不到返回s.npos,4294967295
cout<<s.find_last_of("de"); //查找子串在母串中最后一次出现的位置,找到后返回最后一个字符的下标,"4"

printf("%s ",s.c_str()); //字符串c++转c输出

vector

//插入四个C到vector v[1]中
vector<char> v;
vector<char>::iterator it;
it=v.begin();
v.insert(it+1,4,'C');

(6)插入元素:    vec.insert(vec.begin()+i,a);在第i个元素后面插入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()   //清空之后,vec.size()为0

bool Comp(const int &a,const int &b)
{
    return a>b;
}
调用时:sort(vec.begin(),vec.end(),Comp),这样就降序排序。

set

//定义空的set
set<int> s;
//将vector元素插入到set中
set<int> s(v.begin(),v.end());
//插入一个值
s.insert(x);
//定义set迭代器
set<int>::iterator it;
//遍历set
for(it=s.begin();it!=s.end();it++){
    if(ss.find(*it)!=ss.end()) printf("%d ",*it);
}
//查找set某元素个数(0或1)
set.count(x);
//清空set
s.clear();
原文地址:https://www.cnblogs.com/yzm10/p/7531316.html