Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. IT & Infrastructure
  4. std::vector<> equivalent of CArray::RemoveAt(nIndex)

std::vector<> equivalent of CArray::RemoveAt(nIndex)

Scheduled Pinned Locked Moved IT & Infrastructure
c++graphicsquestion
5 Posts 4 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paul Selormey
    wrote on last edited by
    #1

    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!

    L M J P 4 Replies Last reply
    0
    • P Paul Selormey

      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!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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)

      1 Reply Last reply
      0
      • P Paul Selormey

        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!

        M Offline
        M Offline
        Mukkie
        wrote on last edited by
        #3

        You can always erase an element, if You know it's index: container.erase(container.begin()+5); remember to check size of the vector.

        1 Reply Last reply
        0
        • P Paul Selormey

          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!

          J Offline
          J Offline
          Jonathan Gilligan
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • P Paul Selormey

            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!

            P Offline
            P Offline
            Paul Selormey
            wrote on last edited by
            #5

            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!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups