简体   繁体   English

我应该为指针分配使用哪种返回类型?

[英]What return type should I use for a pointer assignment?

I have a struct like this: 我有一个这样的结构:

struct group
{
    int index; 
    string name; 
    group* child;

};

And I set up a vector to store some group structs. 我设置了一个向量来存储一些组结构。

Now I am trying to have a function to retrieve a group member from that vector by index, something like this: 现在,我尝试使用一个函数,通过索引从该向量中检索组成员,如下所示:

148    newGroup.child = getGroupByIndex(world, i);

And the definition of the function is : 函数的定义是:

group& getGroupByIndex(vector<group>* world, int i)
{
    for(vector<group>::iterator it = world->begin();
        it < world->end(); ++it)
    {
        if(it->index == i) return *it;
    }
267     return 0;
}

Unfortunately, it won't even compile. 不幸的是,它甚至无法编译。

And the error message is : 错误消息是:

tree.cpp: In function ‘int main()’: 
tree.cpp:148: error: cannot convert ‘group’ to ‘group*’ in assignment 
tree.cpp: In function ‘group& getGroupByIndex(std::vector<group, std::allocator<group> >*, int)’: 
tree.cpp:267: error: invalid initialization of non-const reference of type ‘group&’ from a temporary of type ‘int’

My two problems, 我的两个问题

  1. how to fix the compile error? 如何解决编译错误? what return type should I use? 我应该使用哪种返回类型?

  2. If I want to return a null pointer in line 267, what should I use? 如果我想在第267行中返回一个空指针,应该使用什么? I tried (void *)0 and 0, but neither works. 我尝试了(void *)0和0,但均无效。

I think it should be like this: 我认为应该是这样的:

group* getGroupByIndex(vector<group*> world, int i) // See position of two *
{
    for(vector<group*>::iterator it = world.begin();
        it < world.end(); ++it)
    {
        if(it->index == i)
          return *it;
    }
    return 0;
}

or 要么

group* getGroupByIndex(vector<group> *world, int i) // See position of two *
{
    for(vector<group>::iterator it = world->begin();
        it < world->end(); ++it)
    {
        if(it->index == i)
          return &(*it);
    }
    return 0;
}

If you want prefer references to pointers, you can also define a "not found" group object that will be returned by your function. 如果您希望使用对指针的引用,还可以定义一个“未找到”组对象,该对象将由函数返回。

I would do that like this : 我会这样:

struct group
{
    int index; 
    string name; 
    group* child;
    group(int i):index(i),child(null){}
    group(int i, const string& n, group& c):index(i), name(n), child(&c){}

    // assuming index defines the uniqueness of your object class
    bool operator == (const struct group& g)const {return (index == g.index);}

    // an unique special instance of group struct
    static struct group not_found;
};
group group::not_found(-1);

thus you can define your function the way you wanted : 因此,您可以按照自己的方式定义函数:

group& getGroupByIndex(vector<group>* world, int i)
{
    for(vector<group>::iterator it = world->begin();
        it < world->end(); ++it)
    {
        if(it->index == i) return *it;
    }
    return group::not_found; // a reference to a special singleton instance of struct group
}

and you will be able to make calls like this : 这样您就可以拨打电话了:

...
group& g = getGroupByIndex(world, index);
if(g == group::not_found)
{
   // handle the not found case here
   ...

Use 采用

boost::optional

First rule of modern C++ : Don't use ****ing pointers. 现代C ++的第一条规则:不要使用****指针。

boost::optional<group&> get(vector<group>& world, int i)
{
    for(auto & grp : world)
    {
        if(grp.index == i)
           return boost::optional<group&>(grp);
    }
    return boost::none;
}

Please note that this solution has O(n) complexity. 请注意,此解决方案具有O(n)复杂度。 If you want to search basing on index , I'd recommend using a structure that has references to group objects sorted by index , which would give you O(log n) lookup time. 如果要基于index搜索,建议使用一种结构,该结构具有对按index排序的group对象的引用,这将为您提供O(log n)查找时间。

In that case, I'd probably hold a vector of shared_ptr s and a map<int, weak_ptr> . 在那种情况下,我可能会持有一个shared_ptr的向量和map<int, weak_ptr> You could also take a look at boost::multi_index 您也可以看看boost::multi_index

Ah, and just a sidenote to your 2) point I've just noticed : nullptr . 啊,这只是您2)要点的一个旁注: nullptr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM