当前位置: 首页>>代码示例>>C++>>正文


C++ StringX::push_back方法代码示例

本文整理汇总了C++中StringX::push_back方法的典型用法代码示例。如果您正苦于以下问题:C++ StringX::push_back方法的具体用法?C++ StringX::push_back怎么用?C++ StringX::push_back使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在StringX的用法示例。


在下文中一共展示了StringX::push_back方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: MakePassword

StringX CPasswordCharPool::MakePassword() const
{
  // We don't care if the policy is inconsistent e.g. 
  // number of lower case chars > 1 + make pronounceable
  // The individual routines (normal, hex, pronounceable) will
  // ignore what they don't need.
  // Saves an awful amount of bother with setting values to zero and
  // back as the user changes their minds!

  ASSERT(m_pwlen > 0);
  ASSERT(m_uselowercase || m_useuppercase || m_usedigits ||
         m_usesymbols   || m_usehexdigits || m_pronounceable);


  // pronounceable and hex passwords are handled separately:
  if (m_pronounceable)
    return MakePronounceable();
  if (m_usehexdigits)
    return MakeHex();

  vector<typeFreq_s> typeFreqs;

  if (m_uselowercase)
    typeFreqs.push_back(typeFreq_s(this, LOWERCASE, m_numlowercase));

  if (m_useuppercase)
    typeFreqs.push_back(typeFreq_s(this, UPPERCASE, m_numuppercase));

  if (m_usedigits)
    typeFreqs.push_back(typeFreq_s(this, DIGIT, m_numdigits));

  if (m_usesymbols)
    typeFreqs.push_back(typeFreq_s(this, SYMBOL, m_numsymbols));

  // Sort requested char type in decreasing order
  // of requested (at least) frequency:
  sort(typeFreqs.begin(), typeFreqs.end(),
       [](const typeFreq_s &a, const typeFreq_s &b)
       {
         return a.numchars > b.numchars;
       });

  StringX temp;
  // First meet the 'at least' constraints
  for (auto iter = typeFreqs.begin(); iter != typeFreqs.end(); iter++)
    for (uint j = 0; j < iter->numchars; j++) {
      if (!iter->vchars.empty()) {
        temp.push_back(iter->vchars.back());
        iter->vchars.pop_back();
        if (temp.length() == m_pwlen)
          goto do_shuffle; // break out of two loops, goto needed
      }
    }


  // Now fill in the rest
  while (temp.length() != m_pwlen) {
    uint i = PWSrand::GetInstance()->RangeRand(typeFreqs.size());
    if (!typeFreqs[i].vchars.empty()) {
      temp.push_back(typeFreqs[i].vchars.back());
      typeFreqs[i].vchars.pop_back();
      if (temp.length() == m_pwlen)
        goto do_shuffle; // break out of two loops, goto needed
    }
  }

 do_shuffle:
  // If 'at least' values were non-zero, we have some unwanted order,
  // se we mix things up a bit:
  RandomWrapper rnw;
  random_shuffle(temp.begin(), temp.end(), rnw);

  ASSERT(temp.length() == size_t(m_pwlen));
  return temp;
}
开发者ID:anagram,项目名称:pwsafe,代码行数:75,代码来源:PWCharPool.cpp


注:本文中的StringX::push_back方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。