0

getline()我正在 Visual Studio 2010 中完成一些 C++ 练习,并且在使用该函数时,当我尝试使用“CTRL-Z”终止流中的标准时,我一直遇到无限循环问题。这是相关的代码....

// find all the lines that refer to each word in the input
map<string, vector<int> >
    xref(istream& in,
         vector<string> find_words(const string&) = split)
{
    string line;
    int line_number = 0;
    map<string, vector<int> > ret;

    // read the next line
    while (getline(in, line)) {
        ++line_number;

        // break the input line into words
        vector<string> words = find_words(line);

        // remember that each word occurs on the current line
        for (vector<string>::const_iterator it = words.begin();
             it != words.end(); ++it)
            ret[*it].push_back(line_number);
    }
    return ret;
}

...程序没​​有将我踢出 while 循环,而是进入了打印随机整数的无限循环。我很确定这是我所缺少的 Windows 环境特有的东西。这是整个代码...

#include <algorithm>
#include <cctype>
#include <string>
#include <vector>

#include "split.h"

using std::find_if;
using std::string;
using std::vector;

using std::isspace;

// `true' if the argument is whitespace, `false' otherwise
bool space(char c)
{
    return isspace(c);
}

// `false' if the argument is whitespace, `true' otherwise
bool not_space(char c)
{
    return !isspace(c);
}

vector<string> split(const string& str)
{
    typedef string::const_iterator iter;
    vector<string> ret;

    iter i = str.begin();
    while (i != str.end()) {

        // ignore leading blanks
        i = find_if(i, str.end(), not_space);

        // find end of next word
        iter j = find_if(i, str.end(), space);

        // copy the characters in `[i,' `j)'
        if (i != str.end())
            ret.push_back(string(i, j));
        i = j;
    }
    return ret;
}

#include <map>
#include <iostream>
#include <string>
#include <vector>

#include "split.h"

using std::cin;            using std::cout;
using std::endl;           using std::getline;
using std::istream;        using std::string;
using std::vector;         using std::map;

// find all the lines that refer to each word in the input
map<string, vector<int> >
    xref(istream& in,
         vector<string> find_words(const string&) = split)
{
    string line;
    int line_number = 0;
    map<string, vector<int> > ret;

    // read the next line
    while (getline(in, line)) {
        ++line_number;

        // break the input line into words
        vector<string> words = find_words(line);

        // remember that each word occurs on the current line
        for (vector<string>::const_iterator it = words.begin();
             it != words.end(); ++it)
            ret[*it].push_back(line_number);
    }
    return ret;
}

int main()
{
    // call `xref' using `split' by default
    map<string, vector<int> > ret = xref(cin);

    // write the results
    for (map<string, vector<int> >::const_iterator it = ret.begin();
         it != ret.end(); ++it) {
        // write the word
        cout << it->first << " occurs on line(s): ";

        // followed by one or more line numbers
        vector<int>::const_iterator line_it = it->second.begin();
        cout << *line_it;   // write the first line number

        ++line_it;
        // write the rest of the line numbers, if any
        while (line_it != it->second.end()) {
            cout << ", " << *line_it;
            ++line_it;
        }
        // write a new line to separate each word from the next
        cout << endl;
    }

    return 0;
}
4

1 回答 1

0

我认为与其试图完成这项工作,不如从编写我能理解的代码开始(为了理解它,代码必须相当简单):

#include <map>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include "infix_iterator.h"

typedef std::map<std::string, std::vector<unsigned> > index;

namespace std {
ostream &operator<<(ostream &os, index::value_type const &i) { 
    os << i.first << ":\t";
    std::copy(i.second.begin(), i.second.end(),
        infix_ostream_iterator<unsigned>(os, ", "));
    return os;
}
}

void add_words(std::string const &line, size_t num, index &i) { 
    std::istringstream is(line);
    std::string temp;

    while (is >> temp)
        i[temp].push_back(num);
}

int main() { 
    index i;
    std::string line;
    size_t line_number = 0;

    while (std::getline(std::cin, line))
        add_words(line, ++line_number, i);

    std::copy(i.begin(), i.end(), 
        std::ostream_iterator<index::value_type>(std::cout, "\n"));
    return 0;
}

与(或多或少)通常一样,这需要infix_ostream_iterator我在其他地方发布的内容。

于 2012-08-17T19:54:10.357 回答