std::vector<> equivalent of CArray::RemoveAt(nIndex)
-
Hello, Any simple equivalent of MFC CArray's RemoveAt() method in std::vector? std::vector::erase() can delete individual items as well, but it is iterator based. Regards, Paul. Paul Selormey, Bsc (Elect Eng), MSc (Mobile Communication) is currently Windows open source developer in Japan, and open for programming contract anywhere!
-
Hello, Any simple equivalent of MFC CArray's RemoveAt() method in std::vector? std::vector::erase() can delete individual items as well, but it is iterator based. Regards, Paul. Paul Selormey, Bsc (Elect Eng), MSc (Mobile Communication) is currently Windows open source developer in Japan, and open for programming contract anywhere!
I use Hewlett Packard's stl, but it is very similar to the MFC one. You can use iterator to find element You need: vector::iterator start = m_cntVec.begin(); while(start != m_cntVec.end()) { if(*start == MY_OBJECT) { m_cntVec.erase(start); break; } start++; } If You want to do this without the whole above code, please take a closer look at the algo.h file and methods: OutputIterator remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value) and InputIterator find(InputIterator first, InputIterator last, const T& value)
-
Hello, Any simple equivalent of MFC CArray's RemoveAt() method in std::vector? std::vector::erase() can delete individual items as well, but it is iterator based. Regards, Paul. Paul Selormey, Bsc (Elect Eng), MSc (Mobile Communication) is currently Windows open source developer in Japan, and open for programming contract anywhere!
-
Hello, Any simple equivalent of MFC CArray's RemoveAt() method in std::vector? std::vector::erase() can delete individual items as well, but it is iterator based. Regards, Paul. Paul Selormey, Bsc (Elect Eng), MSc (Mobile Communication) is currently Windows open source developer in Japan, and open for programming contract anywhere!
You probably realise this, but Remove() and RemoveAt() in CArray as well as erase() in std::vector<> are expensive methods. If you're using them a lot, you probably should be considering a list instead. As to your question, you can always simulate RemoveAt() by
v.erase(v.begin() + index, v.begin() + index + count);
. You can wrap this in a function thus:#include <vector>
using namespace std;template<class T_> void RemoveAt(vector<T_> &v, size_t index, size_t count = 1)
{
size_t size = v.size();
if (index >= size) {
cout << "index too large (" << index << " >= " << size << ")" << endl;
return;
}
if (index + count > size) {
cout << "reducing count from " << count << " to " << size - index << endl;
count = size - index;
}
vector<T_>::iterator it = v.begin() + index;
vector<T_>::iterator it2 = it + count;
v.erase(it, it2);
}which you would use thus:
#include <vector>
#include <iostream>
#include "my_remove_at.h"using namespace std;
void main(void)
{
vector<int> foo;for (int i = 0; i < 10; ++i) { foo.push\_back(i); } RemoveAt(foo, 3, 12); vector<int>::const\_iterator it; for( it = foo.begin(); it != foo.end(); ++it) { cout << \*it << endl; }
}
He was allying himself to science, for what was science but the absence of prejudice backed by the presence of money? --- Henry James, The Golden Bowl
-
Hello, Any simple equivalent of MFC CArray's RemoveAt() method in std::vector? std::vector::erase() can delete individual items as well, but it is iterator based. Regards, Paul. Paul Selormey, Bsc (Elect Eng), MSc (Mobile Communication) is currently Windows open source developer in Japan, and open for programming contract anywhere!
Thanks all. I was porting some MFC codes to ATL and STL and had the problem. It was solve simply as v.erase(v.begin() + index) Again, thanks for the contribution and the support. Best regards, Paul. Paul Selormey, Bsc (Elect Eng), MSc (Mobile Communication) is currently Windows open source developer in Japan, and open for programming contract anywhere!