当前位置: 首页>>代码示例>>C++>>正文


C++ std::distance方法代码示例

本文整理汇总了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))};
}
开发者ID:qinf,项目名称:myLeetcode,代码行数:7,代码来源:SearchforaRange.cpp

示例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;
            }
开发者ID:Globidev,项目名称:Fun-with-templates,代码行数:13,代码来源:append.hpp

示例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;
    }
}
开发者ID:samuelsmal,项目名称:Informatik-2--Algorithmen-und-Datenstrukturen-Solutions,代码行数:16,代码来源:Heap.cpp

示例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());
}
开发者ID:dieram3,项目名称:quotient_filter,代码行数:8,代码来源:quotient_filter_test.cpp

示例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;
}
开发者ID:StevenDunn,项目名称:CodeEval,代码行数:25,代码来源:d.cpp

示例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{};
}
开发者ID:sansajn,项目名称:aexplor,代码行数:8,代码来源:directory_model.cpp

示例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()));
}
开发者ID:WUhailing,项目名称:Metawear-CppAPI,代码行数:9,代码来源:accelerometer.cpp

示例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;
}
开发者ID:Petr-Kovalev,项目名称:nupic.core.net,代码行数:9,代码来源:histogram_sort.hpp

示例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);

}
开发者ID:duxiuxia,项目名称:Link,代码行数:10,代码来源:multimodlocgen.cpp

示例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;
}
开发者ID:qinf,项目名称:myLeetcode,代码行数:10,代码来源:SearchforaRange.cpp

示例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;
    }
  }
}
开发者ID:AbhinavJain13,项目名称:turicreate,代码行数:55,代码来源:nonblocking.hpp

示例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;
}
开发者ID:KitsuneFox89,项目名称:rlvm,代码行数:11,代码来源:SelectLongOperation.cpp

示例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;
}
开发者ID:duxiuxia,项目名称:Link,代码行数:11,代码来源:multimodlocgen.cpp

示例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());
		}
	}
}
开发者ID:csyezheng,项目名称:Cpp-Primer,代码行数:13,代码来源:9.43.cpp

示例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;
}
开发者ID:kalemas,项目名称:sword,代码行数:36,代码来源:versificationmgr.cpp


注:本文中的std::distance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。