小编BaC*_*aCh的帖子

自己的容器类的C++迭代器和const_iterator问题

我正在写一个自己的容器类,遇到了一个我无法理解的问题.这是显示问题的裸骨样本.

它由一个容器类和两个测试类组成:一个使用std:vector的测试类,它可以很好地编译,第二个测试类试图以完全相同的方式使用我自己的容器类但是很难编译.

#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;

template <typename T>
class MyContainer
{
public:

  class iterator
  {
  public:
    typedef iterator self_type;
    inline iterator() { }
  };

  class const_iterator
  {
  public:
    typedef const_iterator self_type;
    inline const_iterator() { }
  };

  iterator begin() {
    return iterator();
  }

  const_iterator begin() const {
    return const_iterator();
  }
};

// This one compiles ok, using std::vector
class TestClassVector
{
public:
  void test() {
    vector<int>::const_iterator I=myc.begin();
  }

private:
  vector<int> myc;
};

// this one …
Run Code Online (Sandbox Code Playgroud)

c++ containers stl const-iterator

14
推荐指数
1
解决办法
9970
查看次数

传递并调用成员函数(boost :: bind/boost :: function?)

我有一个可能非常简单的问题:在类中传递并调用成员函数.我知道我想使用BOOST绑定(和/或函数),但我还没有真正掌握它的概念.

以下代码编译并执行问题.但是当我想将"f3"函数更改为非静态类函数时,乐趣就开始了:

#include <iostream>
#include <inttypes.h> 
#include <boost/bind.hpp>
#include <boost/function.hpp>

class Test
{
public:
  void f1();
private:
  void f2(void (*callfunc)(uint32_t));
  static void f3(uint32_t x);
};

void Test::f1(){
  f2(f3);
}

void Test::f2(void (*callfunc)(uint32_t)){
  (*callfunc)(42);
}

void Test::f3(uint32_t x){
  std::cout << "x: " << x << std::endl;
}

int main(int argc, char ** argv)
{
  Test ct;
  ct.f1();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在,改变之后

static void f3(uint32_t x);
Run Code Online (Sandbox Code Playgroud)

void f3(uint32_t x);
Run Code Online (Sandbox Code Playgroud)

编译器不满意并告诉我"错误:没有匹配函数调用'Test :: f2()'"

阅读了一些关于boost :: bind和boost :: function的SO帖子,我想我需要改变f2()的定义以及f1()如何调用f2()给f3()作为调用目标,但是除此之外......关于boost :: bind和boost函数的每个组合,我都试过很难编译.

我该如何写这个?作为一个额外的问题:是否有关于boost :: …

c++ boost-bind boost-function

6
推荐指数
2
解决办法
1万
查看次数

C++:容器替换大小的vector/deque

所以我的应用程序拥有包含1亿个元素的容器.

我正在寻找一个容器,它的行为 - 时间方面 - 比std :: deque(更不用说std :: vector)更容易在整个容器中频繁插入和删除...包括在中间附近.对第n个元素的访问时间不需要像vector那样快,但是应该比std :: list(每个元素都有巨大的内存开销)定义要好于完全遍历.

元素应按索引排序(如vector,deque,list),因此std :: set或std :: unordered_set也不能正常工作.

在我坐下来自己编写这样一个容器之前:有没有人见过这样的野兽?我很确定STL没有这样的东西,期待BOOST我找不到我能用的东西,但我可能错了.

任何提示?

c++ containers boost stl large-data

5
推荐指数
1
解决办法
1583
查看次数