码迷,mamicode.com
首页 > 其他好文 > 详细

计概期中前的小小总结

时间:2019-11-03 12:59:33      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:ios   width   位置   双引号   space   namespace   height   输入   class   

1. 打印输入题

 1 #include<iostream>
 2 #include<iomanip>
 3 #include<string>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<vector>
 7 using namespace std;
 8 int main(){
 9     cout << setfill(x) << setw(5) << "x"; 
10     return 0;
11 }

 注意setfill和setw的使用,setw设置宽度,setfill设置用什么填充。其中setfill的参数一定是字符(只能使用单引号,不能用双引号)

2.排序

(1)sort的运用

int a[5] = {1,2,3,4,5};
sort(a, a+5, greater<int>());//最后一个元素的下一个位置

注意从大到小排的话用的函数是greater<int>(),不要忘了括号,这和priority_queue<int, vector<int>, greate<int> > 的用法有区别

还有,sort的第二个参数应该对应最后一个元素的下一个位置。


(2)手写冒泡排序

虽然是很基础的东西,但我总记不住

int a[5] = {4,2,1,3,5};
    for(int i = 0; i < 5; i++)  // 循环n轮(n-1)也行 
        for(int j = 0; j < 4; j++)//从头到尾扫一遍 
            if(a[j]>a[j+1]){ //注意是相邻两元素比 
                int t = a[j];
                a[j] = a[j+1];
                a[j+1] = t;
            }

3. 字符串操作

(1)输入

技术图片

 

 注意这个输入陷阱

如何一个一个读取字符:

 while( (c = cin.get()) != \n) 

(2)常用函数

技术图片这个地方应该返回的是b第一次出现在a中的地址,ppt写的有点问题

 

 上次集体作业遇到的神秘问题: strlen必须再取一次整才能当整型用

用strstr(a,b) - a就可以获得位置(两个指针相减)

(3)和string转换

string ss = "ABCDEF";
//string s2 = "HJK";
char s[100];
strcpy(s,ss.c_str());

 

4. 以前的知识重温,以备不时之需

struct stu{
    int name;
    int age;
    stu(int n = 0, int a = 0):name(n),age(a){}
};
bool cmp(stu a, stu b){
    return a.age > b.age;
}

如何定义一个带构造函数的结构体。以及手写一个比较函数

 

计概期中前的小小总结

标签:ios   width   位置   双引号   space   namespace   height   输入   class   

原文地址:https://www.cnblogs.com/fangziyuan/p/11785646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!