c++ - std::list 的重载括号运算符

标签 c++

我想知道我将如何创建一个重载的括号运算符来提供 std::list 中元素的位置。

我类的列表声明如下:

std::list<Stimulation*> configuration;

重载的括号运算符声明是:

std::list<Stimulation*>::operator[](const int); 

我认为括号运算符的定义如下:

std::list<Stimulation*>::operator[](const int position)
{
  auto i = configuration.begin();
  return i + position;

  ***OR***

  std::list<Stimulation*>::iterator i = configuration.begin();
  return i + position;
}

我是这个概念的新手,所以如果能以正确的方式提供任何帮助,我将不胜感激。在这种情况下,我需要专门使用一个列表,因为我知道其他容器包括括号运算符作为库的一部分。再次感谢您的耐心等待。

最佳答案

std::list 元素在内存中不连续,这与 std::vector 元素不同,这就是为什么不存在 [] 运算符的原因,因为这将是低效的,这不是制作列表的原因。

但是,作为练习,您可以使用 for 循环来实现它。这是我天真的实现,缺少 const 版本,并且在越界时断言失败:

#include <list>
#include <iostream>
#include <cassert>

using namespace std;

class MyList : public list<int>
{
public:

    int &operator[](int pos)
    {
       int count=0;
       for (auto &it : *this)
       {
          if (count==pos) { return it;}
          count++;
       }
     assert(false);
   }
};


int main()
{
    MyList l;
    l.push_back(1);
    l.push_back(2);
    l.push_back(3);
    l.push_back(4);
    cout << l[2] << endl;
    return 0;
}

如果元素位于列表末尾 (O(N)),访问时间会非常糟糕,因为您无法将位置添加到 list::begin()

我想你可以“缓存”最后一个请求的偏移量和迭代器,这样如果调用者请求偏移量+1(这在程序中很常见)你可以直接前进而不用从头开始恢复。

注意:刚看到关于std::advance的评论。那里没有用过(不知道)。

关于c++ - std::list 的重载括号运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40794853/

相关文章:

c++ - 如果添加了虚函数,Build是否需要依赖DLL

c++ - 获取C++类成员变量全名

c++ - 如何使用自定义光标防止系统鼠标光标在 C++ 应用程序中移动?

c++ - RAII 的有用性无一异常(exception)

c++ - C++ 中对象的大小

c++ - 为什么这个整数会无限下降?

c++ - winrt/c++ : await result from dispatched task

c++ - 用192/256位整数求和无符号64位整数 vector 的点积的最快方法?

c++ - 将 constexpr int[] 转换为 void*

c++ - cv::Point2d 的 OpenCV 平方范数