ASCII码

常见ASCII码如下

Bin(二进制) Oct(八进制) Dec(十进制) Hex(十六进制) 缩写/字符 解释
0011 0000 060 48 0x30 0 字符0
0100 0001 0101 65 0x41 A 大写字母A
0110 0001 0141 97 0x61 a 小写字母a
0010 0000 040 32 0x20 (space) 空格

缓冲区输入

cin对空格和回车都不敏感,都不影响继续读入数据,所以需要利用getchar()函数检测回车

如下代码会一直读取缓冲区中数存入vector直到读取到回车结束

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int main(int, char**) {
  5. vector<int> n;
  6. int t;
  7. while(cin>>t){
  8. n.push_back(t);
  9. if(getchar()=='\n') break;
  10. }
  11. for(auto x: n){
  12. cout<<x<<" ";
  13. }
  14. cout<<endl;
  15. return 0;
  16. }

输出结果:

  1. build [main●●●] % /Users/wu000376/Code/Algorithm/c++/inputTest/build/input
  2. 1 2 3 4 5 6 7
  3. 1 2 3 4 5 6 7
  4. build [main●●●] %

int与string转化

  • int转string ```cpp int aa = 30; stringstream ss; ss<<aa; string s1 = ss.str(); cout<<s1<<endl; // 30

string s2; ss>>s2; cout<<s2<<endl; // 30

  1. 或者使用
  2. ```cpp
  3. string to_string (int val);
  • string转int ```cpp string s = “17”; stringstream ss; ss<<s;

ss>>i; cout<<i<<endl; // 17

<a name="XPxbu"></a>
# <br />
<a name="CXOH4"></a>
# C++中常量最大、最小整数

C++中常量INT_MAX和INT_MIN分别表示最大、最小整数,定义在头文件limits.h中。

```cpp
#define INT_MAX 2147483647
#define INT_MIN (-INT_MAX - 1)

随机数

用时间值做种子,就可以产生随机数了,因为时间总是在变的嘛。

time(NULL)作为srand()的参数,更新种子,再用rand()函数产生随机数。

#include<iostream>
#include<cstdlib>
#include<time.h> 
using namespace std;
int main()
{
    srand(time(NULL));
    cout << rand() << endl; 
    return 0;
}

需要注意的是,srand(time(NULL))只需要执行一次即可。

限定区间

但这里获取的值是不确定的,而如果我们希望获得在某一范围内的值呢,也很简单,如下所示:

#include<iostream>
#include<ctime>
using namespace std;
int main()
{
    srand(time(0));
    for (int i = 0; i < 100; i++)
    cout << rand() % 100 << endl;
    return 0;
}

而如果我们希望得到0 - 1之间的数呢? 如下所示:

#include<iostream>
#include<ctime>
using namespace std;
int main()
{
    srand(time(0));
    for (int i = 0; i < 100; i++)
        cout << (rand() % 10) * 0.1 << endl;
    return 0;
}

而我们希望得到-1 到 1 之间的数呢?

#include<iostream>
#include<ctime>
using namespace std;
int main()
{
    srand(time(0));
    for (int i = 0; i < 100; i++)
        cout << (rand() % 10) * 0.2 - 1 << endl;
    return 0;
}

32位扩展

rand()产生的数是0~RAND_MAX( 0x7fff),也就是说,只能生成15位。如果需要32位的,可以进行扩展:

unsigned int rand_32()
{
    return (rand()&0x3)<<30 | rand()<<15 | rand();
}