【问题标题】:How to read integers from console into vector with cin如何使用 cin 将整数从控制台读取到向量中
【发布时间】:2017-08-03 18:26:23
【问题描述】:

我正在尝试将整数从控制台读取到我的整数向量中。我想继续从一行中读取整数,直到用户单击输入。我一直在尝试使用 getline 和 stringstream,但在我按下回车后它一直在寻找输入。有什么解决办法吗?

高级描述:该程序从控制台读取数字并将它们推送到向量的后面。然后对向量进行排序,并创建两个指针来指向前后。然后,用户可以输入一个总和,然后程序将通过取两个指针的总和在线性时间内搜索。然后指针将继续向一个方向移动,直到它们找到这样的总和或确定不存在这样的总和。

#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;

int findSum(vector<int> tempVec, int sum)
{
    int i;
    cout << "Sorted sequence is:";
    for ( i = 0; i < tempVec.size(); i++ )
        cout << " " << tempVec[i];
    cout << endl;

    int *ptr1 = &tempVec[0];
    cout << "ptr1 points to: " << *ptr1 << endl;
    int *ptr2 = &tempVec[tempVec.size() - 1];
    cout << "ptr2 points to: " << *ptr2 << endl;

    int count = 0;
    while ( ptr1 != ptr2 )
    {

        if ( (*(ptr1) + *(ptr2)) == sum )
        {
            cout << *(ptr1) << " + " << *(ptr2) << " = " << sum;
            cout << "!" << endl;
            return count;
        }
        if ( (*(ptr1) + *(ptr2)) < sum)
        {
            cout << *(ptr1) << " + " << *(ptr2) << " != " << sum;
            ptr1 = ptr1 + 1;
            cout << ". ptr1 moved to: " << *ptr1 << endl;
            count++;
        }
        else 
        {
            cout << *(ptr1) << " + " << *(ptr2) << " != " << sum;
            ptr2 = ptr2 - 1;
            cout << ". ptr2 moved to: " << *ptr2 << endl;
            count++;
        }
    }
    return -1;
}

int main()
{
    int ValSum;
    cout << "Choose a sum to search for: ";
    cin >> ValSum;


    vector<int> sumVector;
    int input;
    cout << "Choose a sequence to search from: ";
    while ( cin >> input != "\n" )
    {
        //getline(cin, input);
        if ( cin == '\0' )
            break;
        sumVector.push_back(input);
    }
    sort(sumVector.begin(), sumVector.end());


    int count = findSum(sumVector,ValSum);
    if ( count == -1 )
        cout << "\nThe sum " << ValSum << " was NOT found!" << endl;
    else 
    {
        cout << "\nThe sum " << ValSum << " was found!" << endl;
        cout << count + 1 << " comparisons were made." << endl;
    }
    sumVector.clear();
} 

【问题讨论】:

  • 请看一下以下答案的选项 2。唯一的区别是您使用的是 cin 而不是文件:stackoverflow.com/a/7868998/4581301
  • 您的代码中的getlinestringstream 在哪里?
  • getlinestringstream 是解决这个问题的正确方法,所以你一定做错了。 getline 不应该在循环中 - 你这样做一次,然后从循环中的 stringstream 中读取。
  • 如果您发布该代码,我们可以帮助您修复它。

标签: c++ vector sstream


【解决方案1】:

cin 和输入运算符 &gt;&gt; 在到达你之前会吃掉所有的空格,所以 input 永远不会是 \n

但这还不是最大的问题。 cin &gt;&gt; input 不会返回刚刚读取的内容,而是对流本身的引用(请参阅here)。这意味着您的代码 while ( cin &gt;&gt; input != "\n" ) 并没有按照您的想法执行(老实说,这甚至不应该编译)。

要将一行整数从标准输入读入向量,你可以这样:

string line;
int num;
vector<int> v;

getline(cin, line);
istringstream iss(line);

while(istringstream >> num) {
    v.push_back(num);
}

【讨论】:

  • 这样,一旦我到达 getline(cin, line); 就会出现分段错误;
  • @Grigor 你能给个ideone(ideone.com)链接吗?这可能是您之前代码中另一行的内容。
【解决方案2】:

使用

std::vector<int> v;
std::string line;

// while (!v.empty()) { // optional check to make sure we have some input
std::getline(std::cin, line); // gets numbers until enter is pressed
std::stringstream sstream(line); // puts input into a stringstream
int i;

while (sstream >> i) { // uses the stringstream to turn formatted input into integers, returns false when done
    v.push_back(i); // fills vector
}
// }

【讨论】:

  • 您可以通过使用std::copy()std::istream_iteratorstd::back_inserter 来摆脱while 循环,例如:std::copy(std::istream_iterator&lt;int&gt;(sstream), std::istream_iterator&lt;int&gt;(), std::back_inserter(v));
  • 虽然(明白吗?)这是一个令人印象深刻的 STL 用法,但我不得不说,对于经验不足的程序员来说,它更难理解。有时这些实用程序会导致代码更短、更易读,有时则不然。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-15
相关资源
最近更新 更多