我有以下结构

struct LetterFreq{
  char letter;
  double freq;
};

现在,我正在尝试创建这些的std:list ...
std::list<LetterFreq> freqList;
LetterFreq f = {'A',.08167};
freqList.push_back(f);
f =  {'B',.01492};
freqList.push_back(f);

但是,当我尝试编译时,我得到...
error: expected expression

如果我改成...
std::list<LetterFreq> freqList;
LetterFreq f = {'A',.08167};
freqList.push_back(f);
LetterFreq f2 =  {'B',.01492};
freqList.push_back(f2);

似乎可行,但我并不是真的想要那样。我宁愿重用变量f

最佳答案

没有C++ 11,您可以执行以下操作:

struct LetterFreq{
  char letter;
  double freq;

  LetterFreq(char letter, double freq)
  : letter(letter), freq(freq)
  {}
};

f =  LetterFreq('B',.01492);

关于c++ - 如何替换struct C++,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20384205/

10-09 05:05