如果您有很多读取和较少的写入,您可以使用向量作为映射。这很常见,因为lower_bound
它比内存更有效map
并且使用更少的空间:
bool your_less_function( const your_type &a, const your_type &b )
{
// based on keys
return ( a < b );
}
...
std::vector<your_type> ordered-vector;
添加值时:
...
// First 100 values
ordered-vector.push_back(value)
...
// Finally. The vector must be sorted before read.
std::sort( ordered-vector.begin(), ordered-vector.end(), your_less_function );
询问数据时:
std::vector<your_type>::iterator iter = std::lower_bound( ordered-vector.begin(), ordered-vector.end(), value, your_less_function );
if ( ( iter == ordered-vector.end() ) || your_less_function( *iter, value ) )
// you did not find the value
else
// iter contains the value
不幸的是,它是订购的,但真的很快。