2017-08-03 12 views
1

私はint型のベクトルに整数値を読み込もうとしています。私はユーザーが入力をクリックするまで、一行で整数を読みたいと思っています。私はgetl​​ineとstringstreamを使用しようとしていますが、enterを押した後も入力を探し続けます。どんな解決策ですか?整数をコンソールからベクタに読み込む方法

高レベルの説明:このプログラムはコンソールから数値を読み込み、ベクトルの後ろにプッシュします。次に、ベクトルがソートされ、2つのポインタが作成されて前後を指し示します。次に、ユーザは、2つのポインタの合計を取ることによって、線形時間内にプログラムが検索する合計を入力することができる。ポインタは、そのような合計を見つけるか、そのような合計が存在しないと判断するまで、一方向に移動し続けます。それはあなたに到達する前に、入力オペレータ>>

#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(); 
} 
+1

には、以下の答えのオプション2をチラッを与えます。唯一の違いは、ファイルではなく、cinを使用していることです。https://stackoverflow.com/a/7868998/4581301 – user4581301

+0

コード内で 'getline'と' stringstream'はどこにありますか? – Barmar

+0

'getline'と' stringstream'はこれを解決する正しい方法ですので、間違っていたはずです。 'getline'はループ内にあってはいけません。一度やり直してから、ループ内の' stringstream'から読み込みます。 – Barmar

答えて

1

cinは、すべての空白を食べるので、input\nになることはありません。

でも、それは最大の問題ではありません。 cin >> inputは、読み込まれたものを返すのではなく、ストリーム自体への参照を返します(here参照)。これはあなたのコードwhile (cin >> input != "\n")があなたが思うもの(正直言ってコンパイルすべきではない)をしていないことを意味します。ベクターに標準入力から整数の行を読み取るために

、あなたはとてもこのようなものでしょう:

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

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

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

これで、getline(cin、line)に到達するとSegmentationフォルトが発生します。 – Grigor

+0

@Grigorあなたはideone(ideone.com)のリンクを問題のコードに付けることができますか?直前のコードの別の行からのものかもしれません。 – scohe001

0

使用

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 
} 
// } 
+0

'std :: copy()'を 'std :: istream_iterator'と' std :: back_inserter'と一緒に使って 'while'ループを取り除くことができます。' std :: copy(std :: istream_iterator sstream)、std :: istream_iterator ()、std :: back_inserter(v)); ' –

+0

STLを使用するのは印象的ですが、経験豊かなプログラマー。場合によっては、これらのユーティリティーが短くて読みやすいコードになることもあります。 – jwilson

関連する問題