C ++如何使用map重载+ =运算符?

时间:2017-10-06 17:21:58

标签: c++ operator-overloading

我的 public static final String BASE_URL = "https://maps.googleapis.com/maps/api/distancematrix/json"; private static final HttpRequest<GoogleResponsePojo> HTTP_REQUEST = HttpRequestBuilder.createGet(BASE_URL, GoogleResponsePojo.class) .addDefaultRequestParameter("origins", "Seattle") .addDefaultRequestParameter("destinations", "San+Francisco") .addDefaultRequestParameter("key", "***") .build(); @GET @Produces(MediaType.APPLICATION_JSON) public static void updateTestExecutionDetails() throws IOException { ResponseHandler<GoogleResponsePojo> responseHandler = HTTP_REQUEST.execute(); GoogleResponsePojo name = responseHandler.orElseThrow(); // throws ResponseException when status code != 200 System.out.println(name.getDestination_addresses().get(0)); System.out.println(name.getRows().get(0).getElements().get(0).getDuration().getText()); System.out.println(name.getOrigin_addresses().get(0)); } 类包含地图中的多项式。如何为Poly运算符重载它,以便将乘数与相同的指数相加?

示例:

+=包含在4x²+2x+7

(2, 4), (1, 2), (0, 7)

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

Poly& Poly::operator+=(Poly const& b) {
    for (Values::const_iterator iter = b.m_values.begin(), end = b.m_values.end(); iter != end; ++iter) {
        Values::iterator found = m_values.find(iter->first);
        if (found != m_values.end()) {
            found->second += iter->second;
        }
    }

    return *this;
}

Poly& Poly::operator-=(Poly const& b) {
    for (Values::const_iterator iter = b.m_values.begin(), end = b.m_values.end(); iter != end; ++iter) {
        Values::iterator found = m_values.find(iter->first);
        if (found != m_values.end()) {
            found->second -= iter->second;
        }
    }

    return *this;
}
相关问题