合并两张地图的快捷方式?

时间:2016-12-01 19:38:15

标签: c++

我有2张地图:

map<int,BigObject> a,b;

如果ab不共享任何密钥,是否有办法将b合并到a,而BigObject不会复制BigObject个对象?

复制function deliver_mail() { // if the submit button is clicked, send the email if ( isset( $_POST['cf-submitted'] ) ) { $errors = array(); if (empty($_POST["cf-name"])){ $nameError = "Name is required"; } else { $name = sanitize_text_field( $_POST["cf-name"] ); } // sanitize form values $name = sanitize_text_field( $_POST["cf-name"] ); $email = sanitize_email( $_POST["cf-email"] ); $phone = "Phone: " . sanitize_text_field( $_POST["cf-phone"] ); $company = "Company: " . sanitize_text_field( $_POST["cf-company"] ); $deliverablesArr = $_POST["formDeliverables"]; $deliverables = implode( $deliverablesArr); $body = esc_textarea( $_POST["cf-body"] ); $date_from = "None"; if (empty($_POST["date_from"])){ $errors[] = 'Please choose a start date.'; } else { $date_from = $_POST['date_from']; } $date_end = "None"; if (empty($_POST["date_end"])){ $errors[] = 'Please choose an end date.'; } else { $date_end = $_POST['date_end']; } $date_span = $date_from . " - " . $date_end; $price = $_POST['price']; $price_two = $_POST['price-two']; $budget = $price . " - " . $price_two; $subject = "Someone needs " . $deliverables . " from a-three.cc!"; $copy = 'None'; if(isset($_POST['copy']) && is_array($_POST['copy']) && count($_POST['copy']) > 0){ $copy = implode(', ', $_POST['copy']); } else { $errors[] = 'Please choose if copy is needed.'; } $design = 'None'; if(isset($_POST['design']) && is_array($_POST['design']) && count($_POST['design']) > 0){ $design = implode(', ', $_POST['design']); } else { $errors[] = 'Please choose if design is needed.'; } $illustration = 'None'; if(isset($_POST['illustration']) && is_array($_POST['illustration']) && count($_POST['illustration']) > 0){ $illustration = implode(', ', $_POST['illustration']); } else { $errors[] = 'Error: Please choose if Illustration is needed'; } $photography = 'None'; if(isset($_POST['photography']) && is_array($_POST['photography']) && count($_POST['photography']) > 0){ $photography = implode(', ', $_POST['photography']); } else { $errors[] = 'Please choose if Photography is needed.'; } // get the blog administrator's email address $to = get_option( 'admin_email' ); $message = '<html><body>'; $message .= '<table rules="all" style="border: 1px solid #666; width: 600px;" cellpadding="10">'; $message .= "<tr style='background: #eee;'><td style='font-size: 16px' colspan='2'><strong><center>Contact from A-Three</center></strong> </td></tr>"; $message .= "<tr><td><strong>Name:</strong> </td><td>" . $name . "</td></tr>"; $message .= "<tr><td><strong>Email:</strong> </td><td>" . $email . "</td></tr>"; $message .= "<tr><td><strong>Company:</strong> </td><td>" . $company . "</td></tr>"; $message .= "<tr><td><strong>Phone:</strong> </td><td>" . $phone . "</td></tr>"; $message .= "<tr><td><strong>Deliverables:</strong> </td><td>" . $deliverables . "</td></tr>"; $message .= "<tr style='background: #eee;'><td style='font-size: 16px' colspan='2'><strong><center>Services Needed</center></strong> </td></tr>"; $message .= "<tr><td><strong>Copy:</strong> </td><td>" . $copy . "</td></tr>"; $message .= "<tr><td><strong>Design:</strong> </td><td>" . $design . "</td></tr>"; $message .= "<tr><td><strong>Illustration:</strong> </td><td>" . $illustration . "</td></tr>"; $message .= "<tr><td><strong>Photography:</strong> </td><td>" . $photography . "</td></tr>"; $message .= "<tr style='background: #eee;'><td style='font-size: 16px' colspan='2'><strong><center>Timeline</center></strong> </td></tr>"; $message .= "<tr><td><strong>Timeline:</strong> </td><td>" . $date_span . "</td></tr>"; $message .= "<tr style='background: #eee;'><td style='font-size: 16px' colspan='2'><strong><center>Budget</center></strong> </td></tr>"; $message .= "<tr><td><strong>Budget:</strong> </td><td>" . $budget . "</td></tr>"; $message .= "<tr style='background: #eee;'><td style='font-size: 16px' colspan='2'><strong><center>Project Description</center></strong> </td></tr>"; $message .= "<tr><td><strong>Project Description:</strong> </td><td>" . $body . "</td></tr>"; $headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 对象是瓶颈。

2 个答案:

答案 0 :(得分:5)

你应该做的第一件事是让BigObject移动成本低廉,并且可以使它成为无副本。一种方法是将他们庞大而昂贵的状态转移到std::unique_ptr< InternalState >。如果您仍然需要副本(我发现您经常不这样做),您可能需要一个value_ptr< InternalState >,要么从boost这样的库中获取值ptr,要么写一下自己的。

如果失败了,你可以等待C ++ 17。在C ++ 17中,(某些?)基于关联节点的容器能够快速移动它们之间的节点集。这包括std::map

如果失败,您的地图可以从密钥到unique_ptr<BigObject>。这为地图的其他用途添加了少量间接,但它们现在可以便宜地移动。

答案 1 :(得分:0)

正如一些评论指出的那样,如果复制BigObject是昂贵的,那么你应该首先处理指针或引用......

但是如果碰巧只合并了很少的地图,那么你可以编写一个包装器,在读取(和写入......)时按顺序检查几个合并的地图。