我试图用getline填充矢量数组,这有什么不对?

时间:2017-07-25 19:25:13

标签: c++ string vector getline

我想用一些线条填充矢量数组,但我不会得到同样的回复。我的代码有什么问题?

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

4 个答案:

答案 0 :(得分:1)

std::cin.ignore()之后您不需要getline(),因为后者会吃掉{&1;}。流中的新行字符。根据{{​​3}}:

  

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

     

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

std::cin.ignore()之后你确实需要std::cin >> n,但是输入你已经将换行符放入流中并且它会被getline吃掉,这将会返回结果是一个空字符串。

答案 1 :(得分:1)

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

#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;
}

答案 2 :(得分:1)

这是几个因素的组合。

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

然后,因为您使用该数字作为要阅读的明确数量的字符串,所以您只需阅读3行:"""aaaa aaa aa""bb b bbbb"

因此你的输出错过了'c'的行。

此处最简单的解决方案是在致电getline之前强制拨打calling(我已从您的代码中移除了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 或< UNIX上的kbd> Ctrl + D 。)

将调用行更改为:

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;
}

答案 3 :(得分: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;
}
相关问题