Istream_iterator问题,使用in循环
istream_iterator question, using in loop
为什么这个循环不会终止?程序在打印出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*/ }
更新:
所以这现在使更多的自。谢谢所有。根据提供的信息,我将其简化为更少的行。需要把模数部分放回去,但现在对我来说更清楚了。
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 }
更新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;
}
}
如果您将istream_iterator
连接到流,则该istream_iterator
将有效,直到它读取的流设置了failbit
或badbit
。只有当您从流中读取格式错误的数据,或者流中没有数据时,才会发生这种情况。
因为cin
从标准输入流中读取数据(在大多数系统中默认连接到键盘),istream_iterator
总是可以读取更多的数据。也就是说,在您输入数字列表之后,cin
将不会处于错误状态,因为您总是可以在程序中输入更多的数字。程序"冻结"的事实是由于它正在等待输入更多的输入。您可以通过在程序处理完您输入的所有数据后输入更多数据来验证这一点。
要使cin
停止读取数据,您有几个选项。首先,您可以让操作系统通过将数据输送到程序中或从文件中读取数据来重定向cin
。例如,在Linux系统上,您可以这样运行程序:
./my-program < my-input-file
一旦程序从my-input-file
中读取了所有文本,cin
将遇到文件结束并触发failbit
。然后循环终止。
或者,您可以显式地使cin
到达文件末尾。在Linux系统上,如果从命令行运行编程,可以按CTRL+D使标准输入发送文件结束信号,这也会导致循环退出。或者,您可以在cin
中输入一个无效的浮点值,例如字符串STOP!
。这将导致failbit
触发cin
,因为它不能将该值视为浮点数,然后将导致循环退出。
希望这对你有帮助!
程序运行良好,只是您必须在输入中提供文件结束符:
echo 1.1 5.3 | ./myprogram
如果你是在控制台上打字,发送Ctrl-D
来表示输入的结束。
(与此无关,为了清晰和语义,您的条件可能应该阅读if( numbers.size( ) % 2 != 0 )
;另外,一定要使用并注意编译器警告。
标准输入没有结束符(除非你通过管道输入);它永远继续。因此,istream
迭代器实际上永远不会到达EOF
,因为没有EOF
。
- 将 out/in out 参数与 if/switch 的 init 语句一起使用
- 当键值是 std 向量时,为什么使用 at in C++ 访问映射值如此缓慢?
- 如何使用列表/数组在C++中实现 Python "... not in ..."?
- 如何使用 in C++ 访问相对父目录<filesystem>
- 使用 new in C++(在 Windows 上)分配大于 2GB 的单个对象
- 数组在 C++ 中不使用 static in 函数时不会修改
- 使用 VS2017 构建 OpenSSL 后出现一些链接错误。 " OPENSSL_init_ssl referenced in function _main " ?
- 如何使用 std::in 和 getline() 检测后台进程中从 bash 注入的输入
- 如何使用 new in C++ 分配字节数内存
- 在使用 In Order 遍历成员函数时引发异常(堆栈溢出)时出现问题
- 使用多重性函数与 in C++ 联合两个链表
- 如何在 C++ 中使用 std::insert in 2D map
- 使用 static int 返回 int&in 函数
- 在函数调用中使用类型vector<pair<int,int>>::iterator&in
- 在单线程中使用 asio::async_write 时获得"the requested resource is in use"
- 当<set>我想从文本文件中读取时,使用 in C++ 处理重复项?
- 在不更改代码的情况下使用 .in 文件进行测试
- 为什么C++成员函数使用 & in 参数?
- Istream_iterator问题,使用in循环
- 如何在unix环境中使用.in和.out文件进行测试