有一天,我闲来无事,想:string会不会有极限呢?
于是,我就打开Dev-C++
#include <bits/stdc++.h>
using namespace std;
int main(){
string p;
for(int i=1;;i++){
p+=' ';
}
return 0;
}
这个代码是在string里一直加空格
运行后……
我一猜,就是string的极限到了。
我用freopen打开一个文件,叫test.txt
#include <bits/stdc++.h>
using namespace std;
int main(){
freopen("test.txt","w",stdout);
string p;
for(int i=1;;i++){
p+=' ';
cout<<i<<endl;
}
return 0;
}
运行!
2分钟后……
已经生成了180MB的文件了,可是程序还没运行完。
【TWO THOUSANDS YEARS LATER】
uhh...
已经1GB了
所以……我看了看string的函数
哈哈!正合我意!
#include <bits/stdc++.h>
using namespace std;
int main(){
string p;
cout<<p.max_size();
return 0;
}
max_size()
我们得到了string的最长值是1073741820,也就是2²⁰-4
我觉得这是string给其他variable(比如说npos)的预留
本博客由百联又一博客冠名播出