2012-10-20 53 views
0

比方說,我有我的數據在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任何優雅的解決我的問題也很歡迎..

+0

'find'尋找一個元素。你的外部向量的元素是向量,而不是整數。你可能會最好使用擦除刪除習慣用法與'std :: remove_if'結合使用。 – chris

+0

@chris是的,但'it :: operator->'返回內部向量,所以這應該是好的。此外,錯誤消息表明這只是缺少包含的問題。 –

+0

@LuchianGrigore,錯過了那一個。你是對的。 – chris

回答

2

有什麼錯我發現電話?

你可能忘了#include <algorithm>

是它的安全,同時遍歷它從向量中刪除元素?

看看擦除刪除成語 - 刪除向量元素可能會很棘手。

+0

'#include '已經工作了,現在我又有了一個錯誤。 – none

+0

@gokcehan如果完全不同,請發佈一個新問題。 –

+0

它是完全不同的,但我想我會嘗試使用擦除刪除成語,而不是看起來更容易.. – none