注册 登录
编程论坛 C++教室

怎么用 vector 储存 字符

charderkk 发布于 2011-01-21 22:24, 583 次点击
  请问 想要把一篇文章读入vector中,如下
  vector<string> passage;
  string x;
  cout<<"Please input one passage:"<<endl;
  while(cin>>x)
      passage.push_back(x);
但是用cmd运行时,怎么才能使系统知道文章已经结束,有什么比较简单的方法吗?
  谢了
5 回复
#2
rjsp2011-01-22 08:20
这个和C++无关,各个平台使用不同的方式结束输入,有的是 Ctrl^z,有的是 Ctrl^D,……
#3
pangding2011-01-22 11:27
正如楼上所言。
#4
charderkk2011-01-22 19:26
回复 2楼 rjsp
可是怎么确定自己的平台是什么方式呢
#5
charderkk2011-01-22 20:09
回复 3楼 pangding
  #include<iostream>
  #include<string>
  #include<vector>
 
  using std::cout;   using std::cin;
  using std::string; using std::vector;
  using std::endl;   
  typedef vector<string>::size_type vec_sz;
  
  int main(){
  vector<string> passage;
  string x;
  cout<<"Please input one passage:"<<endl;
  while(cin>>x)
      passage.push_back(x);

  vec_sz  count=0,size=passage.size();
  cout<<"The passage is:";
  for(count=0;count!=size;count++)
  cout<<passage[count];
  return 0;
  }   
试了一下 我的系统结束输入是ctrl^c,
但用cmd运行该程序时,每次都会出现不同结果:想问下是什么原因

E:\cpp>test
Please input one passage:
safsadfsdfasdfsadfsdfsf42sa3f41
The passage is:safsadfsdfasdfsadfsdfsf42sa3f41

E:\cpp>test
Please input one passage:
safsadfsdfasdfsadfsdfsf42sa3f41
The passage is:^C

E:\cpp>test
Please input one passage:
safsadfsdfasdfsadfsdfsf42sa3f41
The passage is:safsadfsdfasdfsadfsdfsf42sa3f41^C
#6
charderkk2011-01-22 23:52
借贴求问
  #include<iostream>
  #include<string>  
  #include<vector>
  #include<algorithm>

  using namespace std;
  typedef vector<double>::size_type vec_sz;
  
  int main(){
  vector<string> passage;
  vector<double> numble;
  string x;
  cout<<"Please input one passage:"<<endl;
  cin>>x;
  while(x!="0"){
      passage.push_back(x);
      cin>>x;
  }
  vec_sz count,size=passage.size();

  cout<<"The original passage is:"<<endl;
  for(count=0;count!=size;count++){
      cout<<passage[count];
  }  
  /*for(count=0;count!=size;count++)
      numble[count]=passage[count].size();*/
  for(count=0;count!=size;count++)
      cout<<numble[count];
  sort(numble.begin(),numble.end());
  for(count=0;passage[count].size()==numble[0];count++)
      cout<<"The shortest words are:"<<passage[count]<<endl;
  for(count=0;passage[count].size()==numble[size-1];count++)
      cout<<"The longest words are:"<<passage[count]<<endl;
  return 0;
  }



return 0;
}
  为什么编译可以通过  但是一运行就错误,觉得/* 之间那句有问题,但是考虑后面要对字符长度进行排序,不这样不好办,请各位高手指点,非常感谢。
1