如果我有一個矢量對象矢量,我該如何檢查A * myA是否在該矢量內?如何迭代矢量列表
Q
如何迭代矢量列表
1
A
回答
5
嘗試......
#include <algorithm>
bool in_vector(const std::vector<A*>& vec, const A* myA)
{
std::vector<A*>::const_iterator end = vec.end();
return std::find(vec.begin(), end, myA) != end;
}
+2
'end'將需要是'const_iterator'。 – 2010-06-17 00:35:56
+0
@Mike很好找:) – 2010-06-17 00:36:33
0
這可能會或可能無法正常工作:
vector<int> v;
. . .
for (int i=0; i<v.size(); i++) {
//---do a compare of v[i] with your object?
cout << v[i] << endl;
}
0
也許是這樣的:
std::vector<A*> v;
for (std::vector<A*>::iterator it = v.begin(); it != v.end(); ++it) {
// Do comparison with *it to your myA...
}
0
這是wowus'答案的擴張因爲我認爲它更接近你想要做的事情:
#include <algorithm>
// let's call it simply TVectorVectorA and TVectorA
typedef std::vector<std::vector<A*> > TVectorVectorA;
typedef TVectorVectorA::value_type TVectorA;
bool in_vector(const std::vector<std::vector<A*> >& vec, const A* myA)
{
TVectorVectorA::const_iterator vend = vec.end();
// iterate through all 'top-level' vectors
for (TVectorVectorA::const_iterator it = vec.begin(); vend != it; ++it)
{
// check if the current TVector element contains myA
if (std::find((*it).begin(), (*it).end(), myA) != end)
{
return true;
}
}
return false;
}
給出一個相關的代碼示例總是很好的。當您提出這些問題時,您正在使用的數據類型的聲明)。這樣,其他人更容易理解你想要做什麼。
相關問題
- 1. C++:列表迭代器與矢量迭代器
- 2. 矢量end迭代
- 3. 如何增加矢量迭代器?
- 4. 迭代多的std ::矢量
- 5. 矢量擦除迭代
- 6. 迭代的矢量蟒蛇
- 7. 二維矢量迭代器
- 8. 矢量迭代器不dereferencable
- 9. C++矢量迭代器不增量
- 10. 如何迭代列表?
- 11. 如何Python列表迭代?
- 12. 如何迭代列表?
- 13. 如何迭代矢量矢量並在下面的場景中獲取輸出?
- 14. 的std ::矢量斷言失敗(矢量迭代器不兼容)
- 15. 迭代通過一個std ::矢量
- 16. C++矢量迭代器轉換錯誤
- 17. 矢量迭代器不可取消?
- 18. STL矢量迭代給核心轉儲
- 19. 矢量迭代器不能解碼....?
- 20. C++中的矢量迭代器
- 21. 矢量迭代器和模板函數
- 22. 一個的typedef矢量迭代器
- 23. 無法在矢量上迭代
- 24. VC++矢量迭代器初始化
- 25. C++「矢量迭代器不可遞減」?
- 26. 迭代遍歷矢量時跳過行
- 27. 如何在Matlab中迭代列向量?
- 28. 如何獲取模板對象矢量上的迭代器?
- 29. 如何從模板中的對象矢量獲取迭代器?
- 30. 如何將Foo *迭代器添加到Foo矢量中?
也許一些代碼會有所幫助。 – GManNickG 2010-06-16 23:46:13
一些澄清(可能與代碼)會很好。你的意思是你有一個指向某些struct A的向量向量? A類型是否相關,或者您的問題是否適用於void *? – Karmastan 2010-06-16 23:59:37