Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 它是否存在一个c++;只包含第一个和最后一个值的容器?_C++_Algorithm_Performance_Stl_Containers - Fatal编程技术网

C++ 它是否存在一个c++;只包含第一个和最后一个值的容器?

C++ 它是否存在一个c++;只包含第一个和最后一个值的容器?,c++,algorithm,performance,stl,containers,C++,Algorithm,Performance,Stl,Containers,我需要一个只使用范围的第一个和最后一个值初始化的容器 我需要一个如下所示的容器: range<int> myRange(first, last, bound); int occur=myRange.count(r%5==0 && r%10!=0); range myRange(第一个、最后一个、绑定); int-occure=myRange.count(r%5==0&&r%10!=0); bound将定义容器内迭代期间对象之间的距离。例如,如果first=0,la

我需要一个只使用范围的第一个和最后一个值初始化的容器

我需要一个如下所示的容器:

range<int> myRange(first, last, bound);
int occur=myRange.count(r%5==0 && r%10!=0);
range myRange(第一个、最后一个、绑定);
int-occure=myRange.count(r%5==0&&r%10!=0);
bound
将定义容器内迭代期间对象之间的距离。例如,如果
first=0
last=10
bound=1
将对从
first
last
的每个整数执行迭代。如果
bound=2
,迭代将在{0,2,4,6,8,10}内执行。但大小将为2
myRange[0]=第一个
myRange[1]=最后一个

简而言之,一个容器允许对不存在的对象进行迭代。在第一个、第一个+边界、第一个+2*边界、第一个+n*边界上迭代,直到到达最后一个元素。我不想使用数组

count()
方法将返回给定条件下范围内有多少对象
返回true

注意:我是初学者,因此如果有解决方案,请详细说明最低要求:)
感谢您抽出时间;)

请参见以下实施:

cat range.h

\ifndef\u范围\u H_
#定义范围_
#包括
#包括
#定义表达式(变量)(变量%5==0&&var%10!=0)
等级范围{
int优先;
int last;
内界;
友元类范围迭代器;
公众:
范围(intf,intl,intb):第一(f),最后(l),界(b){}
~Range(){}
int count()常量{
int-cnt=0;
for(int i=第一;i第一;
}
~RangeIterator(){}
int getNext(){
int ret=int_MIN;
如果(mCurrVal last){
ret=mCurrVal;
mCurrVal+=mRange->bound;
}
返回ret;
}
};
#恩迪夫
cat main.cxx

#包括
#包括
#包括“range.h”
使用名称空间std;
int main(){
范围r(0,50,5);
范围迭代器ri&r;
int curr=int_MIN;
而((curr=ri.getNext())!=INT\u MIN){

CUT定义为,它不是一个容器。它是一个范围。我已经搜索范围了,我什么也没找到。更难搜索,比如:谢谢,我会在NickyCAny的网站上查看“无视C++和STL迭代规则”的说法。C++的约定是什么意思?我不知道现有的STL迭代器,所以我实现了。基本上是类型需求或概念,如增量运算符、解引用操作符、成员类型等,由原始STL发起,现在由国际标准指定,在C++开发人员中是众所周知的,并且由许多第三方图书馆一致。哦,你是指迭代器在STL图书馆中实现的方式。style之所以常见是因为STL。但是,我认为这不是标准。我们在我工作的组织中使用不同的样式(如我所述)。它不是“样式”它是C++国际标准中迭代器的定义,它是ISO/IEC 1488—2014年第24.2节所规定的正式要求。不要把它贬低为比它少的东西。
#ifndef _RANGE_H_
#define _RANGE_H_

#include <climits>
#include <cassert>
#define expr(var) (var%5 == 0 && var%10 != 0)

class Range {
    int first;
    int last;
    int bound;
    friend class RangeIterator;

    public: 
    Range(int f, int l, int b) : first(f), last(l), bound(b) {}
    ~Range() {}

    int count() const {
        int cnt = 0;
        for (int i = first; i <= last; i += bound) {
            if (expr(i))
                cnt++;
        }
        return cnt;
    }

    int getFirst() const { return first; }
    int getLast() const { return last; }
};

class RangeIterator {
    const Range* mRange;
    int mCurrVal;

    public: 
    RangeIterator(const Range* range) : mRange(range) 
    { 
        assert(range); 
        mCurrVal = mRange->first;
    }

    ~RangeIterator() {}

    int getNext() {
        int ret = INT_MIN;
        if (mCurrVal <= mRange->last) {
            ret = mCurrVal;
            mCurrVal += mRange->bound;
        }
        return ret;
    }
};

#endif
#include <iostream>
#include <climits>
#include "range.h"
using namespace std;

int main() {
    Range r(0, 50, 5);

    RangeIterator ri(&r);
    int curr = INT_MIN;
    while ((curr = ri.getNext()) != INT_MIN) {
        cout << curr << '\t';
    }
    cout << endl;

    cout << "count is : " << r.count() << endl;
    return 0;
}