【问题标题】:I am trying to fill vector array with getline, what's wrong?我正在尝试用 getline 填充向量数组,怎么了?
【发布时间】:2017-12-31 21:21:06
【问题描述】:

我想用一些线填充一个向量数组,但我没有得到同样的结果。我的代码有什么问题?

using namespace std;

vector<string> calling(int n){
    vector<string> file(n);
    int a=0;

    while(n){
       getline(cin,file[a]);
       cin.ignore();
       a++;
       n--;
    }
    return file;
}

int main() {
    int n;
    cin >> n;
    vector<string> hrml = calling(n);
    int a=0;
    while(n){
        cout << hrml[a] <<endl;
        a++;
        n--;
     }
     return 0;
}

输入:

3
aaaa aaa aa
bb b bbbb
c cc ccc

输出:

aaaa aaa aa
bb b bbbb

【问题讨论】:

  • 删除getline(cin,file[a]);之后的cin.ignore();并在cin &gt;&gt; n;之后添加。

标签: c++ string vector getline


【解决方案1】:

此声明

cin >> n;

不会从输入缓冲区中删除换行符。因此,函数getline 的以下调用会读取一个空字符串,直到遇到换行符为止。

函数中的这条语句

cin.ignore();

没有意义。您应该在此声明之后放置类似的声明

cin >> n;

此外,如果您正在处理int 类型的对象,您应该检查它是否不小于零。

考虑到这一点,程序可以如下所示。

#include <iostream>
#include <string>
#include <vector>
#include <limits>

using namespace std;

vector<string> calling( int n )
{
    vector<string> file;

    if ( n > 0 )
    {
        file.reserve( n );
        string s;

        while ( n-- && getline( cin, s ) ) file.push_back( s );
    }

    return file;
}

int main() 
{
    int n = 0;

    cin >> n;

    cin.ignore( numeric_limits<streamsize>::max(), '\n' );

    vector<string> hrml = calling(n);

    for ( const string &s : hrml ) cout << s << '\n';

    return 0;
}

【讨论】:

    【解决方案2】:

    这是几个因素的组合。

    读取输入中的第一个数字3,不会将“光标”移动到下一行;它只是将其移过数字。下次调用 getline 时,它会返回大小为 0 的 std::string,因为它会抓取字符末尾和行尾之间的空间,这没什么。

    然后,因为您使用该数字作为要读取的字符串的明确数量,所以您正在读取 3 行:""、"aaaa aaa aa" 和 "bb b bbbb"。

    因此您的输出缺少'c' 的行。

    这里最简单的解决方案是在调用calling 之前强制额外调用getline。 (我已经从你的代码中删除了using namespace std;,因为它是bad practice)

    int main() {
        int n;
        std::cin >> n;
        std::string line;
        std::getline(std::cin, line);
    

    然后,我们可以重写calling,使其不需要整数参数:

    std::vector<std::string> calling(){
        std::vector<std::string> file;
        std::string line;
        while(std::getline(std::cin, line)){
            file.push_back(std::move(line));
        }
        return file;
    }
    

    (如果您在控制台中进行测试,并且需要标记行尾的内容,请在 Windows 上使用 Ctrl+Z 或 Ctrl+D 在 UNIX 上。)

    这将调用行更改为:

    std::vector<std::string> hrml = calling();
    

    或者,如果您使用的是 C++11(并且不介意丢失显式可见的类型声明):

    auto hrml = calling();
    

    只要我们在这里,我们就可以编写一个更好的循环来打印向量的内容:

    for(size_t i = 0; i < hrml.size(); i++) {
        std::cout << hrml[i] << std::endl;
    }
    

    或者,如果我们使用 C++11:

    for(std::string const& line : hrml) {
        std::cout << line << std::endl;
    }
    

    而且,如果允许您更改输入文件的格式,我们可以一开始就放弃读取数字,从而生成更小且更易于调试的程序:

    #include<iostream>
    #include<string>
    #include<vector>
    
    std::vector<std::string> calling(){
        std::vector<std::string> file;
        std::string line;
        while(std::getline(std::cin, line)) {
            file.push_back(std::move(line));
        }
        return file;
    }
    
    int main() {
        std::vector<std::string> hrml = calling();
    
        for(std::string const& line : hrml) {
            std::cout << line << std::endl;
        }
        return 0;
    }
    

    【讨论】:

    • 读号后用std::cin.ignore(1000,'\n');怎么样?
    • @ThomasMatthews 没有什么能阻止你这样做(......除了行长超过 1000 个字符的输入文件......)但我编写的代码更符合习惯。跨度>
    【解决方案3】:

    @DimChtz 回答了你的问题,但我想我会向你展示如何使用 for 循环,因为你似乎喜欢笨重的 while 循环:

    #include <vector>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    vector<string> calling(int n)
    {
        vector<string> file(n);
    
        //for loop
        for (int i=0; i<n; i++) {
            getline(cin, file[i]);
        }
    
        return file;
    }
    
    int main() {
        int n;
        cin >> n;
        cin.ignore();
    
        vector<string> hrml = calling(n);
    
        //for each loop (C++11)
        for (auto & str : hrml) {
            cout << str << endl;
        }
    
        return 0;
    }
    

    【讨论】:

      【解决方案4】:

      在getline() 之后不需要std::cin.ignore(),因为后者会“吃掉”流中的换行符。根据getline() docs:

      从输入中提取字符并将它们附加到 str 直到其中之一 发生以下情况(按列出的顺序检查): (...)

      b) 下一个可用的输入字符是 delim,由 Traits::eq(c, delim),在这种情况下分隔符是 从输入中提取,但不附加到 str。

      在std::cin &gt;&gt; n 之后确实需要std::cin.ignore(),尽管按下回车键后,您已将换行符放入流中,它会被getline 吃掉,结果将返回一个空字符串。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-28
        • 1970-01-01
        • 1970-01-01
        • 2022-12-09
        • 1970-01-01
        • 2021-04-07
        • 1970-01-01
        • 2013-11-25
        相关资源
        最近更新 更多