Mat*_*gan 2 c++ stl istream-iterator
为什么这个循环不会终止?程序打印出istream_iterator中的所有元素后冻结.
/*30*/ int main( int arc, char **arv ) {
/*31*/ vector<float> numbers( 0 );
/*32*/ cout << "Please, input even number of floats: ";
/*33*/ istream_iterator<float> iit (cin);
/*34*/ istream_iterator<float> eos;
/*35*/ while( iit != eos ) {
/*36*/ cout << (*iit) << endl;
/*37*/ iit++;
/*38*/ }
/*39*/ if( numbers.size( ) & 1 == 1 ) {
/*40*/ cerr << "Sorry: you must input"
/*41*/ << " an even number of inputs" << endl;
/*42*/ }
/*43*/ return ( 0 );
/*44*/ }
Run Code Online (Sandbox Code Playgroud)
更新:
所以现在这样做更多.谢谢大家.通过提供的信息,我将简化的内容简化为更少.需要重新获得模数部分,但现在对我来说更清楚了.
34 int main( int arc, char **arv ) {
35 vector<float> num;
36 cout << "Please, input even number of floats: ";
37 for( istream_iterator<float> iit (cin);
38 iit!=istream_iterator<float>( );iit++ ) {
39 num.push_back( (*iit) );
40 }
41 }
Run Code Online (Sandbox Code Playgroud)
更新2:如果有人还在阅读这个帖子,我可以在"最终"产品上获得社区的内容/风格反馈吗?
/********************************************************************
* Author: Mattew Hoggan
* Description: Write a client program that uses the data type point.
* Read a sequence of points (pairs of floating-point numbers) from
* standard input, and find the one that is closest to the first.
* *****************************************************************/
#include <math.h>
#include <iostream>
#include <vector>
#include <istream>
#include <iterator>
#include <algorithm>
#include <limits.h>
using namespace std;
typedef struct point {
float x;
float y;
} point;
float calc_distance( const point *a, const point *b ) {
return sqrt( pow( a->x-b->x, 2.0 ) + pow( a->y-b->y, 2.0 ) );
}
void print_point( point *a ) {
cout << "(" << a->x << ", " << a->y << ") ";
}
void delet_point( point *a ) {
delete a;
}
vector<point*>* form_pairs( vector<float> &num ) {
vector<point*> *points=NULL;
if( ( num.size( ) & 1 ) == 1 ) {
cerr << "ERROR: You input: " << num.size( )
<< " which is odd, failed to build vector "
<< endl;
return points;
} else {
cout << "Going to build points" << endl;
points = new vector<point*>;
for( vector<float>::iterator vit=num.begin( );
vit!=num.end( ); vit+=2 ) {
point *in = new point( );
in->x = *(vit);
in->y = *(vit+1);
points->push_back( in );
}
return points;
}
}
void pop_front( vector<point*> *pairs ) {
reverse( pairs->begin( ), pairs->end( ) );
pairs->pop_back( );
reverse( pairs->begin( ), pairs->end( ) );
}
void min_euclidean_distance( vector<point*> *pairs ) {
if( pairs->size( ) == 1 ) {
cerr << "You already know the answer to this" << endl;
return;
}
point *first = pairs->front( );
pop_front( pairs );
point *second = pairs->front( );
pop_front( pairs );
for_each( pairs->begin( ),pairs->end( ),print_point );
cout << endl;
point *p_min = second;
float f_min = calc_distance( first,second );
for( vector<point*>::iterator pit = pairs->begin( );
pit != pairs->end( ); pit++ ) {
float tmp = calc_distance( first,(*pit) );
if( tmp < f_min ) {
f_min = tmp;
p_min = (*pit);
}
}
cout << "The closest node to "; print_point( first );
cout << " is "; print_point( p_min );
cout << " at " << f_min << " units away " << endl;
delete first;
delete second;
}
int main( int arc, char **arv ) {
vector<float> num;
cout << "Please, input even number of floats: ";
for( istream_iterator<float> iit (cin);
iit!=istream_iterator<float>( );iit++ ) {
num.push_back( (*iit) );
}
vector<point*>* pairs = form_pairs( num );
if( pairs ) {
min_euclidean_distance( pairs );
for_each( pairs->begin( ),pairs->end( ),delet_point );
delete pairs;
}
}
Run Code Online (Sandbox Code Playgroud)
tem*_*def 10
如果您将istream_iterator一个流连接到一个流,那么istream_iterator它将在它读取的流具有failbit或badbit设置之前有效.只有当您从流中读取格式错误的数据或流用尽数据时,才会发生这种情况.
因为cin从标准输入流(默认情况下在大多数系统上连接到键盘)istream_iterator读取数据,所以总能读取更多数据.也就是说,在输入数字列表后,cin将不会处于错误状态,因为您始终可以在程序中输入更多数字.程序"冻结"的事实是由于它正在等待输入更多输入.您可以在程序处理您输入的所有数据后输入更多数据来验证这一点.
要使cin数据停止读取,您有几种选择.首先,您可以cin通过将数据传输到程序或从文件中读取数据来重定向操作系统.例如,在Linux系统上,您可以像这样运行您的程序:
./my-program < my-input-file
Run Code Online (Sandbox Code Playgroud)
一旦程序读取了所有文本my-input-file,cin将遇到文件结束并将触发failbit.循环然后终止.
或者,您可以明确地导致cin命中文件结尾.在Linux系统上,如果从命令行运行编程,可以按CTRL + D使标准输入发送文件结束信号,这也会导致循环退出.或者,您可以输入无效的浮点值cin,例如字符串STOP!.这会导致failbit触发cin,因为它不能将值视为float,然后会导致循环退出.
希望这可以帮助!
该程序运行正常,只需要在输入中提供文件结尾:
echo 1.1 5.3 | ./myprogram
Run Code Online (Sandbox Code Playgroud)
如果你真的在控制台上打字,发送Ctrl-D信号表示输入结束.
(不相关的,您的条件可能应该是if( numbers.size( ) % 2 != 0 )为了清晰度和语义而阅读;另外,请编译并注意编译器警告.)
| 归档时间: |
|
| 查看次数: |
1318 次 |
| 最近记录: |