部分类模板专业化与地图 [英] Partial class template specialization with maps

查看:116
本文介绍了部分类模板专业化与地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新的C ++程序员,之前我学习了Java和ANSI C,并决定给它一个镜头。



嗯,我喜欢C ++,但是我不喜欢迭代器的工作原理:



在java中,您可以将整个容器设为私有,并为其迭代器实现一个getter函数,迭代器有一个方法 hasNext(),返回一个布尔值,具体取决于是否已到达容器的末尾。



唯一的方法我发现在C ++上做类似的事情是编写两个getter,$ code> iteratorBegin()和 iteratorEnd(),返回一个对应于第一个和最后一个位置的interator,递增由 iteratorBegin()返回的迭代器,并将其与 iteratorEnd() ,允许我遍历容器,直到最终位置达到



但是,我只想使用一个getter方法,我想:让我们自己迭代器类



到目前为止好的,我已经使用集合和列表成功完成了,但我似乎无法使用地图,这里的代码令我感到困扰:(该类定义在一个单独的.h,这被称为customIterator.cpp)

 模板< typename T,typename D> 
const D& custIterator< T,D> :: next()
{
const D& obj =(* it);
if(hasNext())
{
it ++;
}
return obj;
}

//上述工作正常

模板< typename T,typename D>
const D& custIterator< map< T,D>,D> :: next()//此行中的错误
{
D& obj =(* it).second;
if(hasNext())
{
it ++;
}
return obj;
}

当编译专门的方法时,它说:错误:'map'不是在这个范围内宣布
即使我在文件的顶部添加了 #include< map>

我正在使用 gcc版本4.4.5(Debian 4.4.5-8)与codeblocks



请,我需要一些帮助。 / p>

感谢您的关注!

解决方案

所有标准容器都在命名空间std,你应该有资格,因此

  std :: map< T,D> 

一般来说,我建议不要将Java成语转载到C ++,因为每种语言都有自己的成语混合它们会混淆其他程序员(就像我认为 #define BEGIN {不是最好的想法)。



你想要做的是被称为范围,被一些人认为是比C ++迭代器更好的解决方案,但直到它进入我认为它对每个开发者发明自己的范围。



进一步阅读 tl; dr幻灯片(有一个视频可以去这个我现在找不到的)。


I'm a new C++ programmer, I learned Java and ANSI C a time ago and decided to give it a shot.

Well, I love C++, but I didn't like how the iterators work:

In java, you could make a whole container private and implement a getter function to it's iterator, and the iterator has a method hasNext() that returns a boolean depending on if it has reached the end of the container.

The only way I found to do something similar on C++ is writing 2 getters, iteratorBegin() and iteratorEnd(), that returned an interator corresponding to the first and last positions, incrementing the iterator returned by iteratorBegin() and comparing it with iteratorEnd(), allowed me to iterate over the container until the final position had been reached

But, I want to use only ONE getter method, and I thought: "Let's make my own iterator class"

So far so good, I've done it successfully with sets and lists, but I can't seem to make it with maps, here's the code that's troubling me: (the class is defined in a separate .h, this is called customIterator.cpp)

template<typename T, typename D>
const D& custIterator<T,D>::next()
{
    const D& obj = (*it);
    if(hasNext())
    {
        it++;
    }
    return obj;
}

//the above works fine

template<typename T, typename D>
const D& custIterator<map<T,D>,D>::next() //error in this line
{
    D& obj = (*it).second; 
    if(hasNext())
    {
        it++;
    }
    return obj;
}

when compiling the specialized method, it says: error: ‘map’ was not declared in this scope even though I added #include <map> on top of the file

I'm using gcc version 4.4.5 (Debian 4.4.5-8) with codeblocks

Please, I need some assistance.

Thanks for your attention!

解决方案

All the standard containers are inside the namespace std, you should qualify it thus

std::map<T,D>

In general I would recommend not trying to carry over Java idioms to C++ since each language has its own idioms and mixing them will confuse other programmers (just as I think that #define BEGIN { isn't the best idea ever).

What you're trying to do is known as ranges and is considered by some to be a better solution than C++ iterators but until it makes its way into the language I think it's harmful for each developer to invent their own ranges.

Further reading or tl;dr slides (there's a video to go with this which I can't find at the moment).

这篇关于部分类模板专业化与地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆