请教一个C++问题

ymcymc 2003-10-18 05:49:24
#include <utility>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
int main()
{
string file_name;
cout <<"please enter file name:";
cin>>file_name;
ifstream infile(file_name.c_str(),ios::in);
if(!infile){
cerr<<"oops!unable to open file"
<<file_name<<"--bailing out!\n";
exit(-1);
}
else cout <<'\n';
vector<string,allocator> *lines_of_text=new vector<string,allocator>;
string textline;
typedef pair<string::size_type,int> stats;
stats maxline;
int linenum=0;
while(getline(inflie,textline,'\n')){
cout<<"line read:"<<textline<<'\n';
if(maxline.first<textline.size()){
maxline.first=textline.size();
maxline.second=linenum;
}
lines_of_text->push_back(textline);
linenum++;
}
return 0;
}
这个程序有哪些错误
...全文
43 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Beover1984 2003-10-18
  • 打赏
  • 举报
回复
C++标准化后,属于标准库的都不需要加.h,加.h的是为了兼容以前的C++程序
sinomesh 2003-10-18
  • 打赏
  • 举报
回复
想问一下,为什么加上.h不可以?
Wolf0403 2003-10-18
  • 打赏
  • 举报
回复
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string file_name;
cout <<"please enter file name:";
cin>>file_name;
ifstream infile(file_name.c_str(),ios::in);
if(!infile){
cerr<<"oops!unable to open file"
<<file_name<<"--bailing out!\n";
exit(-1);
}
else cout <<'\n';
vector<string> *lines_of_text=new vector<string>;
string textline;
typedef pair<string::size_type,int> stats;
stats maxline;
int linenum=0;
while(getline(infile,textline,'\n')){
cout<<"line read:"<<textline<<'\n';
if(maxline.first<textline.size()){
maxline.first=textline.size();
maxline.second=linenum;
}
lines_of_text->push_back(textline);
linenum++;
}
return 0;
}
ymcymc 2003-10-18
  • 打赏
  • 举报
回复
vector<string,allocator>中的allocator不需要指定
如果加上allocator是什么意思
coderStone 2003-10-18
  • 打赏
  • 举报
回复
学习!!
fireseed 2003-10-18
  • 打赏
  • 举报
回复
1. 少一句:#include <fstream>

2. 少一句:using namespace std;

3. vector<string,allocator>中的allocator不需要指定

4. infile误写为:inflie

coderStone 2003-10-18
  • 打赏
  • 举报
回复
#include <utility>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
=======================================
...
...
#include <string.h>
上面那些头文件应该都要有

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧