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. General Programming
  3. C / C++ / MFC
  4. vector, adding things

vector, adding things

Scheduled Pinned Locked Moved C / C++ / MFC
graphicstutorialquestion
6 Posts 3 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.
  • C Offline
    C Offline
    Calin Negru
    wrote on last edited by
    #1

    I figured push_back is for adding things at the front but if you want things added at the tail you should probably use insert. I`m having trouble calling insert, I don`t know how to fill in the parameters. Could someone lend a helping hand?

    Quote:

    void AddTask(vector * UnitTasks, int type ) { Task * T = new Task(); T->Type = type; int nSize = UnitTasks->empty() ? -1 : static_cast(UnitTasks->size()); UnitTasks->insert(T,nSize);//? }

    CPalliniC L 2 Replies Last reply
    0
    • C Calin Negru

      I figured push_back is for adding things at the front but if you want things added at the tail you should probably use insert. I`m having trouble calling insert, I don`t know how to fill in the parameters. Could someone lend a helping hand?

      Quote:

      void AddTask(vector * UnitTasks, int type ) { Task * T = new Task(); T->Type = type; int nSize = UnitTasks->empty() ? -1 : static_cast(UnitTasks->size()); UnitTasks->insert(T,nSize);//? }

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Actually, as its name suggestes, push_back adds an item at the back of the vector. Try the following sample code in order to see insert in action:

      #include #include using namespace std;

      int main()
      {
      vector v;

      v.push_back(1);
      v.push_back(2);
      v.push_back(3);

      cout << "v-front " << v.front() << "\n";
      cout << "v-back " << v.back() << "\n";

      v.insert(v.begin(), 4); // insert '4' before the first item: now '4' is the new front of 'v'

      cout << "v-front " << v.front() << "\n";

      v.insert(v.end() - 1, 5); // insert '5' just before the last item (back) of 'v', now '5' is the second-last item of 'v'

      v.insert(v.end(), 6); // insert '6' just before the 'pass-the-end' of 'v', that is '6' is the new back of 'v'.

      for (auto x : v) // show, in order, all 'v' items
      cout << x << " ";
      cout << "\n";
      }

      "In testa che avete, Signor di Ceprano?" -- Rigoletto

      In testa che avete, signor di Ceprano?

      C 1 Reply Last reply
      0
      • CPalliniC CPallini

        Actually, as its name suggestes, push_back adds an item at the back of the vector. Try the following sample code in order to see insert in action:

        #include #include using namespace std;

        int main()
        {
        vector v;

        v.push_back(1);
        v.push_back(2);
        v.push_back(3);

        cout << "v-front " << v.front() << "\n";
        cout << "v-back " << v.back() << "\n";

        v.insert(v.begin(), 4); // insert '4' before the first item: now '4' is the new front of 'v'

        cout << "v-front " << v.front() << "\n";

        v.insert(v.end() - 1, 5); // insert '5' just before the last item (back) of 'v', now '5' is the second-last item of 'v'

        v.insert(v.end(), 6); // insert '6' just before the 'pass-the-end' of 'v', that is '6' is the new back of 'v'.

        for (auto x : v) // show, in order, all 'v' items
        cout << x << " ";
        cout << "\n";
        }

        "In testa che avete, Signor di Ceprano?" -- Rigoletto

        C Offline
        C Offline
        Calin Negru
        wrote on last edited by
        #3

        Thanks CPallini the reason for which the ide wouldn`t accept my insert function was because I switched the order of parameters in the function, the intellisense function helper description is impossible to understand.

        CPalliniC 1 Reply Last reply
        0
        • C Calin Negru

          Thanks CPallini the reason for which the ide wouldn`t accept my insert function was because I switched the order of parameters in the function, the intellisense function helper description is impossible to understand.

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          You are welcome.

          "In testa che avete, Signor di Ceprano?" -- Rigoletto

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • C Calin Negru

            I figured push_back is for adding things at the front but if you want things added at the tail you should probably use insert. I`m having trouble calling insert, I don`t know how to fill in the parameters. Could someone lend a helping hand?

            Quote:

            void AddTask(vector * UnitTasks, int type ) { Task * T = new Task(); T->Type = type; int nSize = UnitTasks->empty() ? -1 : static_cast(UnitTasks->size()); UnitTasks->insert(T,nSize);//? }

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

            Hi, Just wanted to point out that you should choose the right STL container. The vector is designed for sequential insertion/access. If you want to frequently insert at both ends then you should consider using a std::deque Best Wishes, -David Delaune

            C 1 Reply Last reply
            0
            • L Lost User

              Hi, Just wanted to point out that you should choose the right STL container. The vector is designed for sequential insertion/access. If you want to frequently insert at both ends then you should consider using a std::deque Best Wishes, -David Delaune

              C Offline
              C Offline
              Calin Negru
              wrote on last edited by
              #6

              Thanks for your tip, at this point I`m still not sure what I want to use.

              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