2022/1/24

基本语法

1.sort与比较器

使用:#include <algorithm>
     using namespace std;
时间复杂度:n*lg(n)

形式:sort(first_pointer,first_pointer+n,cmp)

第一个参数是数组的首地址,一般写上数组名就可以,因为数组名是一个指针常量。
第二个参数相对较好理解,即首地址加上数组的长度n(代表尾地址的下一地址)。
最后一个参数是比较函数的名称(自定义函数cmp),即第三个参数可以缺省,这样sort会默认按数组升序排序。

对数组A的0~n-1元素进行升序排序,只要写sort(A,A+n)即可
对于向量V也一样,sort(v.begin(),v.end())即可。
sort(first_pointer,first_pointer+n,cmp)
sort函数第三个参数比较器
传入的第一个参数O1 > 第二个O2  则与降序
传入的第一个参数O1 < 第二个O2  则为升序  默认相同
传入的第一个参数O1 = 第二个O2  则返回谁都行

使用标准库函数    #include <functional>
升序:sort(begin,end,less<data-type>())            less<int>()
降序:sort(begin,end,greater<data-type>())         greater<int>()

bool cmp1(int a,int b)//int为数组数据类型
{
    return a>b;//降序排列
    //return a<b;//默认的升序排列
}

bool cmp2(Student a,Student b)
{
    return a.id>b.id;//按照学号降序排列
    //return a.id<b.id;//按照学号升序排列
}
#include<algorithm>
#include<iostream>
#include<vector>
#include<functional>
using namespace std;
bool mycmp_up(int a,int b)
{
	return a>b;
}
bool mycmp_down(int a,int b)
{
	return a<b;
}
int main()
{
	int arr[]={4,1,2,8,6,2};
	vector<int> arr_arr={4,5,9,2,1,4,7};
	int arr_len=sizeof(arr)/sizeof(arr[0]);
	
	//sort(arr,arr+arr_len,mycmp_down);
	sort(arr,arr+arr_len,less<int>());    //升序 
	for(int a=0;a<arr_len;a++) cout<<arr[a];
	cout<<endl;
	
	//sort(arr_arr.begin(),arr_arr.end(),mycmp_down);
	sort(arr_arr.begin(),arr_arr.end(),greater<int>());  //降序 
	for(int a=0;a<arr_arr.size();a++) cout<<arr_arr[a];
	cout<<endl;
}

2.结构体

#include <string>
#include <iostream>
using namespace std;
struct data{
	int year,month,day;
}; 
struct student {
    string name;
    int age;
    float height;
    data *brithday;               data类型指针指向data类型对象的地址stu1_p->brithday=&d; 
};
int main() {
    student stu1;
    data d={1999,2,15};
    //student *stu1_p=&stu1;  
    student *stu1_p;                           定义student类型指针stu1_p
    stu1_p = &stu1;        &stu1取地址          指针stu1_p指向stu1的地址
    stu1_p->brithday=&d; 
    cin>>(*stu1_p).name>>(*stu1_p).age>>(*stu1_p).height; (*stu1_p)括号不能省略
    cout<<stu1_p->name<<" ";                   
    cout<<stu1_p->age<<" ";
    cout<<stu1_p->height<<" ";    
	cout<<stu1_p->brithday->year<<" ";
	cout<<stu1_p->brithday->month<<" ";    
	cout<<stu1_p->brithday->day<<" ";
    return 0;
}
张三
20
182.5
张三 20 182.5 1999 2 15

3.swap函数


/**************************
 * 通过引用交换两个数的值
 *(类型)&(变量名)=(所引用的变量)
 * 
 * 引用就是某一变量(目标)的一个别名,对引用的操作与对变量直接操作完全一样。
 * 引用的声明方法:类型标识符 &引用名=目标变量名;
 * 【例1】:int a; int &ra=a; //定义引用ra,它是变量a的引用,即别名
 * 说明:
 * (1)&在此不是求地址运算,而是起标识作用。
 * (2)类型标识符是指目标变量的类型。
 * (3)声明引用时,必须同时对其进行初始化。
 * (4)引用声明完毕后,相当于目标变量名有两个名称,即该目标原名称和引用名,
 * 且不能再把该引用名作为其他变量名的别名。
 *  ra=1; 等价于 a=1;
 * (5)声明一个引用,不是新定义了一个变量,它只表示该引用名是目标变量名的
 * 一个别名,它本身不是一种数据类型,因此
 * 引用本身不占存储单元,系统也不给引用分配存储单元。故:对引用求地址,就是
 * 对目标变量求地址。&ra与&a相等。
 * (6)不能建立数组的引用。因为数组是一个由若干个元素所组成的集合,所以无法
 * 建立一个数组的别名。
 **************************/
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

void Swap (int left, int right) //值传递的方式无法实现交换,
//因为传参时对于参数left和right拷贝一临时副本,交换的是副本值,
//因为其是临时变量函数退出,变量销毁,并不会影响外部left和right的值。
{
	 int temp = left;
     left = right ;
     right = temp ;
}
 
void Swap1 (int& left, int& right)   //就是int& left=len;int& right=num;
//使用引用的话,
//不做临时拷贝,&的使用说明此处只是原参数的另一个名字而已,
//所以修改时直接在原参数的基础上修改变量值。
{
     int temp = left;
     left = right ;
     right = temp ;
}
 
void Swap2 (int* pLeft, int* pRight)//传入的是地址,
//因为地址是唯一的,所以指针通过地址的访问进而可修改其内容。
//传入的是地址
{
     int temp = *pLeft;
     *pLeft = *pRight;
     *pRight = temp;
}
int main()
{
    int len=9,num=4;
	Swap(len,num);
	cout<<len<<num<<endl;
	len=9;num=4;	
	Swap1(len,num);
	cout<<len<<num<<endl;
	len=9;num=4;
	Swap2(&len,&num);          输入的是地址
	cout<<len<<num<<endl;
}

4.strlen()函数与sizeof()操作符char数组初始化

头文件:#include <string.h>
strlen()函数用来计算字符串的长度,其原型为:
    unsigned int strlen (char *s);
【参数说明】s为指定的字符串。
strlen()用来计算指定的字符串s 的长度,不包括结束字符"\0"。
【返回值】返回字符串s 的字符数。
注意一下字符数组,例如
    char str[100] = "http://see.xidian.edu.cn/cpp/u/biaozhunku/";
定义了一个大小为100的字符数组,但是仅有开始的11个字符被初始化了,剩下的都是0,所以 sizeof(str) 等于100,strlen(str) 等于11。
如果字符的个数等于字符数组的大小,那么strlen()的返回值就无法确定了,例如
    char str[6] = "abcxyz";
#include<stdio.h>
#include<string.h>
或者#include<cstring>
int main()
{
    char *str1 = "http://see.xidian.edu.cn/cpp/u/shipin/";
    char str2[100] = "http://see.xidian.edu.cn/cpp/u/shipin_liming/";
    char str3[5] = "2345";
    printf("strlen(str1)=%d, sizeof(str1)=%d\n", strlen(str1), sizeof(str1));
    printf("strlen(str2)=%d, sizeof(str2)=%d\n", strlen(str2), sizeof(str2));
    printf("strlen(str3)=%d, sizeof(str3)=%d\n", strlen(str3), sizeof(str3));
    return 0;
}
strlen(str1)=38, sizeof(str1)=8           sizeof(str1)是指针的长度
strlen(str2)=45, sizeof(str2)=100          str2数组的长度
strlen(str3)=4, sizeof(str3)=5
strlen()中间可以用     char数组名字    或者    指向开头的char指针

char数组初始化

#include<stdio.h>
#include<string.h>
#include<string>
using namespace std; 
int main()
{        //不指定时长度根据初始化长度,一定要初始化才能有长度
    char *str1 = "http://see.xidian.edu.cn/cpp/u/shipin/";
    char str2[] = "http://see.xidian.edu.cn/cpp/u/shipin_liming/";
    char str3[] = "2345";
//  char str[];   单独这一行也不行数组得说明长度
//	str="I am happy";//错误,字符数组的赋值只能按元素一一赋值
    printf("strlen(str1)=%d, sizeof(str1)=%d\n", strlen(str1), sizeof(str1));
    printf("strlen(str2)=%d, sizeof(str2)=%d\n", strlen(str2), sizeof(str2));
    printf("strlen(str3)=%d, sizeof(str3)=%d\n", strlen(str3), sizeof(str3));
    return 0;
}
strlen(str1)=38, sizeof(str1)=8
strlen(str2)=45, sizeof(str2)=46
strlen(str3)=4, sizeof(str3)=5
#include <iostream>
#include <string.h>
using namespace std;
int mystrcmp(const char* src, const char* dst);
int main() {
    char s1[100] = { 0 };
    char s2[100] = { 0 };
    cin.getline(s1, sizeof(s1)); //sizeof(s1)=100控制范围 
    cin.getline(s2, sizeof(s2));
    int ret = mystrcmp(s1, s2);
    cout << ret << endl;
    return 0;
}
int mystrcmp(const char* src, const char* dst) {
    // char* src=s1 ,const char* dst=s2
	int l=strlen(src),l1=strlen(dst);
	for(int a=0;a<l;a++) cout<<src[a];
	cout<<endl;
	for(int a=0;a<l1;a++) cout<<*(dst+a);
	cout<<endl;
    return l;
}
skzk
ckqndn
skzk
ckqndn
4
#include <iostream>
#include <string.h>
using namespace std;
int main() {
    char s1[100] = { 0 };
    char s2[100] = { 0 };
    cin.getline(s1, sizeof(s1)); //sizeof(s1)=100控制范围 
    cin.getline(s2, sizeof(s2));
    cout<<s1<<" "<<s2<<endl;   //数组名就是首地址 
	char* src=s1;              //指针指向首地址src就是首地址
	char* dst=s2;
	cout<<src<<" "<<dst<<endl;
	int l=strlen(src),l1=strlen(dst);
	for(int a=0;a<l;a++) cout<<src[a];//src就是首地址可以当成数组名使用 
	cout<<endl;
	for(int a=0;a<l1;a++) cout<<*(dst+a);
	cout<<endl;
    return 0;
}
asd
czxcz
asd czxcz
asd czxcz
asd
czxcz

5.strrev(), reverse()

1.使用string.h中的strrev函数
strrev函数只对字符数组有效,对string类型是无效的。
#include<stdio.h>
#include<string.h>
int main()
{
     char s[]="hello";
     strrev(s);
     puts(s);
     return 0;
}
2.使用algorithm中的reverse函数
reverse函数是反转容器中的内容,对字符数组无效。  string和vector
reverse函数可以反转vector里的元素。
#include <iostream>
#include <string>
#include<vector>
#include <algorithm>
using namespace std;
int main()
{
     string s= "hello";
     reverse(s.begin(),s.end());
     cout<<s<<endl;
     return 0;
 	vector<int> s;
 	for(int i=0;i<10;i++){
 		s.push_back(i+1);
	 }
     reverse(s.begin(),s.end());
 	for(int i=0;i<10;i++){
 		cout<<s[i]<<" ";
	 }
}

题目比较器的使用 

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct fruit{
    int id;
    string name;
    int price;
};
bool mycmp(fruit a,fruit b){         bool所以有返回值一定要有return
    if(a.price!=b.price) 
       return a.price<b.price;
    else return a.name<b.name;
}
int main()
{
    int len=0,num=0,res=0;
    cin>>len>>num;
    vector<fruit> input(len);         (len)一定要加上不加上a一直为空写不进去
    for(auto &a:input)                每个a都是一个人fruit的结构体类型的对象
        cin>>a.id>>a.name>>a.price;
    sort(input.begin(),input.end(),mycmp);        //使用algorithm
    for(int j=0;j<num;j++)  res+=input[j].price;
    cout<<res;
}

题目 500. 键盘行

class Solution {
public:
    vector<string> findWords(vector<string>& words) {
        string line1("qwertyuiop");
        string line2("asdfghjkl");
        string line3("zxcvbnm");
        vector<string> res;
        int count1=0,count2=0,count3=0;
        for(string a:words){
            for(auto b:a){
                if(b>='A'&&b<='Z') b=b+32;   将大写字母转化为小写
                for(auto c:line1) if(b==c) {count1++;break;}
                for(auto c:line2) if(b==c) {count2++;break;}
                for(auto c:line3) if(b==c) {count3++;break;}
            }
            if(count1==a.size()||count2==a.size()||count3==a.size())
            res.push_back(a);
            count1=count2=count3=0;
        }
        return res;
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值