首页 > 试题广场 >

有以下程序 #include ...

[单选题]
有以下程序
#include<iostream> 
#include<fstream> 
#include<string> 
using namespace std; 
int main()  {
    ofstream File1("text.txt");  
    string d("20160314"); 
    string y=d.substr(0,4); 
    int k=d.find("2");
    int i=d.find("3"); 
    string m=d.substr(k+2,i-k); 
    string dd=d.substr(i+1,2); 
    string n=dd+m+y; 
    File1<<n<<endl; 
    File1.close(); 
}                                            
文件text.txt中写入的结果是(      )?
  • 20160314
  • 14032016
  • 14160312016
  • 20031416
c++中substr函数第一个参数指位置,第二个参数指个数,取值是左闭右开
y:"2016",k:0,i:5
m=substr(2,5),值为:"16031"
dd=substr(6,2),值为:"14"
因此,n=dd+m+y的值为"14160312016"

发表于 2020-08-28 10:18:11 回复(2)