#include
#include
using namespace std;
int main()
{
string s = "12d1 2 ddsd";
int d = 0;
for (int i = 0; i<s.length(); i++)
{
if (s[i] != ' ' && isprint(s[i])) //判断不为空格 且能输出;
{
s[d++] = s[i];
}
}
cout << "改变后的字符串是:" << endl;
for (int i = 0; i < d; i++)
{
cout << s[i];
}
}

setw(int n)是c++中在输出操作中使用的字段宽度设置,设置输出的域宽,n表示字段宽度。只对紧接着的输出有效,紧接着的输出结束后又变回默认的域宽。
当后面紧跟着的输出字段长度小于n的时候,在该字段前面用空格补齐;当输出字段长度大于n时,全部整体输出。

#include
#include
using namespace std;
int main()
{
int a[3] = {1,2,3};
for (int i = 0; i < 3; i++)
{
cout << setw(2) << a[i]; //setw(n)
}
return 0;
}

#include
#include
using namespace std;
int main()
{
int a[3] = {1,2,3};
for (int i = 0; i < 3; i++)
{
cout << setw(0) << a[i]; //setw(n)
}
return 0;
}

#include
#include
using namespace std;
int main()
{
int a[3] = {1,2,3};
for (int i = 0; i < 3; i++)
{
cout << setw(2) << a[i]<<4;
}
return 0;
}

#include
#include
#include
using namespace std;
int main()
{
string s = "123";
cout << setw(5) << setfill('*') << s << endl; // 共设置5个空间,用不满的在前面加*,用不满的话不用
return 0;
}

1.如果用sort进行排序,那么需要用头文件#include
2、sort模板含三个参数:
sort (begin,end,cmp)
参数begin:要排序数组的起始地址(第一个数据的地址)
参数end:最后一个数据的下一个数据的地址
参数cmp:若这个参数不写,默认为升序
#include
#include
#include
using namespace std;
int main()
{
int i;
string s;
cin >> s;
sort(s.begin(), s.end());
for (i = 0; i < s.length(); i++)
{
cout << s[i] << " ";
}
return 0;
}

#include
#include
using namespace std;
bool cmp(int a,int b)
{
return a<b; //或则在cmp函数里面写return a
}
int main()
{
int a[]={6,5,8,4,3,2},i;
sort(a,a+6,cmp);
for(i=0;i<5;i++)
{
cout<<a[i]<<" ";
}
return 0;
}

#include
#include
using namespace std;
bool cmp(int a,int b)
{
return a>b; //注意这里是a>b是指降序
}
int main()
{
int a[]={6,5,8,4,3,2},i;
sort(a,a+6,cmp);
for(i=0;i<5;i++)
{
cout<<a[i]<<" ";
}
return 0;
}

while()循环方法
int gcd(int a,int b) {
int r;
while (a%b!=0)
{
r=a%b;
a=b;
b=r;
}
return b; }
2.递归+三元运算符
int gcd(int a, int b) { return b > 0 ? gcd(b, a % b) : a; }
3.递归+ if 语句
求x 和 y 的最大公约数,就是求 y 和 x % y 的最大公约数
int gcd(int a,int b) {
if(a%b==0)
return b;
else
return (gcd(b,a%b)); }
#include
#include
using namespace std;
int gcd(int a, int b)
{
return b > 0 ? gcd(b, a % b) : a;
}
int main()
{
int a, b;
cin >> a >> b;
cout << gcd(a, b) << endl;
}

级联(cascade)在计算机科学里指多个对象之间的映射关系,建立数据之间的级联关系提高管理效率
#include
#include
using namespace std;
int main()
{
cout.put('C').put('+').put('+').put('\n');
return 0;
}

#include
#include
#include
using namespace std;
int main()
{
int a = 3, b = 0;
int c=pow(a, 1);
cout << c << endl;
}

#include
Sleep(1000); //以毫秒为单位
#include
#include
using namespace std;
int main()
{
int a;
Sleep(1000);
cin >> a;
cout << a << endl;
}
