常见的语法知识(经常更新

1 int 转 string (4 steps)

【1】#include <sstream>  // 引入stringstream头文件

【2】  stringstream ss;  //step-1
【3】  ss << i;    //step-2  int值传给stringstream
【4】 string out_string = ss.str();  //step-3   stringstream转string类型

【5】 ss.clear();  //step-4 释放stringstream。。注意的是:stringstream类型变量,不能自动释放内存,多次用到时,最好要配合.clear(); 主动释放内存。

#include <sstream>  // 引入stringstream头文件
#include <iostream>

using namespace std;

int main()
{
    
  int i = 123;
  
  // 【1】int 转 string的 4 步!!!
  stringstream ss;  //step-1
  ss << i;    //step-2  int值传给stringstream
  string out_string = ss.str();  //step-3   stringstream转string类型
  ss.clear();  //step-4 释放stringstream
  cout << out_string << "\n";
  return 0;
}

2 string 转 int (2 steps)

【方式1】int a = stoi(x); 


【方式2】:

(1)头文件:#include <cstdlib> 

(2)int x = atoi(str2.c_str()+2);    //atoi()函数转,字符串首字母不为数字时,输出为0 。括号内是由这个string的某个地址到结尾,对应数字,可以是 + / - 或是不带符号就默认正数。

#include <iostream>
#include <cstdlib>
using namespace std;
 
int main()
{ 
	int x;
	string str = "-10";
	string str1 = "-10agdgd";
	string str2 = "da10";
	x = atoi(str.c_str());//返回首地址
	cout << x << endl;
	x = atoi(str1.c_str());		 //atoi()函数遇到字母时会自动停下
	cout << x << endl;
	x = atoi(str2.c_str()+2);    //atoi()函数转,字符串首字母不为数字时,输出为0 
	cout << x << endl;
}

3 long long与取模

sum = sum * (u.second + 1) % MOD; // 正确

sum*= (u.second + 1) % MOD;  // 错误 这个式子不等价上面这个式子!

4 reverse

 反转Vector容器 

 reverse(a.begin(), a.end());

反转string

 reverse(s.begin(), s.end());

翻转字符数组

    char s[] = "abcdefg";
    int N = sizeof(s) / sizeof(s[0]);  //char[]的个数 N = 8
    reverse(s, s + N - 1);  // -1是减掉'\0'

反转数组

    int s[] = { 1,2,3,4,5,6,7,8,9 };   
    reverse(s, s + sizeof(s) / sizeof(s[0]));

5 string

【1】substr函数

1、substr函数格式 (俗称:字符截取函数)

  格式1: substr(string string, int a, int b);

  •     string 需要截取的字符串 
  •     a 截取字符串的开始位置(注:当a等于0或1时,都是从第一位开始截取)
  •     b 要截取的字符串的长度

  格式2:substr(string string, int a) ;

  •    string 需要截取的字符串
  •     a 可以理解为从第a个字符开始截取后面所有的字符串。
     

更多注意事项


【2】find函数

string的find()函数用于找出字母在字符串中的位置。

find(str,position)

str:是要找的元素。 position:字符串中的某个位置,表示从从这个位置开始的字符串中找指定元素(不填第二个参数,默认从字符串的开头进行查找)。返回值为目标字符的位置(第一个字符位置为0),当没有找到目标字符时返回npos


 

6 全排列


next_permutation的意思是下一个排列,与其相对的是prev_permutation,即上一个排列。

next_permutation只能获得上一个排列,如果要获得全排列,那么就需要先对数组进行升序排序

基本定义如下:
next_permutaion(起始地址,末尾地址+1)
next_permutaion(起始地址,末尾地址+1,自定义排序)

1、普通数组全排列

普通数组可以通过数组名表示地址,非常容易实现。示例代码:

注意!其不会输出重复的组合 比如 a[4] = {1,1,1,1}; 它只会输出一个。复杂度: O(n)

#include<iostream>
#include<algorithm>//使用 next_permutation()和sort()需要导入的头文件 
using namespace std;
int main(){
    int a[4]={2,1,4,3};
    
    sort(a,a+4);//对数组排序 
    
    do{
        for(int i=0;i<4;i++){//打印排列 
            cout<<a[i]<<' ';
        }
        cout<<endl;
    }while(next_permutation(a,a+4));//获取下一个排列 
} 

2、结构体全排列

结构体默认是不能比较大小的,那么就不能使用默认的next_permutation()排列比较函数,需要使用自定义排列比较函数

示例代码:

#include<iostream>
#include<algorithm>//使用 next_permutation()和sort()需要导入的头文件 
using namespace std;

struct test{//结构体test 
    int val;
}; 

bool cmp(test t1,test t2){//自定义的排列 
    return t1.val<t2.val;
}

int main(){
    test t[4];//结构体数组 
    t[0].val=1;
    t[1].val=2;
    t[2].val=3;
    t[3].val=4;
    
    do{
        for(int i=0;i<4;i++){//打印排列 
            cout<<t[i].val<<' ';
        }
        cout<<endl;
    }while(next_permutation(t,t+4,cmp));//获取下一个排列 
} 

3、vector

vector及string等数据结构不能直接用名字代表地址,只能够使用自带的迭代器begin()、end()实现全排列

示例代码:

#include<iostream>
#include<vector> //使用vector需要导入的头文件 
#include<algorithm>//使用 next_permutation()和sort()需要导入的头文件 
using namespace std;

int main(){
    vector<int> v;//定义一个int型的vector 
    v.push_back(1);//在尾部插入数据1 
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    
    do{
        for(int i=0;i<v.size();i++){//打印排列 
            cout<<v[i]<<' ';
        }
        cout<<endl;
    }while(next_permutation(v.begin(),v.end()));//获取下一个排列 
} 

7 输出a位小数

exp(x);  // 该函数返回 e 的 x 次幂。
cout<<fixed<<setprecision(c);     // 保留n位小数

8 多组输入(没有组数)

代码:

#include<iostream>
using namespace std;

int main()
{
    char a[110];
    
    while(cin.getline(a,110))
    {
        int sum = 0;
        for(int i = 0; a[i] != '\0'; i++)
        {
            if(a[i] != ' ')sum+= a[i]-'0';
        }
        cout << sum << endl;
    }
    return 0;
}

核心:cin.getline(a,110); // 默认以换行符结束,接受空格!

代码:

#include<bits/stdc++.h>
using namespace std;

bool cmp(string a,string b)
{
    return a < b;
}

int main()
{
    string x;
    char y[110];
    
//     while(cin.getline(y,10))
    while(getline(cin,x))  //【重点1】
    {
        vector<string> tmp;
        
        int i = 0;
        int position = 0;
        string uu;
        while((position=x.find(' ' , i)) != string::npos) //【重点2】  字符串切割
        { 
            uu = x.substr(i,position-i);
            tmp.push_back(uu);
            i = position + 1;
        }
        uu = x.substr(i); //【重点2】  字符串切割
        tmp.push_back(uu);
        
        sort(tmp.begin(),tmp.end(),cmp);
        for(auto u: tmp)cout << u << ' ';
        puts("");
    }
    return 0;
}

 


end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值