一些STL的新姿势pika~

本文通过C++代码实例展示了如何使用next_permutation函数生成数组和字符串的全排列,以及如何使用atoi与itoa函数将整数转换为任意进制表示。代码涵盖了数组元素全排列、字符串全排列和数字进制转换的基本实现。

1.next_permutation生成全排列


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int main(){
//数组元素全排列
    int a[4] = {6,3,7,2};
    sort(a,a+4);
    for(int i = 0 ; i < 4 ; i++){
        cout << a[i] << ' ';
    }
    cout << endl;
    while(next_permutation(a,a+4)){
        for(int i = 0 ; i < 4 ; i++){
            cout << a[i] << ' ';
        }
        cout << endl;
    }//这里用do-while改写更佳
//字符串全排列
    string s = "cdfas";
    sort(s.begin(),s.end());
    cout << s << endl;
    while(next_permutation(s.begin(),s.end())){
        cout << s << endl;
    }//这里用do-while改写更佳
    return 0;
}

也可以用与next_permutation相反的perv_permutation

2.利用atoi与itoa将一个数字转化成任意进制
关于atoi与itoa的介绍在之前日志里有,这里直接做演示。

#include<cstdlib>
#include<cstdio>
int main()
{
    int num = 233;
    char str[100];
    int n = atoi(itoa(num, str, 2));
    printf("%d\n",n);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值