to_string()
字符数组
注意结尾的
'\n'
#include<iostream>
#include<string>
using namespace std;
const int N = 100;
char s[N];
char ss[N];
string to_String(int n)
{
int i = 0 ,j = 0;
if (n < 0)// 处理负数
{
n = 0 - n;
j = 1;
ss[0] = '-';
}
while (n > 0)
{
s[i ++] = n % 10 + '0';
n /= 10;
}
s[i] = '\0'; // 结尾
// 逆序过来
i = i - 1;
while (i >= 0)
{
ss[j++] = s[i--];
}
ss[j] = '\0';
return ss;
}
int main()
{
cout << "请输入整数:";
int m;
cin >> m;
string s = "转为字符串:" + to_String(m) ;
cout << s << endl;
system("pause");
return 0;
}
字符串
借助
reverse
#include<iostream>
#include<string>
#include <algorithm>
using namespace std;
string to_String(int n)
{
string s = "";
int i = 0 ,j = 0;
if (n < 0)// 处理负数
{
n = 0 - n;
j = 1;
s += '-';
}
while (n > 0)
{
s += n % 10 + '0';
n /= 10;
}
reverse(s.begin() + j, s.end());
return s;
}
int main()
{
cout << "请输入整数:";
int m;
cin >> m;
string s = "转为字符串:" + to_String(m) ;
cout << s << endl;
system("pause");
return 0;
}
nnext_permutation
void nextPermutation(vector<int>& nums) {
int n = nums.size();
int k = n - 1;
while(k > 0 && nums[k - 1] >= nums[k]) k --;
if(k <= 0) reverse(nums.begin(), nums.end()); // 没有升序
else
{
int t = k; // 找最大的最小的那个交换
while(t + 1 < n && nums[t + 1] > nums[k - 1]) t ++;
swap(nums[k - 1], nums[t]);
reverse(nums.begin() + k, nums.end()); // 从下边为k的地方翻转
}
}