防止重复进入矢量? C ++

时间:2017-10-12 14:46:24

标签: c++ vector

我有一个程序,提示用户输入值。用户输入的每个值都被放入一个向量“other”,该向量仅用于验证。如果输入了重复值,则用户将获得提示,直到他们输入唯一值。

我面临的主要问题是,由于某种原因,在运行代码并打印出向量的结果时,似乎存在重复的条目。谁能告诉我为什么会这样?

请参阅下面的代码:

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

3 个答案:

答案 0 :(得分: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 :(得分: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

答案 2 :(得分: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);
}