Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以用那种方式返回迭代器吗? 我希望我的C++代码能像我所能封装的那样,迭代器返回的方式是正确的?< /P> const map<string,bool>::iterator getFollowers() { return followers.begin(); } const map<string,bool>::iterator getFollowing() { return following.begin(); }_C++_Iterator - Fatal编程技术网

我可以用那种方式返回迭代器吗? 我希望我的C++代码能像我所能封装的那样,迭代器返回的方式是正确的?< /P> const map<string,bool>::iterator getFollowers() { return followers.begin(); } const map<string,bool>::iterator getFollowing() { return following.begin(); }

我可以用那种方式返回迭代器吗? 我希望我的C++代码能像我所能封装的那样,迭代器返回的方式是正确的?< /P> const map<string,bool>::iterator getFollowers() { return followers.begin(); } const map<string,bool>::iterator getFollowing() { return following.begin(); },c++,iterator,C++,Iterator,完整代码: #ifndef twitClient_twitUser_h #define twitClient_twitUser_h #include <map> #include <iostream> #include <string> using namespace std; class user { string username; map<string,bool> followers; map<string,

完整代码:

#ifndef twitClient_twitUser_h
#define twitClient_twitUser_h

#include <map>
#include <iostream>
#include <string>

using namespace std;
class user {
    string username;
    map<string,bool> followers;
    map<string,bool> following;
    string name;


public:

    user(string username):username(username) {
        followers [username] = false;
        following [username] = false;
    }

    bool removeFollower (string friendName);
    bool addFollower(string friendName);
    bool stopFollowing(string friendName);
    bool startFollowing(string friendName);

    const map<string,bool>::iterator getFollowers() {

        return followers.begin();

    }

    const map<string,bool>::iterator getFollowing() {

        return following.begin();

    }


};

您的方法并没有任何错误,只是您可能也想添加const访问方法,例如

map<string,bool>::const_iterator getFollowers() const;
另外,您还希望添加对末端迭代器的访问


关于封装,您封装了映射,但是您的客户机暴露于map::iterator。有一篇关于隐藏这些依赖项的非常有趣的文章。这绝非小事,但仍然值得考虑。

您的方法没有错,只是您可能也想添加常量访问方法,例如

map<string,bool>::const_iterator getFollowers() const;
另外,您还希望添加对末端迭代器的访问

关于封装,您封装了映射,但是您的客户机暴露于map::iterator。有一篇关于隐藏这些依赖项的非常有趣的文章。这绝非小事,但仍然值得考虑。

是和否

封装有多种含义

在更轻的形式中,它意味着类对封装的成员拥有完全的控制权,如果您返回一个const_迭代器,这里可能就是这种情况 在更重的形式中,它意味着类实现细节不会泄漏到外部单词中,这里的情况并非如此 因此,您不会让其他人控制您的内部,但您仍然会暴露实现细节,如果您更改了追随者或追随者在幕后的实现方式,则会破坏客户机

一种可能的解决方案是引入循环构造,如foreach:

template <typename Pred>
void user::foreachFollower(Pred& p) const {
    for (auto const& f: followers) { p(f); }
}
这更灵活,因为如果您将地图从更改为计数,则始终可以更改函数:

template <typename Pred>
void user::foreachFollower(Pred& p) const {
    for (std::pair<std::string, bool> const& f: followers) { p(f); }
}
这样你的客户就不会破产。它们也是检测客户机是否能够处理std::pair的精心设计的技巧,但它们的实现有点困难

另一方面,仅仅给出begin迭代器是不够的,它们还需要end迭代器。

是和否

封装有多种含义

在更轻的形式中,它意味着类对封装的成员拥有完全的控制权,如果您返回一个const_迭代器,这里可能就是这种情况 在更重的形式中,它意味着类实现细节不会泄漏到外部单词中,这里的情况并非如此 因此,您不会让其他人控制您的内部,但您仍然会暴露实现细节,如果您更改了追随者或追随者在幕后的实现方式,则会破坏客户机

一种可能的解决方案是引入循环构造,如foreach:

template <typename Pred>
void user::foreachFollower(Pred& p) const {
    for (auto const& f: followers) { p(f); }
}
这更灵活,因为如果您将地图从更改为计数,则始终可以更改函数:

template <typename Pred>
void user::foreachFollower(Pred& p) const {
    for (std::pair<std::string, bool> const& f: followers) { p(f); }
}
这样你的客户就不会破产。它们也是检测客户机是否能够处理std::pair的精心设计的技巧,但它们的实现有点困难


在另一个注释中,仅仅给出开始迭代器是不够的,它们也需要最后一个。

< P>我认为如果你进一步考虑这个问题,可能会有一些设计问题。似乎您将在这个设置中保留许多容器,并设置一个follower/following bool来声明一个条件是否为真。马上回到这个话题

如果容器在使用前/使用过程中由另一个方法操纵,则传回迭代器可能非常危险。因此,对于您的问题,我会考虑向要分析/操作的容器传递一个引用,如果在多核时代使用多线程,我们需要始终考虑使用互斥来确保容器线程安全或类型设计以及简单的安全队列实现


对于这种设置,如果你有一个封闭的用户组,或者如果你有一个多对多的情况,那么不妨考虑一个矩阵类型的设计,然后考虑一下类似的情况。这可以提供更清晰、更可扩展的设计 如果容器在使用前/使用过程中由另一个方法操纵,则传回迭代器可能非常危险。因此,对于您的问题,我会考虑向要分析/操作的容器传递一个引用,如果在多核时代使用多线程,我们需要始终考虑使用互斥来确保容器线程安全或类型设计以及简单的安全队列实现


对于这种设置,如果你有一个封闭的用户组,或者如果你有一个多对多的情况,那么不妨考虑一个矩阵类型的设计,然后考虑一下类似的情况。这可以提供更清晰、更可扩展的设计

为什么不创建诸如const map和getFollowers const之类的函数,这样我就可以访问数据,但不能进行更改
信息技术它更方便,因为迭代器并没有给我完全的控制权。此外,对于许多算法,还需要使用末端迭代器。因此,我建议您不要使用这样的incapculation为什么不使用const-map和getFollowers-const这样的函数,这样我就可以访问数据,但不能更改它。它更方便,因为迭代器并没有给我完全的控制权。此外,对于许多算法,还需要使用末端迭代器。所以我建议你不要用这种胶囊