+ -
当前位置:首页 → 问答吧 → 模版内部定义

模版内部定义

时间:2011-12-21

来源:互联网

C/C++ code

//VecType::iterator newFirst = vec.begin()这个的定义。

#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

template <typename T>
typename T::value_type mostFre(T first, T last)
{
    std::size_t amount = 0;
    T start = first;
    while (start != last)
    {
        amount++;
        start++;
    }

    typedef std::vector<typename T::value_type> VecType;


    VecType vec(amount);

    VecType::iterator newFirst = vec.begin();//gcc编译器提示error: expected ';' before 'newFirst'
//但是同一段代码,在vs2010中通过。
    VecType::iterator newLast = vec.end();

    std::uninitialized_copy(first, last, newFirst);

    std::sort(newFirst, newLast);

    std::size_t maxOccu = 0, occu = 0;

    VecType::iterator preIter = newFirst;
    VecType::iterator maxOccuElemIt = newFirst;

    while (newFirst != newlast)
    {
        if (*newFirst != *preIter)
        {
            if (occu > maxOccu)
            {
                maxOccu = occu;
                maxOccuElemIt = preIter;
            }

                return *maxOccuElemIt;
            }
            occu =0;
        }
        ++occu;
        preIter = newFirst;
        ++newFirst;
    }

    if (occu > maxOccu)
    {
        maxOccu = occu;
        maxOccuElemIt = preIter;
    }

    return *maxOccuElemIt;
}

int main()
{
    

    return 0;
}


作者: jy01807853   发布时间: 2011-12-21

typename VecType::iterator newFirst = vec.begin();//
加上这个就万无一失了

作者: mingliang1212   发布时间: 2011-12-21

while (newFirst != newlast)
  {
  if (*newFirst != *preIter)
  {
  if (occu > maxOccu)
  {
  maxOccu = occu;
  maxOccuElemIt = preIter;
  } ==>>>

  return *maxOccuElemIt;
  } ===>>> 这个括号有问题吧
  occu =0;
  }
  ++occu;
  preIter = newFirst;
  ++newFirst;
  }

作者: blh   发布时间: 2011-12-21

引用 2 楼 blh 的回复:

while (newFirst != newlast)
{
if (*newFirst != *preIter)
{
if (occu > maxOccu)
{
maxOccu = occu;
maxOccuElemIt = pr……

嗯,这个是我复制过来的出的。

作者: jy01807853   发布时间: 2011-12-21


请问 mingliang1212。
这样做是明确告诉编译器,VecType::iterator 是类型VecType的iterator成员是类型名字?

C/C++ code


template <typename T>
typename T::value_type mostFre(T first, T last)
{
    std::size_t amount = 0;
    T start = first;
    while (start != last)
    {
        amount++;
        start++;
    }

    typedef std::vector<typename T::value_type> VecType;


    VecType vec(amount);

        typename VecType::iterator newFirst = vec.begin();
/*改成这样就通过了,编译器给出的完整error: dependent-name 'std::VecType::iterator' is parsed as a non-type, but instantiation yields a type*/


    typename VecType::iterator newLast = vec.end();

    std::uninitialized_copy(first, last, newFirst);

    std::sort(newFirst, newLast);

    std::size_t maxOccu = 0, occu = 0;

    typename VecType::iterator preIter = newFirst;
    typename VecType::iterator maxOccuElemIt = newFirst;

    while (newFirst != newLast)
    {
        if (*newFirst != *preIter)
        {
            if (occu > maxOccu)
            {
                maxOccu = occu;
                maxOccuElemIt = preIter;
            }
                        occu =0;
                }

        ++occu;
        preIter = newFirst;
        ++newFirst;
    }

    if (occu > maxOccu)
    {
        maxOccu = occu;
        maxOccuElemIt = preIter;
    }

    return *maxOccuElemIt;
}


作者: jy01807853   发布时间: 2011-12-21

引用 4 楼 jy01807853 的回复:
请问 mingliang1212。
这样做是明确告诉编译器,VecType::iterator 是类型VecType的iterator成员是类型名字?


C/C++ code


template <typename T>
typename T::value_type mostFre(T first, T last)
{
std::size_t amount = 0;……


是的,,
否则可能会被当作非类型成员

作者: mingliang1212   发布时间: 2011-12-21