C++ 限制要作为参数在函数中传递的向量位置

C++ 限制要作为参数在函数中传递的向量位置,c++,vector,C++,Vector,我想限制要传递给函数的索引 main.cpp: typedef vector<int *> my_int; my_int a; for (int i = 0; i < 8; ++i) a.push_back(i); calc1(a); //(Note:This is wrong) start the data from 0 until 3 only calc2(a.begin() + 5); //(Note:This is wrong) start data fr

我想限制要传递给函数的索引

main.cpp:

typedef vector<int *> my_int;
my_int a;
for (int i = 0; i < 8; ++i)
    a.push_back(i);  

calc1(a); //(Note:This is wrong) start the data from 0 until 3 only
calc2(a.begin() + 5); //(Note:This is wrong) start data from 4 to 7 only
输出应为:

0 1 2 3
4 5 6 7

int calc2(my_int *d)
{
    for (my_int::iterator it = d.begin(); i != d.end(); ++i)
        printf("%d ", *it);
}
输出应为:

0 1 2 3
4 5 6 7

我的语法不是特别准确,因为我还没有测试过它。但我只想先知道如何实现这种情况。

只编写一个这样的函数就足够了

int calc( std::vector<int>::iterator first, std::vector<int>::iterator last );
或者,您可以通过以下方式声明函数

int calc_n( std::vector<int>::iterator first, size_t n );

calc1&a;计算C2(&a.开始()+5)在语法上都是错误的。相反,使用类似于
int-calc2(const-std::vector&start,const-std::vector&end)
@Gibs对人们大喊大叫并不能改变这样一个事实:没有无端语法错误的问题是一个更清楚的问题。@Gibs别对我大喊大叫了,好吧!我一直在读这篇文章,但这并不能让问题变得更好。好吧,对不起,我只是不知道如何正确地构造这个问题
calc_n( my_int.begin(), 4 );
calc_n( std::next( my_int.begin(), 4 ), 4 );