Chinaunix首页 | 论坛 | 博客
  • 博客访问: 479168
  • 博文数量: 120
  • 博客积分: 1853
  • 博客等级: 上尉
  • 技术积分: 1177
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-22 22:40
文章分类

全部博文(120)

文章存档

2013年(16)

2012年(104)

分类: C/C++

2012-06-02 10:43:27


点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <sstream>
  3. #include <vector>
  4. #include <fstream>
  5. #include <string>
  6. using namespace std;

  7. int main(){

  8.     ifstream ifs;
  9.     ifs.open("cpp");
  10.     vector<string> vec_str;//初始化时是空的
  11.     vector<string>::iterator iter = vec_str.begin();
  12.     while(getline(ifs, *iter))//input file to vector    此处产生段错误,因为此时vecs_str是空的,必须在前面定义个string,然后push_back()才可以
  13.     {
  14.         cout<<*iter<<endl;            
  15.     }
  16.     
  17.     return 0;
  18. }

正确的 可以如下:

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5. #include <vector>
  6. using namespace std;

  7. int main(){
  8.     
  9.     ifstream ofs;
  10.     ofs.open("cpp");
  11.     vector<string> vec_s;
  12.     cout<<"ok"<<endl;
  13.     string ss;
  14.     while(getline(ofs,ss)){
  15.         cout<<"getline"<<endl;
  16.         vec_s.push_back(ss);
  17.         istringstream oss(ss);
  18.         string single;
  19.         while(oss >> single){
  20.             cout<<single<<endl;;
  21.         }
  22.     }

  23.     ofs.close();

  24.     return 0;
  25. }

阅读(1847) | 评论(0) | 转发(0) |
0

上一篇:函数指针和内联函数

下一篇:find 命令

给主人留下些什么吧!~~