public member function
<locale>

std::collate::hash

long hash (const char_type* low, const char_type* high) const;
獲取雜湊值
返回字串的雜湊值,該字串對應於字元序列[low,high)

雜湊值使得兩個具有相同雜湊值的字串使用collate::compare進行比較時相等。

請注意,雜湊值不保證是唯一的,因此兩個不同的字串可能會產生相同的雜湊值。但是,此類衝突的機率應接近1/numeric_limits<unsigned long>::max()(在大多數系統上為十億分之一)。

內部,此函式僅呼叫虛保護成員do_hash,該函式預設執行上述操作。

引數

low, high
指向序列開始和結束字元的指標。使用的範圍是[low,high),它包含low和high指向的字元之間的所有字元,包括low指向的字元,但不包括high指向的字元。
請注意,空字元(如果有)也會被考慮在內,用於雜湊值,以及它們之後的所有字元,直到high。
成員型別char_type是分面(facet)的字元型別(定義為collate的模板引數charT的別名)。

返回值

一個標識整個字元序列內容的整數值。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// collate::hash example
#include <iostream>       // std::cin, std::cout
#include <string>         // std::string, std::getline
#include <locale>         // std::locale, std::collate, std::use_facet

int main ()
{
  std::string myberry = "strawberry";
  std::string yourberry;

  std::locale loc;                 // the "C" locale

  const std::collate<char>& coll = std::use_facet<std::collate<char> >(loc);

  long myhash = coll.hash(myberry.data(),myberry.data()+myberry.length());

  std::cout << "Please, enter your favorite berry: ";
  std::getline (std::cin,yourberry);

  long yourhash = coll.hash(yourberry.data(),yourberry.data()+yourberry.length());

  if (myhash == yourhash)
    std::cout << "Mine too!\n";
  else
    std::cout << "I prefer strawberries...\n";

  return 0;
}

可能的輸出

Please enter your favorite berry: strawberry
Mine too!


資料競爭

訪問分面對象以及範圍[low,high)中的字元。

異常安全

強異常保證: 如果丟擲異常,則沒有副作用。

另見