当在网页上看到一个东西比较好,就情不自禁地保存一个书签,久而久之书签就多了,然后进行分类,发现类别也越来越多,于是每隔一段时间就对知识性的书签进行整理。
1.C++读取一行不定数目的整数
中间用空格隔开,可以有多空格:
#include <iostream>
using namespace std;
//return true if \n, or return false
bool escape_white_space(istream &is)
{
char dummy;
while (true)
{
is >> dummy;
if (dummy == '\n')
{
return true;
}
else if (!isspace(dummy))
{
is.putback(dummy);
return false;
}
}
}
int main()
{
int a[10];
cin >> std::noskipws;
int n = 0;
while (true)
{
if (escape_white_space(cin))
break;
cin >> a[n++];
}
for (int i = 0; i < n; i++)
{
cout << a[i];
}
return 0;
}
例子:
当然还有C语言版的:
注意sscanf_s,gets_s,这是在VS里编译的。
#include <string>
#include <stdio.h>
int main() {
char buffer[1024];
char* p = buffer;
int a[50];
int n = 0;
gets_s(buffer);
while (*p != 0)
{
// 过滤空格与缩进
while (*p == ' ' || *p == '\t') p++;
// 读整数
if (sscanf_s(p, "%d", &a[n++]) == 0) break;
// 偏移指针到非数字部分
p++;
while (*p >= '0' && *p <= '9') p++;
}
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
return 0;
}
2.sort函数排序
很多东西太久不用就会忘,比如sort函数的comp参数写法:
对结构体排序:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct example
{
int elem1;
int elem2;
}example;
/*这个comparison函数很重要.如果希望升序排序,就是"<",降序排列就是">"号,这样便于直观记忆.如果希望用elem2作为比较标准
就把elem1改为elem2,这样结构体就以elem2为比较标准排序了.*/ bool comparison(example a,example b){
return a.elem1<b.elem1;
}
int main()
{
int N;
fin>>N;
vector<example> array(N);
for(int i=0;i<N;i++)
{
fin>>array[i].elem1>>array[i].elem2;
}
sort(array.begin(),array.end(),comparison);
for(int i=0;i<N;i++)
{
cout<<array[i].elem1<<" "<<array[i].elem2<<endl;
}
return 0;
}
还有简单的对整数排序:
#include <iostream>
#include <algorithm>
using namespace std;
bool compare(int a, int b)
{
return a<b; //升序排列,如果改为return a>b,则为降序
}
int main()
{
int a[10] = { 2,4,1,23,5,76,0,43,24,65 }, i;
for (i = 0; i < 10; i++)
cout << a[i] << " ";
cout << endl;
sort(a, a + 10, compare);
for (i = 0; i < 10; i++)
cout << a[i]<<" ";
cout << endl;
return 0;
}
还有C语言用的qsort:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
int compare(const void *a, const void *b)
{
return *(int*)b - *(int*)a;
}
int main()
{
int a[10] = { 2,4,1,23,5,76,0,43,24,65 }, i;
for (i = 0; i < 10; i++)
cout << a[i] << " ";
cout << endl;
qsort(a, 10, sizeof(int),compare);
for (i = 0; i < 10; i++)
cout << a[i]<<" ";
cout << endl;
return 0;
}
3.结构体直接比较
我们可以重载运算符,比如根据点的x轴进行比较:
以下重载==运算符,小于大于自然就会了:
//存放点的结构体
struct Point {
float x;
float y;
bool operator==(const Point &p) const {
if (this->x == p.x&&this->y == p.y) {
return true;
}
else {
return false;
}
}
};
4.C++设置浮点数位数
设置浮点数位数有很多方法,假设这里取两位:
1.setprecision方法,这个方法总是记不住,需要头文件#include<iomanip.h>:
cout<<setiosflags(ios::fixed)<<setprecision(2)<<a<<endl;
2.C语言法,printf控制很简单:
printf("%.2f\n",a);
3.还可以先乘100转成整数转成float再除100:
((float)((int)(f*100)))/100;
接下来看看结果:
#include <stdio.h>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float a = 1.236567;
//1
cout << setiosflags(ios::fixed) << setprecision(2) << a << endl;
//2
printf("%.2f\n", a);
//3
cout << ((float)((int)(a * 100))) / 100 << endl;
return 0;
}
可以看到最后一种是不会四舍五入的。
更多可以参考C++标准库
11万+

被折叠的 条评论
为什么被折叠?



