简体   繁体   English

为什么迭代器不返回到集合的开头?

[英]Why does the iterator not return to the beginning of the set?

I wrote a program in C++ to help me study for the GMAT. 我用C ++编写了一个程序来帮助我学习GMAT。 I use it to practice the times tables up to 25 in randomized order. 我用它来练习随机表最多25个时间表。 It works by generating a random number and asking the user to multiply that number by the times table that they want to work on. 它的工作原理是生成一个随机数,并要求用户将该数字乘以他们要处理的时间表。 The program adds that number to a set so that it is not repeated. 该程序将该数字添加到集合中,以使其不再重复。 Please find the code below: 请在下面找到代码:

int randumb(int a){
  random_device rd;
  mt19937 gen(rd());
  uniform_real_distribution <> dis(1,a+1);
  int u=dis(gen);
  return u;
}

void mult_tables(){
  cout<<"Which times table would you like to practice?"<<endl;
  int a, input;
  set<int> vect;
  cin>>a;
  int mult;
  for(int i=0; i<a; ++i){
    mult=randumb(a);
    if(i==0)
      vect.insert(mult);
    if(i!=0){
      for(set<int>::iterator it=vect.begin(); it!=vect.end(); ++it){
        if(mult==*it){
          mult=randumb(a);
          it=vect.begin();
        }
      }
      vect.insert(mult);
    }
    cout<<endl;
    cout<<a<<" x "<<mult<<" = ";
    cin>>input;
    if(input==a*mult)
      cout<<"Correct!"<<endl;
    else
      cout<<"Wrong."<<endl;
  }
} 

As you can see, the code compares each multiplier with the elements already in the set. 如您所见,该代码将每个乘数与集合中已存在的元素进行比较。 If a number is already in the set, it generates and new number and returns the iterator to the beginning of the set so that it can compare the new multiplier with ALL of the previously used numbers. 如果集合中已经有一个数字,它将生成新的数字,并将迭代器返回到集合的开头,以便它可以将新的乘数与所有先前使用的数字进行比较。 I stepped through the program in GDB and every time it hits it=vect.begin(); 我逐步浏览了GDB中的程序,并在每次it=vect.begin();该程序时执行it=vect.begin(); it returns the iterator to the SECOND element in the set. 它将迭代器返回到集合中的SECOND元素。 It happens every time and causes numbers to repeat. 每次都会发生,并导致数字重复。 Does anyone know why this is happening? 有人知道为什么会这样吗?

The logic error is that you are incrementing the iterator after the call to vect.begin() in the loop. 逻辑错误是您在循环中调用vect.begin()之后增加了迭代器。

What you need is: 您需要的是:

  for(set<int>::iterator it=vect.begin(); it!=vect.end(); /** ++it **/){
    if(mult==*it){
      mult=randumb(a);

      // Reset the iterator to the start.
      // Don't increment it.
      it=vect.begin();
    }
    else {
     // Increment the iterator.
     ++it;
    }
  }

Not a fix for your implementation but rather a cleaner way of doing the same thing. 这不是您的实现的解决方案,而是一种更干净的方法来完成相同的事情。

A set container ensures each value inside it is unique, so instead of checking if the number already exists it is easier to just add numbers as long as the size of the set is smaller than required. 集合容器可确保其中的每个值都是唯一的,因此,只要检查集合的大小小于要求的数量,就不用检查数字是否已存在,而仅添加数字会更容易。

Here is a code snippet: 这是一个代码片段:

std::set<int> nums;
while(nums.size() < REQUIRED_SIZE)
{
    nums.insert(randumb(a));
}

At this point you have a set of unique numbers, use it as you wish. 此时,您有一组唯一的数字,可以根据需要使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么递归 function 中的迭代器在返回后指向开头? - Why does an iterator in a recursive function point to the beginning after a return? 为什么迭代器操作符+返回副本? - Why does an iterator operator + return a copy? 为什么std :: advance不返回生成的迭代器? - Why does not std::advance return the resulting iterator? 为什么 std::array::begin 不返回迭代器? - Why does std::array::begin not return an iterator? 为什么 range::sort 返回一个迭代器? - Why does ranges::sort return an iterator? 为什么std :: set.insert()返回一个非常量迭代器,而我却不能修改它? - Why does std::set.insert() return a non-const iterator, and yet I cannot modify it? 为什么“std :: begin()”在这种情况下总是返回“const_iterator”? - Why does “std::begin()” always return “const_iterator” in such a case? 为什么向量的.at()成员函数返回引用而不是迭代器? - Why does .at() member function of a vector return a reference instead of an iterator? 为什么路径的迭代器在遍历时会返回“\\\\”? - Why does path's iterator return “\\” while traversing? 为什么string :: find返回size_type而不是迭代器? - Why does string::find return size_type and not an iterator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM