本文整理汇总了C++中std::distance方法的典型用法代码示例。如果您正苦于以下问题:C++ std::distance方法的具体用法?C++ std::distance怎么用?C++ std::distance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类std
的用法示例。
在下文中一共展示了std::distance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: searchRange
vector<int> searchRange(int A[], int n, int target) {
auto lower = lower_bound(A, A+n, target);
auto upper = upper_bound(A, A+n, target);
if (lower == A + n || *lower != target)
return vector<int> {-1, -1};
return vector<int> {distance(A, lower), distance(A, prev(upper))};
}
示例2: operator
auto operator()(const C & c1, const C & c2) const {
using std::distance;
using std::copy;
auto c1_size = distance(c1.begin(), c1.end());
auto c2_size = distance(c2.begin(), c2.end());
C r(c1_size + c2_size);
auto end = copy(c1.begin(), c1.end(), r.begin());
copy(c2.begin(), c2.end(), end);
return r;
}
示例3: distance
void MyHeap<Key, Element>::insertItem(const Key& k, const Element& e) {
heap.push_back(item(k, e));
iterator parent = heap.end() - distance(heap.begin()++, heap.end()) / 2;
if (parent > heap.end()) {
parent = heap.begin();
}
iterator child = heap.end();
while (child->first < parent->first) {
swap(*child, *parent);
child = parent;
parent = child - distance(heap.begin()++, child) / 2;
}
}
示例4: expect_empty
// Checks whether a filter is empty and checks invariants.
static void expect_empty(const filter_t &c) {
EXPECT_TRUE(c.empty());
EXPECT_EQ(0, c.size());
EXPECT_EQ(0, distance(c.begin(), c.end()));
EXPECT_FLOAT_EQ(0.0f, c.load_factor());
EXPECT_LE(c.load_factor(), c.max_load_factor());
}
示例5: main
int main (int argc, char* argv[])
{
ifstream input_file(argv[1]);
string line;
if (input_file)
{
while (getline(input_file, line))
{
vector<string> entries = tokenize(line);
vector<int> dots;
for (int i = 0; i < entries.size(); ++i)
{
string entry = entries[i];
// special case for failure on CodeEval's end
if (entry == "XYYYY.Y")
entry = "XYYYYYY";
dots.push_back(count(entry.begin(), entry.end(), '.'));
}
cout << dots[distance(dots.begin(), min_element(dots.begin(), dots.end()))] << endl;
}
input_file.close();
}
return 0;
}
示例6: find
QModelIndex directory_model::find(std::string const & file_name) const
{
list<file_info>::const_iterator it = find_if(_files.begin(), _files.end(), find_by_name{file_name});
if (it != _files.end())
return index(distance(_files.begin(), it));
else
return QModelIndex{};
}
示例7: fabs
static inline uint8_t closest_index(const vector<float>& values, float key) {
vector<float> differences;
differences.resize(values.size());
transform(values.begin(), values.end(), differences.begin(), [key](const float val) {
return fabs(val - key);
});
return distance(differences.begin(), min_element(differences.begin(), differences.end()));
}
示例8: reserve_count_for_single_pass_helper
size_t
reserve_count_for_single_pass_helper(InputIterator first, InputIterator last,
std::random_access_iterator_tag)
{
using std::distance;
typename std::iterator_traits<InputIterator>::difference_type n =
distance(first, last);
return (size_t)n;
}
示例9: _super
MultiModLocGen::MultiModLocGen(MultiModLocGen const &rhs) :
_super(rhs) {
using std::distance;
_rangeMap = rhs._rangeMap;
range_map_type::difference_type dist =
distance(_rangeMap.begin(), rhs._rangeIt);
_rangeIt = std::next(_rangeMap.begin(), dist);
}
示例10: upper_bound
ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last, T value) {
while (first != last) {
auto mid = next(first, distance(first, last) / 2);
if (value >= *mid)
first = ++mid;
else
last = mid;
}
return first;
}
示例11: wait_all
void
wait_all(ForwardIterator first, ForwardIterator last)
{
typedef typename std::iterator_traits<ForwardIterator>::difference_type
difference_type;
using std::distance;
difference_type num_outstanding_requests = distance(first, last);
std::vector<bool> completed(num_outstanding_requests);
while (num_outstanding_requests > 0) {
bool all_trivial_requests = true;
difference_type idx = 0;
for (ForwardIterator current = first; current != last; ++current, ++idx) {
if (!completed[idx]) {
if (optional<status> stat = current->test()) {
// This outstanding request has been completed.
completed[idx] = true;
--num_outstanding_requests;
all_trivial_requests = false;
} else {
// Check if this request (and all others before it) are "trivial"
// requests, e.g., they can be represented with a single
// MPI_Request.
all_trivial_requests =
all_trivial_requests
&& !current->m_handler
&& current->m_requests[1] == MPI_REQUEST_NULL;
}
}
}
// If we have yet to fulfill any requests and all of the requests
// are trivial (i.e., require only a single MPI_Request to be
// fulfilled), call MPI_Waitall directly.
if (all_trivial_requests
&& num_outstanding_requests == (difference_type)completed.size()) {
std::vector<MPI_Request> requests;
requests.reserve(num_outstanding_requests);
for (ForwardIterator current = first; current != last; ++current)
requests.push_back(current->m_requests[0]);
// Let MPI wait until all of these operations completes.
BOOST_MPI_CHECK_RESULT(MPI_Waitall,
(num_outstanding_requests, &requests[0],
MPI_STATUSES_IGNORE));
// Signal completion
num_outstanding_requests = 0;
}
}
}
示例12: selectOption
bool SelectLongOperation::selectOption(const std::string& str) {
std::vector<Option>::iterator it =
find_if(options_.begin(), options_.end(), bind(&Option::str, _1) == str);
if (it != options_.end() && it->shown) {
selected(distance(options_.begin(), it));
return true;
}
return false;
}
示例13: distance
MultiModLocGen& MultiModLocGen::operator=(MultiModLocGen const &rhs) {
if (this != &rhs) {
using std::distance;
_super::operator=(static_cast<_super const&>(rhs));
_rangeMap = rhs._rangeMap;
range_map_type::difference_type dist =
distance(_rangeMap.begin(), rhs._rangeIt);
_rangeIt = std::next(_rangeMap.begin(), dist);
}
return *this;
}
示例14: replace
void replace(string &s, const string &oldVal, const string &newVal)
{
for (auto iter = s.begin(); distance(iter, s.end()) >=
static_cast<ptrdiff_t>(oldVal.size()); ++iter)
{
if (*iter == oldVal[0] && string(iter, std::next(iter, oldVal.size()))
== oldVal)
{
iter = s.erase(iter, std::next(iter, oldVal.size()));
iter = s.insert(iter, newVal.cbegin(), newVal.cend());
}
}
}
示例15: BookOffsetLess
char VersificationMgr::System::getVerseFromOffset(long offset, int *book, int *chapter, int *verse) const {
if (offset < 1) { // just handle the module heading corner case up front (and error case)
(*book) = -1;
(*chapter) = 0;
(*verse) = 0;
return offset; // < 0 = error
}
// binary search for book
vector<Book>::iterator b = lower_bound(p->books.begin(), p->books.end(), offset, BookOffsetLess());
if (b == p->books.end()) b--;
(*book) = distance(p->books.begin(), b)+1;
if (offset < (*(b->p->offsetPrecomputed.begin()))-((((!(*book)) || (*book)==BMAX[0]+1))?2:1)) { // -1 for chapter headings
(*book)--;
if (b != p->books.begin()) {
b--;
}
}
vector<long>::iterator c = lower_bound(b->p->offsetPrecomputed.begin(), b->p->offsetPrecomputed.end(), offset);
// if we're a book heading, we are lessthan chapter precomputes, but greater book. This catches corner case.
if (c == b->p->offsetPrecomputed.end()) {
c--;
}
if ((offset < *c) && (c == b->p->offsetPrecomputed.begin())) {
(*chapter) = (offset - *c)+1; // should be 0 or -1 (for testament heading)
(*verse) = 0;
}
else {
if (offset < *c) c--;
(*chapter) = distance(b->p->offsetPrecomputed.begin(), c)+1;
(*verse) = (offset - *c);
}
return ((*chapter > 0) && (*verse > b->getVerseMax(*chapter))) ? KEYERR_OUTOFBOUNDS : 0;
}