C++ 在一组字符串对中搜索一个字符串

C++ searching for one string in a set of pairs of strings

我在图表中将边定义为一对城市,例如: make_pair(city1, city2)

我已将这些对存储在 set<pair<string,string>>

我现在想将 cityA 的所有实例更改为 cityBcityA 可能处于 pair.firstpair.second 位置。

我尝试使用以下循环进行搜索,但在赋值运算符 = 符号上出现错误。

这段代码展示了两种方式。

我做错了什么?

for (edgeSetIter = edgeSet.begin(); edgeSetIter != edgeSet.end(); edgeSetIter++)            
    {
    if ((*edgeSetIter).first == cityA) { edgeSetIter->first = cityB; }
    else if ((*edgeSetIter).second == cityA) { (*edgeSetIter).second = cityB; }
    }                                                                                   

您不能修改集合的元素,因为它们是关联容器的键。精确 quote from cplusplus.com:

In a set, the value of an element also identifies it (the value is itself the key, of type T), and each value must be unique. The value of the elements in a set cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.

set 的替代方法可能是使用非关联容器和:unique