2017-10-12 84 views
-2

我有一個程序,提示用戶輸入一個值。用戶輸入的每個值都放入一個向量'other'中,這個值只用於驗證。如果輸入了重複值,則用戶將得到一個提示,直到他們輸入唯一值。防止重複進入矢量? C++

我面臨的主要問題是,由於某些原因,在運行代碼並打印出矢量結果時,似乎有重複條目。誰能告訴我爲什麼?

請參閱以下我的代碼:

// prompt to continue 
cout << "Would you like to continue? [Y]es, [N]o: "; 
cin >> toContinue; 

while (toContinue == 'Y') 
{ 
    bool isDuplicate = 0; 

    // prompt for product no. 
    cout << "Please enter product number: "; 
    cin >> productB; 

    // Validation check for duplicate entries 
    for (size_t i = 0; i < other.size(); i++) 
    { 
     if (productB == other[i]) 
      isDuplicate = 1; 

     while (isDuplicate == 1) 
     { 
      cout << "You have already entered this product number!" << endl; 
      cout << "Please enter correct product number: "; 
      cin >> productB; 

      if (productB != other[i]) 
       other.push_back(productB); 

      isDuplicate = 0; 
     } 
    } 
    // prompt to continue 
    cout << "Would you like to continue? [Y]es, [N]o: "; 
    cin >> toContinue; 
} 
+7

改爲使用'std :: set'。 – user0042

+0

你可能想用調試器來瀏覽你的程序。提示:當我輸入'3 4 3'時會發生什麼? – Rakete1111

+5

檢測到重複項目後,您要求用戶重新輸入。您可以根據之前輸入的一個號碼檢查此新號碼,但不能對所有先前輸入的號碼進行檢查。所以說,用戶再次輸入「1」,然後輸入「2」,然後輸入「1」。您檢測到重複並要求重新輸入。他們輸入'2',你只檢查'2!= 1',並愉快地將第二個'2'加到向量中。 –

回答

1

雖然它通常使用std::set獨特的元素,如果函數必須返回一個矢量由於某些原因,我用這樣的方法:

std::set<int> my_set; 

my_set.insert(1); 
my_set.insert(2); 
my_set.insert(1); 

// ... insert more 

std::vector<int> my_vector(my_set.size()); 
std::copy(my_set.begin(), my_set.end(), my_vector.begin()); 

assert(my_vector.size()==2); 

請注意,矢量my_vector將被排序。

1

一旦你輸入一個副本,你讓用戶重新輸入一個數字;那麼你只檢查新輸入的號碼是否與之前輸入的重複相同;但是您不檢查用戶是否輸入了不同但仍然重複的值。

通常,您將用戶輸入與程序邏輯混合;分拆這使得代碼更具可讀性並且不易出錯。參見,例如,下面的片段示出了如何人們可以分離這些顧慮:

bool isContained(const vector<int> &v, int value) { 
    // your code to check for duplicates goes here 
} 

int main() { 

    ... 

    while (toContinue == 'Y') { 

     // prompt for product no. 
     cout << "Please enter product number: "; 
     cin >> productB; 

     if (isContained(other, productB)) { 
     cout << "You have already entered this product number!" << endl; 
     } 
     else { 
     other.push_back(productB); 
     } 

     // prompt to continue 
     cout << "Would you like to continue? [Y]es, [N]o: "; 
     cin >> toContinue; 
    } 
} 

而且一般暗示:使用適當的數據結構還可以幫助避免碼的不必要的行;例如,避免重複的容器是std::set

1

您可以通過將邏輯組件分解爲更小的函數來幫助自己。

我在這裏完成的大部分工作都已經整理完畢,但請注意contains函數的封裝。

#include <vector> 
#include <iostream> 
#include <algorithm> 

using namespace std; 

bool contains(std::vector<int> const& vals, int val) 
{ 
    return std::count(std::begin(vals), std::end(vals), val) != 0; 
} 

bool shouldContinue() 
{ 
    char toContinue; 
    cout << "Would you like to continue? [Y]es, [N]o: "; 
    cin >> toContinue; 
    return toContinue == 'Y'; 
} 

int getProduct(bool again) 
{ 
    int productB; 
    if (again) 
    { 
     cout << "You have already entered this product number!" << endl; 
    } 
    cout << "Please enter correct product number: "; 
    cin >> productB; 
    return productB; 
} 

void printProducts(std::vector<int> const& vals) 
{ 
    std::cout << "You have selected:"; 
    const char* sep = " "; 
    for(int p : vals) 
    { 
     std::cout << sep << p; 
     sep = ", "; 
    } 
    std::cout << std::endl; 
} 


int main() 
{ 
    std::vector<int> other; 

    while (shouldContinue()) 
    { 
     int productB = getProduct(false); 
     while(contains(other, productB)) 
     { 
      productB = getProduct(true); 
     } 
     other.push_back(productB); 
    } 

    printProducts(other); 
}