简体   繁体   English

C ++ rbegin修改reverse_iterator的地址

[英]C++ rbegin modify address of reverse_iterator

I have a strange problem in c++ with this code: 我在C ++中使用以下代码遇到了一个奇怪的问题:

mutex_type  list_mutex;
typedef list<char*> RQueue;
RQueue rQueue;
RQueue::reverse_iterator  rstart, rend, last;

  1  while(true) {
  2      LockMutex(list_mutex);
  3      rstart = rQueue.rbegin();
  4      rend   = rQueue.rend();
  5      while( (rstart != rend) && (rstart != last) ) {
  6           print *rstart;
  7      }
  8      last = rQueue.rbegin(); 
  9      UnlockMutex(list_mutex);
  10  }
  • rQueue is a queue in which I iterate in reverse order rQueue是一个我以相反顺序进行迭代的队列
  • rQueue can receive messages at any time rQueue可以随时接收消息
  • I added the iterator last in order to avoid reworking with a receive message at line 6 last添加了迭代器,以避免在第6行处理接收消息
  • At line 8, I retain the position from where I printed the messages and I want to only print the messages that are newer than the last message. 在第8行,我保留了打印消息的位置,并且只想打印比上一条消息更新的消息。

    My problem: When a iteration is finished and new messages are added in the queue, the value of iterator last is changed, becoming the same as the value of the iterator rstart , therefore the new arrived messages are not printed at line 6. 我的问题:当迭代完成并将新消息添加到队列中时,迭代器last的值将更改,变得与迭代器rstart的值相同,因此新到达的消息不会在第6行打印。

I don't know why last = rQueue.rbegin() modifies its value when receiving new elements after unlocking the queue. 我不知道为什么在解锁队列后last = rQueue.rbegin()在接收新元素时会修改其值。

Thanks. 谢谢。

If you set an iterator to rbegin() , it will always keep pointing to the last element of the list. 如果将迭代器设置为rbegin() ,它将始终指向列表的最后一个元素。 If you add another element at the back, the iterator will point still point to the last element (which now is the new one). 如果在后面添加另一个元素,则迭代器将仍然指向最后一个元素(现在是新元素)。 It will not change, it just keeps pointing to the end. 它不会改变,只是不断指向终点。

I did this test: 我做了这个测试:

list<const char *> my_list;
my_list.push_back("msg 1");

list<const char*>::reverse_iterator it = my_list.rbegin();

cout << "Iterator is " << *it << endl;

my_list.push_back("msg 2");
my_list.push_back("msg 3");
my_list.push_back("msg 4");

cout << "Iterator is " << *it << endl;

This program gives the output: 该程序给出输出:

Iterator is msg 1
Iterator is msg 4

I have this other solution you might use which does not use a reverse iterator. 我有可能使用的其他解决方案,它不使用反向迭代器。 Instead, the addMessage() -function updates read_pos to the newest message. 相反, addMessage()函数将read_pos更新为最新消息。 If read_pos does not point to the end, it is not changed either. 如果read_pos没有指向结尾,则也不会更改。 This allows printMessage() to print all messages which where added since last time it ran. 这允许printMessage()打印自上次运行以来添加的所有消息。

Notice that I have only tested this without the locking. 请注意,我仅在没有锁定的情况下对此进行了测试。

mutex_type  list_mutex;
typedef list<const char*> RQueue;
RQueue rQueue;

RQueue::iterator read_pos;

void addMessage(const char *message) {
    LockMutex(list_mutex);

    rQueue.push_back(message);

    if (rQueue.size() == 1) {
        read_pos = rQueue.begin();
    }
    else if (read_pos == rQueue.end()) {
        read_pos--;
    }

    UnlockMutex(list_mutex);
}

void printMessage() {
  RQueue::iterator prev_pos;

  while (true) {
    LockMutex(list_mutex);

    if (rQueue.size() == 0) {
          UnlockMutex(list_mutex);
          continue;
    }

    RQueue::iterator end = rQueue.end();
    while (read_pos != end) {
        cout << *read_pos << endl;
        read_pos++;
    }

    UnlockMutex(list_mutex);
  }
}

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

相关问题 对于std :: reverse_iterator c ++,operator!=不明确 - Operator != is ambiguous for std::reverse_iterator c++ 带有cout和reverse_iterator的C ++奇怪输出 - C++ strange output with cout and reverse_iterator 为我的字符串类实现reverse_iterator(还有rbegin()和rend()方法) - implement reverse_iterator for my string class (also rbegin() and rend() methods) 如何在C ++中将DRY原则应用于迭代器? (iterator,const_iterator,reverse_iterator,const_reverse_iterator) - How do I apply the DRY principle to iterators in C++? (iterator, const_iterator, reverse_iterator, const_reverse_iterator) 如何编写 c++ function 什么可以返回迭代器或 reverse_iterator - How to write a c++ function what can return either iterator or reverse_iterator C++ STL:由于缺少迭代器和反向迭代器的基类而导致重复代码 - C++ STL: Duplicating code due to missing base-class for iterator and reverse_iterator reverse_iterator适配器 - reverse_iterator adapter C ++自定义集合reverse_iterator具有与std :: vector实现类似的行为 - C++ custom collection reverse_iterator with similar behaviour to std::vector implementation 为什么c ++容器不实现擦除(reverse_iterator位置)? - Why c++ containers do not implement erase( reverse_iterator position )? iterator vs reverse_iterator - iterator vs reverse_iterator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM