It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
                            
                        
                    
                
                                9年前关闭。
            
                    
我正在尝试使数字电子学问题适应基于C ++ STL的程序。

本来我有4个输入C1,C2,C3,C4。这意味着我总共有16种组合:

0000
0001
.
.
.
1111


我有一个多图定义

typedef std::pair<int, int> au_pair; //vertices
typedef std::pair<int, int> acq_pair; //ch qlty
typedef std::multimap<int, acq_pair> au_map;
typedef au_map::iterator It_au;


没有模拟的数量取决于au_map的大小。
例如:如果au_map.size() = 5我将具有C1,C2,C3,C4,C5。因此2 ^ 5 = 32例。

例如:
如果au_map.size()=4,我需要模拟16种情况的算法。

for(It_au it = a_map.begin(); it != a_map.end(); it++)
{
  acq_pair it1 = it->second;
  //case 0:
  //C3 = 0, C2 = 0, C1 = 0, C0 = 0
  //Update it1.second with corresponding C values

  //simulate algorithm

  //case 1:
  //C3 = 0, C2 = 0, C1 = 0, C0 = 1
  //simulate

  .........
  //case 15:
  //C3 = 1, C2 = 1, C1 = 1, C0 = 1
  //simulate

}

最佳答案

不是最好的主意。现在,通过手动设置C1-C4并在for循环中编写一些模拟例程,您正在做很多无用的工作。

自动化它。

使用一些抽象的State-Simulator映射器(其中Simulator实际上代表某些具体的功能对象)。

typedef char State;

struct basic_simulator {
   // You could also pass some other parameters to your
   // simulator
   virtual void operator()(...) = 0
};

struct concrete_simulator : public basic_simulator {
   virtual void operator()(...) {
      // Do something concrete
      // Trolololo
   }
};

typedef basic_simulator Simulator;


在这种情况下,您的实际包装器将看起来像std::map<State, Simulator*> map;



接下来需要做的就是从定义为char的状态中获取C1-C4值。使用按位运算符。

您的所有状态都可以定义为0-15转换为二进制(2 -> 0010)的数字。因此,要获得C1-C4值,您只需要进行适当的移动即可:

// C1 C2 C3 C4
// -----------
// 1  1  1  1
State state = 15;

for (int i = 3; i >= 0; i--) {
   // State of some of the inputs, defined by one bit
   InputState C_xx = ((state >> i) & 1);
}


现在,只需将所有这些0-15状态映射到适当的模拟函子即可:

std::map<State, Simulator*> mapper;
// Generally speaking, you should use some sort of
// smart pointer here
mapper[0] = new concrete_simulator(...);
mapper[1] = ...


假设只有3个或4个具体的模拟器可以映射到某些州,这真的很酷。

在这种情况下,调用实际模拟将意味着:

  // Fire appropriate simulation object
  (*(mapper[actual_state]))(...);


进行所有可能的模拟意味着遍历每个地图元素。



更新:对于拥有多个4个输入的状态/单个输入状态可以具有两个以上的可能值,可以使用相同的技术。

只需编写适当的映射函数和状态生成器即可。

关于c++ - C++案例声明? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3110975/

10-13 08:20