gpt4 book ai didi

c++ - 在 unordered_set 上没有插入的匹配成员

转载 作者:行者123 更新时间:2023-11-30 03:33:51 24 4
gpt4 key购买 nike

我试图插入一个 Movie反对 unordered_set<Movie>容器,但我得到一个错误,说没有这样一个匹配的成员函数。我做了这样的事情

void ActorGraph::addActor(string actor_name, string movie_title, int movie_year){
unordered_map<string, unordered_set<ActorNode>>::iterator con_itr = connections.find(actor_name);
ActorNode actor(actor_name);
Movie movie(movie_title, movie_year);

if(con_itr != connections.end()){
auto adjSet = con_itr->second;
unordered_set<ActorNode>::iterator act_itr = adjSet.find(actor);
if(act_itr != adjSet.end()){
//in the set
auto mov_itr = act_itr->movies.find(movie);
if( mov_itr == act_itr->movies.end()){
act_itr->movies.insert(movie) //no matching function, while act_itr->movies is of type unordered_set<Movie>
}
}
}else{
unordered_set<ActorNode> adjSet;
actor.movies.insert(movie);
adjSet.insert(actor);
connections[actor_name] = adjSet;
cout << "The size is: " << actor.movies.size() << endl;
}
}

我的 ActorNode 是一个看起来像这样的结构

struct ActorNode{
//the name of the actor/actress
string name;

/** the movie that this actor/actree participated in
*/
unordered_set<Movie> movies;

ActorNode(string n) : name(n){}

bool operator ==(const ActorNode &other) const;
};


namespace std
{
template <>
struct hash<ActorNode>
{
size_t operator()(const ActorNode& actor) const
{
return hash<std::string>{}(actor.name);
}
};
}

电影结构

#ifndef Movie_h
#define Movie_h

#include <iostream>
using namespace std;

struct Movie{
string name;
int year;
Movie(string n, int y): name(n), year(y){}
bool operator ==(const Movie &m) const;
};



namespace std
{
template <>
struct hash<Movie>
{
size_t operator()(const Movie& movie) const
{
return hash<std::string>{}(movie.name + to_string(movie.year));
}
};
}


#endif /* Movie_ph*/

enter image description here

我已经实现并覆盖了运算符,并使我的 Movie 和 ActorNode 结构兼容用于 unordered_set 的键中

这是存储库: Repo


Minimal Reproduction Example

#include <iostream>
#include <string>
#include <unordered_set>

struct Movie{
std::string name;
int year;

Movie(std::string n, int y): name(std::move(n)), year(y)
{
}

bool operator ==(const Movie &m) const
{
return year == m.year && name == m.name;
};
};


namespace std
{
template <>
struct hash<Movie>
{
size_t operator()(const Movie& movie) const
{
return hash<std::string>{}(movie.name + to_string(movie.year));
}
};
}
////////////////////

struct ActorNode
{
std::string name;
std::unordered_set<Movie> movies;

ActorNode(std::string n) : name(std::move(n))
{
}

bool operator ==(const ActorNode &other) const
{
return name == other.name;
}
};


namespace std
{
template <>
struct hash<ActorNode>
{
size_t operator()(const ActorNode& actor) const
{
return hash<std::string>{}(actor.name);
}
};
}
////////////////////


int main()
{
std::unordered_set<ActorNode> actors;
actors.emplace("Gene Wilder");

auto itr = actors.find(ActorNode("Gene Wilder"));
if (itr != actors.end())
{
// error: no matching function for call to
// 'std::unordered_set<Movie>::insert(Movie) const'
itr->movies.insert(Movie("Stir Crazy", 1980));
}
}

最佳答案

问题是你不能修改 set 的键,因为它会导致 set 重建,但是 set 不会被重建,因为你只是修改变量,没有别的。因此,这是明确禁止的。

unordered_set 中迭代器的定义|

iterator    Constant ForwardIterator
const_iterator Constant forward iterator

关于c++ - 在 unordered_set 上没有插入的匹配成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42504093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com