當前位置: 首頁>>代碼示例>>C++>>正文


C++ std::binary_search方法代碼示例

本文整理匯總了C++中std::binary_search方法的典型用法代碼示例。如果您正苦於以下問題:C++ std::binary_search方法的具體用法?C++ std::binary_search怎麽用?C++ std::binary_search使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在std的用法示例。


在下文中一共展示了std::binary_search方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C++代碼示例。

示例1: insert

int Search_DB::insert(const key_t& key, value_t value)
{
	off_t parent = search_index(key);
	off_t offset = search_leaf(parent, key);
	leaf_node_t leaf;
	map(&leaf, offset);

	// check if we have the same key
	if (binary_search(begin(leaf), end(leaf), key))
		return 1;

	if (leaf.n == meta.order) {
		// split when full

		// new sibling leaf
		leaf_node_t new_leaf;
		node_create(offset, &leaf, &new_leaf);

		// find even split point
		size_t point = leaf.n / 2;
		bool place_right = keycmp(key, leaf.children[point].key) > 0;
		if (place_right)
			++point;

		// split
		std::copy(leaf.children + point, leaf.children + leaf.n,
			new_leaf.children);
		new_leaf.n = leaf.n - point;
		leaf.n = point;

		// which part do we put the key
		if (place_right)
			insert_record_no_split(&new_leaf, key, value);
		else
			insert_record_no_split(&leaf, key, value);

		// save leafs
		unmap(&leaf, offset);
		unmap(&new_leaf, leaf.next);

		// insert new index key
		insert_key_to_index(parent, new_leaf.children[0].key,
			offset, leaf.next);
	} else {
		insert_record_no_split(&leaf, key, value);
		unmap(&leaf, offset);
	}

	return 0;
}
開發者ID:seem-sky,項目名稱:LocalSearch,代碼行數:50,代碼來源:search_db.cpp

示例2: binarySearch

//-----------------------------------------------------------------------------
vector<answer> binarySearch(
    const vector<int>& numbers,
    const vector<int>& queries) {
  vector<int> numeros = numbers;
  sort(numeros.begin(), numeros.end());
  vector<answer> answer_array;
  for (int q = 0; q < queries.size(); ++q) {
    if (binary_search(numeros.begin(), numeros.end(), queries[q])) {
      answer_array.push_back(YES);
    } else {
      answer_array.push_back(NO);
    }
  }
  return answer_array;
}
開發者ID:orcchg,項目名稱:StudyProjects,代碼行數:16,代碼來源:perfhash_stress.cpp

示例3: fourSum

vector<vector<int>> fourSum(vector<int>& num, int target)
{
	vector< vector<int> > result;
	if(num.size() < 4) return result;
	sort(num.begin(), num.end());

	auto last = num.end();
	for (auto a = num.begin(); a < prev(last, 3); a = upper_bound(a, prev(last, 3), *a))
	{
		for(auto b = next(a);b < prev(last, 2); b = upper_bound(b, prev(last, 2), *b))
		{
			for(auto c = next(b); c < prev(last); c = upper_bound(c, prev(last), *c))
			{
				const int d = target - *a - *b - *c;
				if(binary_search(next(c), last, d))
					result.push_back(vector<int>{*a, *b, *c, d});
			}
		}
	}
}
開發者ID:cwlseu,項目名稱:Algorithm,代碼行數:20,代碼來源:4sum.cpp

示例4: portAllowed

// We copied the KURL version here on Dec 4, 2009 while doing a WebKit
// merge.
//
// FIXME Somehow share this with KURL? Like we'd theoretically merge with
// decodeURLEscapeSequences below?
bool portAllowed(const KURL& url)
{
    unsigned short port = url.port();

    // Since most URLs don't have a port, return early for the "no port" case.
    if (!port)
        return true;

    // This blocked port list matches the port blocking that Mozilla implements.
    // See http://www.mozilla.org/projects/netlib/PortBanning.html for more information.
    static const unsigned short blockedPortList[] = {
        1,    // tcpmux
        7,    // echo
        9,    // discard
        11,   // systat
        13,   // daytime
        15,   // netstat
        17,   // qotd
        19,   // chargen
        20,   // FTP-data
        21,   // FTP-control
        22,   // SSH
        23,   // telnet
        25,   // SMTP
        37,   // time
        42,   // name
        43,   // nicname
        53,   // domain
        77,   // priv-rjs
        79,   // finger
        87,   // ttylink
        95,   // supdup
        101,  // hostriame
        102,  // iso-tsap
        103,  // gppitnp
        104,  // acr-nema
        109,  // POP2
        110,  // POP3
        111,  // sunrpc
        113,  // auth
        115,  // SFTP
        117,  // uucp-path
        119,  // nntp
        123,  // NTP
        135,  // loc-srv / epmap
        139,  // netbios
        143,  // IMAP2
        179,  // BGP
        389,  // LDAP
        465,  // SMTP+SSL
        512,  // print / exec
        513,  // login
        514,  // shell
        515,  // printer
        526,  // tempo
        530,  // courier
        531,  // Chat
        532,  // netnews
        540,  // UUCP
        556,  // remotefs
        563,  // NNTP+SSL
        587,  // ESMTP
        601,  // syslog-conn
        636,  // LDAP+SSL
        993,  // IMAP+SSL
        995,  // POP3+SSL
        2049, // NFS
        3659, // apple-sasl / PasswordServer [Apple addition]
        4045, // lockd
        6000, // X11
        6665, // Alternate IRC [Apple addition]
        6666, // Alternate IRC [Apple addition]
        6667, // Standard IRC [Apple addition]
        6668, // Alternate IRC [Apple addition]
        6669, // Alternate IRC [Apple addition]
        invalidPortNumber, // Used to block all invalid port numbers
    };
    const unsigned short* const blockedPortListEnd = blockedPortList + sizeof(blockedPortList) / sizeof(blockedPortList[0]);

#ifndef NDEBUG
    // The port list must be sorted for binary_search to work.
    static bool checkedPortList = false;
    if (!checkedPortList) {
        for (const unsigned short* p = blockedPortList; p != blockedPortListEnd - 1; ++p)
            ASSERT(*p < *(p + 1));
        checkedPortList = true;
    }
#endif

    // If the port is not in the blocked port list, allow it.
    if (!binary_search(blockedPortList, blockedPortListEnd, port))
        return true;

    // Allow ports 21 and 22 for FTP URLs, as Mozilla does.
    if ((port == 21 || port == 22) && url.protocolIs("ftp"))
//.........這裏部分代碼省略.........
開發者ID:Akheon23,項目名稱:chromecast-mirrored-source.vendor,代碼行數:101,代碼來源:KURLGoogle.cpp

示例5: acceptState

bool Lexical::acceptState( int curState ){
    int acceptedStates[ACCEPTED_LEN] = {1, 2, 4, 7, 8, 9, 10, 11, 12, 13, 15, 17, 18, 19, 20, 21, 22};

    return binary_search( acceptedStates, acceptedStates + ACCEPTED_LEN, curState );
}
開發者ID:Blurred-9L,項目名稱:BlurCC,代碼行數:5,代碼來源:Lexical.cpp

示例6: remove

int Search_DB::remove(const key_t& key)
{
	internal_node_t parent;
	leaf_node_t leaf;

	// find parent node
	off_t parent_off = search_index(key);
	map(&parent, parent_off);

	// find current node
	index_t *where = find(parent, key);
	off_t offset = where->child;
	map(&leaf, offset);

	// verify
	if (!binary_search(begin(leaf), end(leaf), key))
		return -1;

	size_t min_n = meta.leaf_node_num == 1 ? 0 : meta.order / 2;
	assert(leaf.n >= min_n && leaf.n <= meta.order);

	// delete the key
	record_t *to_delete = find(leaf, key);
	std::copy(to_delete + 1, end(leaf), to_delete);
	leaf.n--;

	// merge or borrow
	if (leaf.n < min_n) {
		// first borrow from left
		bool borrowed = false;
		if (leaf.prev != 0)
			borrowed = borrow_key(false, leaf);

		// then borrow from right
		if (!borrowed && leaf.next != 0)
			borrowed = borrow_key(true, leaf);

		// finally we merge
		if (!borrowed) {
			assert(leaf.next != 0 || leaf.prev != 0);

			key_t index_key;

			if (where == end(parent) - 1) {
				// if leaf is last element then merge | prev | leaf |
				assert(leaf.prev != 0);
				leaf_node_t prev;
				map(&prev, leaf.prev);
				index_key = begin(prev)->key;

				merge_leafs(&prev, &leaf);
				node_remove(&prev, &leaf);
				unmap(&prev, leaf.prev);
			} else {
				// else merge | leaf | next |
				assert(leaf.next != 0);
				leaf_node_t next;
				map(&next, leaf.next);
				index_key = begin(leaf)->key;

				merge_leafs(&leaf, &next);
				node_remove(&leaf, &next);
				unmap(&leaf, offset);
			}

			// remove parent's key
			remove_from_index(parent_off, parent, index_key);
		} else {
			unmap(&leaf, offset);
		}
	} else {
		unmap(&leaf, offset);
	}

	return 0;
}
開發者ID:seem-sky,項目名稱:LocalSearch,代碼行數:76,代碼來源:search_db.cpp


注:本文中的std::binary_search方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。