比方說,我有我的數據在4×3 vector<vector<int> >
爲:發現在2D向量的元素,然後刪除它
1 2 3
4 5 6
7 8 9
10 11 12
,我想刪除包含元素8
每一行,並最終像:
1 2 3
4 5 6
10 11 12
我一直在努力做的事情:
for (vector<vector<int> >::iterator it = v.begin(); it != v.end(); it++) {
if (find(it->begin(), it->end(), 8)) {
// I will remove the row in here
}
}
這給了我:
error: no matching function for call to 'find(std::vector<int>::iterator, std::vector<int>::iterator, int)'
我沒有與stl
很多經驗,所以我想知道:
- 有什麼錯我的
find
電話? - 在迭代它的時候從矢量中移除一個元素是否安全?
OFC任何優雅的解決我的問題也很歡迎..
'find'尋找一個元素。你的外部向量的元素是向量,而不是整數。你可能會最好使用擦除刪除習慣用法與'std :: remove_if'結合使用。 – chris
@chris是的,但'it :: operator->'返回內部向量,所以這應該是好的。此外,錯誤消息表明這只是缺少包含的問題。 –
@LuchianGrigore,錯過了那一個。你是對的。 – chris