c++ - istream_iterator问题,在循环中使用

标签 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*/ }  

更新:

所以这现在赚了更多。谢谢大家。使用提供的信息,我将事情简化为更少的行。需要重新获得模数部分,但现在我更清楚了。

 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!。这会导致 failbitcin 上触发,因为它不能将值视为 float ,然后会导致循环退出。

希望这对您有所帮助!

关于c++ - istream_iterator问题,在循环中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6886653/

相关文章:

C++白行跳过错误

c++ - 使用队列的 Phantom Bug(STL 库),Windows/MingW/G++

c++ - istream_iterator cin 初始化等待输入

c++ - istream_iterator 遍历二进制文件中的字节

c++如何读取文件并拆分它的行

带循环的字符串的 C++ 输入验证

c++ - 重载强制转换运算符的优先级

c++ - 是否可以在编译时打印出 C++ 类的大小?

C++ 库 API。使用转换器类而不是普通的 C api

c++ - STL 在 C++ 中的强大功能