• STL


    std::string to_string(int value); (1) (C++11起) 
    std::string to_string(long value); (2) (C++11起) 
    std::string to_string(long long value); (3) (C++11起) 
    std::string to_string(unsigned value); (4) (C++11起) 
    std::string to_string(unsigned long value); (5) (C++11起) 
    std::string to_string(unsigned long long value); (6) (C++11起) 
    std::string to_string(float value); (7) (C++11起) 
    std::string to_string(double value); (8) (C++11起) 
    std::string to_string(long double value); (9) (C++11起)

    std::to_string是C++标准(2011年)的最新版本中引入的功能。旧的编译器可能不支持它。

    std::to_string//数字转换成字符串

    #include <iostream>
    #include <string>
    using namespace std;
    //编译器必须是c++11以上
    int main()
    {
        string pi = "pi is " + std::to_string(3.1415926);
        string perfect = to_string(1 + 2 + 4 + 7 + 14) + " is a perfect number";
        cout << pi << '
    ';
        cout << perfect << '
    ';
        return 0;
    }
    View Code

    stoi函数

    stoi()函数如果传入的字符串s中含有不是数字的字符,则只会识别到从开头到第一个非法字符之前,如果第一个字符就是非法字符则会报错

    如图所示:

    next_permutation

    当返回为1时,表示找到了下一全排列;返回0时,表示无下一全排列。注意,如果从begin到end为降序,则表明全排列结束,逆置使其还原到升序。

    如何获取所有全排列情况?STL中的代码非常精妙,利用next_permutation的返回值,判断是否全排列结束(否则将死循环)。对于给定的一个数组,打印其所有全排列只需如下:

    #include <iostream>
    #include <string>
    #include<algorithm>
    using namespace std;
    //编译器必须是c++11以上
    // Display All Permutation
    template<class T>
    void all_permutation(T arr[], int n)
    {   int num=0;
        sort(arr,arr+n);    // sort arr[] in ascending order
        do{
            for(int i=0; i<n; printf("%c",arr[i++]),num++);
            printf("
    ");
        }while(next_permutation(arr,arr+n));//利用了返回值
        cout<<"排列总数:"<<num/n<<endl;
    }
    int main()
    {   char arr[50];;
        int arr_size;
        cin>>arr_size;
        for(int i=0;i<arr_size;i++)cin>>arr[i];
        all_permutation(arr,arr_size);
        return 0;
    }
    不一样的烟火
  • 相关阅读:
    JAVA读取properties
    nginx默认语法
    csp-s模拟45
    csp-s模拟44
    noip模拟测试42
    noip模拟测试40
    noip模拟测试21
    noip模拟测试20
    noip模拟测试19
    noip模拟测试18
  • 原文地址:https://www.cnblogs.com/cstdio1/p/10994861.html
Copyright © 2020-2023  润新知