限制在函数中作为参数传递的矢量位置
Limit the vector position to be passed in a function as parameter
我正在考虑将索引限制在函数上传递。
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
我想把它传递给我的函数calc1()和calc2()。
int calc1(my_int *d)
{
for (my_int::iterator it = d.begin(); i != d.end(); ++i)
printf("%d ", *it);
}
输出应该是:
0 1 2 3
在 int calc2(my_int *d)
{
for (my_int::iterator it = d.begin(); i != d.end(); ++i)
printf("%d ", *it);
}
输出应该是:
4 5 6 7
我的语法不是特别准确,因为我还没有测试过。但是我想先知道如何实现这种情况
只写一个这样的函数就足够了
int calc( std::vector<int>::iterator first, std::vector<int>::iterator last );
并命名为
calc( my_int.begin(), std::next( my_int.begin(), 4 ) );
calc( std::next( my_int.begin(), 4 ), my_int.end() );
或者你可以这样声明函数
int calc_n( std::vector<int>::iterator first, size_t n );
并将其命名为
calc_n( my_int.begin(), 4 );
calc_n( std::next( my_int.begin(), 4 ), 4 );
相关文章:
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用指向成员的指针将成员函数作为参数传递
- 如何将参数传递给正在使用模板的类
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 修改函数中的指针(将另一个指针作为参数传递)
- 如何将部分流作为参数传递
- 我正在开发服务器,ip作为参数传递不起作用
- 将成员函数指针作为参数传递给模板方法
- 如何在C++中将迭代器作为函数参数传递
- 将附加参数传递给使用 beast::bind_front_handler 调用的函数
- 如何将一个类的函数作为另一个类的另一个函数的参数传递
- 将参数传递为"const"的奇怪效果
- 如何在 c++ 中将函数作为参数传递?
- 在 C++ 中将非指定类型作为参数传递的最佳方法?
- 使用引用与指针将数组作为参数传递
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 为什么我总是在传递位置参数时得到默认值
- 从服务器中不同位置的命令行参数传递的打开文件名
- 限制在函数中作为参数传递的矢量位置
- 参数传递中常量位置的 c++ 标准是什么?