2017-08-03 90 views
1

我想從控制檯讀取整數到我的整數向量。我希望從單行讀取整數直到用戶點擊輸入。我一直在嘗試使用getline和stringstream,但在按下Enter鍵後,它一直在尋找輸入。任何解決方案如何從控制檯讀取整數到與cin向量

高級描述:該程序從控制檯讀入數字,並將它們推到矢量的後面。然後對矢量進行排序,並創建兩個指針指向前後。然後用戶可以輸入一個總和,程序將通過取兩個指針的總和以線性時間進行搜索。然後指針將繼續向一個方向移動,直到他們找到這樣的總數或確定不存在這樣的總和。

#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); – 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 :: istream_iterator'和'std :: back_inserter'使用std :: copy()來擺脫'while'循環,例如:'std :: copy(std :: istream_iterator ( std :: istream_iterator (),std :: back_inserter(v));' –

+0

雖然(得到它?)這是令人印象深刻的STL使用,但我不得不說更難以理解有經驗的程序員。有時這些實用程序會導致更短,更易讀的代碼,有時它們不會。 – jwilson