本文整理汇总了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;
}
示例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;
}
示例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});
}
}
}
}
示例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"))
//.........这里部分代码省略.........
示例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 );
}
示例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;
}