그들은 O (1)의 동일한 상각 된 복잡성을 갖지만, 값을 찾을 수없는 경우에도 연산자는 새로운 요소를 만듭니다. 값이 발견되면 성능 차이는 작아야합니다. 내 부스트는 약간 오래된 버전인데, 1.41 버전이지만 괜찮 으면 좋겠다. 코드는 다음과 같습니다.
// find
//
// strong exception safety, no side effects
template <class H, class P, class A, class G, class K>
BOOST_DEDUCED_TYPENAME hash_table<H, P, A, G, K>::iterator_base
hash_table<H, P, A, G, K>::find(key_type const& k) const
{
if(!this->size_) return this->end();
bucket_ptr bucket = this->get_bucket(this->bucket_index(k));
node_ptr it = find_iterator(bucket, k);
if (BOOST_UNORDERED_BORLAND_BOOL(it))
return iterator_base(bucket, it);
else
return this->end();
}
// if hash function throws, basic exception safety
// strong otherwise
template <class H, class P, class A, class K>
BOOST_DEDUCED_TYPENAME hash_unique_table<H, P, A, K>::value_type&
hash_unique_table<H, P, A, K>::operator[](key_type const& k)
{
typedef BOOST_DEDUCED_TYPENAME value_type::second_type mapped_type;
std::size_t hash_value = this->hash_function()(k);
bucket_ptr bucket = this->bucket_ptr_from_hash(hash_value);
if(!this->buckets_) {
node_constructor a(*this);
a.construct_pair(k, (mapped_type*) 0);
return *this->emplace_empty_impl_with_node(a, 1);
}
node_ptr pos = this->find_iterator(bucket, k);
if (BOOST_UNORDERED_BORLAND_BOOL(pos)) {
return node::get_value(pos);
}
else {
// Side effects only in this block.
// Create the node before rehashing in case it throws an
// exception (need strong safety in such a case).
node_constructor a(*this);
a.construct_pair(k, (mapped_type*) 0);
// reserve has basic exception safety if the hash function
// throws, strong otherwise.
if(this->reserve_for_insert(this->size_ + 1))
bucket = this->bucket_ptr_from_hash(hash_value);
// Nothing after this point can throw.
return node::get_value(add_node(a, bucket));
}
}
두 코드 샘플은 다른 작업을 수행합니다. – GManNickG
어떻게 설명 할 수 있습니까? 최종 결과는 defaultrow에 의해 매핑 된 정수를 defRow에 할당하는 것입니다. 문자열이 값에 해시되지 않으면 해당 값이 같지 않다는 것을 알지만, 그렇지 않으면 값이 같지 않습니까? – Megatron
지도에 'defaultrow'가 없으면 첫 번째 행은 하나만 추가됩니다. 두 번째 항목은 이미 존재하지 않으면 ** 항목을 만듭니다 **. 따라서 기본 초기화 된 데이터가 반환됩니다. –