【发布时间】:2017-04-28 14:03:04
【问题描述】:
我有一个方法,我想根据传递的变量是否是向量的元素来执行不同的方法,例如:
void method(int a){
if (/*a is an element of a vector*/) //do one thing
else //do another thing
}
然后在 main:
vector<int> a;
a.pushback(1);
int b = 1;
method(a[0]); // does one thing
method(b); // does the other thing
最简单的方法是什么?
【问题讨论】:
-
为什么你想这样做?这听起来像一个非常个坏主意。
-
向量保存值。无论您传入
a[0]还是1,向量中都有一个1。您真正想要解决的高层次问题是什么? -
这听起来像xy problem。向量元素与独立变量的操作有什么区别?您可以声明第二个函数来获取向量和索引
void method(vector& a, size_t index){。 -
@HermannDöppes 呃,对不起。现在我不知道我在问自己什么。我的大脑短路了
-
@Yokubasu 它发生了。快点好起来。 (=