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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Function Taking Generic Vector?

Function Taking Generic Vector?

Scheduled Pinned Locked Moved C / C++ / MFC
c++comgraphicsquestioncareer
10 Posts 4 Posters 0 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.
  • G Offline
    G Offline
    gvanto
    wrote on last edited by
    #1

    I was wondering if there's a way to take a vector of any type into the argument of a function somehow? So instead of: showVectorSizeAndContents ( vector v ) it can be of any type, not just int? Thanks alot gvanto [CODE] /* * 4_VectorDemo2_InsertDelete.cpp * * Created on: 2/02/2009 * Author: pacific */ //Vector basics: #include #include using namespace std; void showVectorSizeAndContents(vector v) { // here i'd like a vector of any type to be passed ...? unsigned int i = 0; cout << "Current contents:\n"; cout << "Size = " << v.size() << endl; // display contents of vector for(i=0; i<v.size();> cout << v[i] << " "; } cout << endl; cout << endl; return; } int main() { vector v; unsigned int i; for(i = 0; i < 10; i++) { v.push_back('A' + i); } //cant call the show method since it takes vector } [/CODE]

    Find Your Dream Job in Australia, Free! http://www.WebCV.com.au

    N S N 3 Replies Last reply
    0
    • G gvanto

      I was wondering if there's a way to take a vector of any type into the argument of a function somehow? So instead of: showVectorSizeAndContents ( vector v ) it can be of any type, not just int? Thanks alot gvanto [CODE] /* * 4_VectorDemo2_InsertDelete.cpp * * Created on: 2/02/2009 * Author: pacific */ //Vector basics: #include #include using namespace std; void showVectorSizeAndContents(vector v) { // here i'd like a vector of any type to be passed ...? unsigned int i = 0; cout << "Current contents:\n"; cout << "Size = " << v.size() << endl; // display contents of vector for(i=0; i<v.size();> cout << v[i] << " "; } cout << endl; cout << endl; return; } int main() { vector v; unsigned int i; for(i = 0; i < 10; i++) { v.push_back('A' + i); } //cant call the show method since it takes vector } [/CODE]

      Find Your Dream Job in Australia, Free! http://www.WebCV.com.au

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      gvanto wrote:

      I was wondering if there's a way to take a vector of any type into the argument of a function somehow?

      Pass the vector as reference or pointer. Check the prototype of showVectorSizeAndContents as follows. void showVectorSizeAndContents(vector**&** v) Read Stephen's answer.

      nave [OpenedFileFinder] [My Blog]

      modified on Monday, February 2, 2009 1:21 AM

      N 1 Reply Last reply
      0
      • N Naveen

        gvanto wrote:

        I was wondering if there's a way to take a vector of any type into the argument of a function somehow?

        Pass the vector as reference or pointer. Check the prototype of showVectorSizeAndContents as follows. void showVectorSizeAndContents(vector**&** v) Read Stephen's answer.

        nave [OpenedFileFinder] [My Blog]

        modified on Monday, February 2, 2009 1:21 AM

        N Offline
        N Offline
        Nishad S
        wrote on last edited by
        #3

        Naveen wrote:

        void showVectorSizeAndContents(vector& v)

        Wrong. vector is a template.

        - ns ami -

        N 1 Reply Last reply
        0
        • G gvanto

          I was wondering if there's a way to take a vector of any type into the argument of a function somehow? So instead of: showVectorSizeAndContents ( vector v ) it can be of any type, not just int? Thanks alot gvanto [CODE] /* * 4_VectorDemo2_InsertDelete.cpp * * Created on: 2/02/2009 * Author: pacific */ //Vector basics: #include #include using namespace std; void showVectorSizeAndContents(vector v) { // here i'd like a vector of any type to be passed ...? unsigned int i = 0; cout << "Current contents:\n"; cout << "Size = " << v.size() << endl; // display contents of vector for(i=0; i<v.size();> cout << v[i] << " "; } cout << endl; cout << endl; return; } int main() { vector v; unsigned int i; for(i = 0; i < 10; i++) { v.push_back('A' + i); } //cant call the show method since it takes vector } [/CODE]

          Find Your Dream Job in Australia, Free! http://www.WebCV.com.au

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          I assume you mean std::vector. If so then try something like this:

          template <typename T>
          inline void SomeFunction(std::vector<T> &vec)
          {
          // Do stuff here.
          }

          Steve

          G 2 Replies Last reply
          0
          • N Nishad S

            Naveen wrote:

            void showVectorSizeAndContents(vector& v)

            Wrong. vector is a template.

            - ns ami -

            N Offline
            N Offline
            Naveen
            wrote on last edited by
            #5

            ns ami wrote:

            Wrong. vector is a template.

            :doh: I forgot. Corrected now :) Thanks for pointing

            nave [OpenedFileFinder] [My Blog]

            1 Reply Last reply
            0
            • G gvanto

              I was wondering if there's a way to take a vector of any type into the argument of a function somehow? So instead of: showVectorSizeAndContents ( vector v ) it can be of any type, not just int? Thanks alot gvanto [CODE] /* * 4_VectorDemo2_InsertDelete.cpp * * Created on: 2/02/2009 * Author: pacific */ //Vector basics: #include #include using namespace std; void showVectorSizeAndContents(vector v) { // here i'd like a vector of any type to be passed ...? unsigned int i = 0; cout << "Current contents:\n"; cout << "Size = " << v.size() << endl; // display contents of vector for(i=0; i<v.size();> cout << v[i] << " "; } cout << endl; cout << endl; return; } int main() { vector v; unsigned int i; for(i = 0; i < 10; i++) { v.push_back('A' + i); } //cant call the show method since it takes vector } [/CODE]

              Find Your Dream Job in Australia, Free! http://www.WebCV.com.au

              N Offline
              N Offline
              Nishad S
              wrote on last edited by
              #6

              You may need to derive from vector for flexibility. And can write the code for "showVectorSizeAndContents" inside that.

              - ns ami -

              1 Reply Last reply
              0
              • S Stephen Hewitt

                I assume you mean std::vector. If so then try something like this:

                template <typename T>
                inline void SomeFunction(std::vector<T> &vec)
                {
                // Do stuff here.
                }

                Steve

                G Offline
                G Offline
                gvanto
                wrote on last edited by
                #7

                Awesome this works just as intended !!!!!!! Many thanks Stephen! :-D

                Find Your Dream Job in Australia, Free! http://www.WebCV.com.au

                1 Reply Last reply
                0
                • S Stephen Hewitt

                  I assume you mean std::vector. If so then try something like this:

                  template <typename T>
                  inline void SomeFunction(std::vector<T> &vec)
                  {
                  // Do stuff here.
                  }

                  Steve

                  G Offline
                  G Offline
                  gvanto
                  wrote on last edited by
                  #8

                  I am trying to do the same for a list, but cant get the iterator pointer to use the generic type T: template inline void showListSizeAndContents(list lst) { //generic list cout << "Size = " << lst.size() << endl; cout << "List Contents: "; list::iterator p = lst.begin(); //this gives error! while(p != lst.end()) { cout << *p; p++; } return; }

                  Find Your Dream Job in Australia, Free! http://www.WebCV.com.au

                  S 1 Reply Last reply
                  0
                  • G gvanto

                    I am trying to do the same for a list, but cant get the iterator pointer to use the generic type T: template inline void showListSizeAndContents(list lst) { //generic list cout << "Size = " << lst.size() << endl; cout << "List Contents: "; list::iterator p = lst.begin(); //this gives error! while(p != lst.end()) { cout << *p; p++; } return; }

                    Find Your Dream Job in Australia, Free! http://www.WebCV.com.au

                    S Offline
                    S Offline
                    Stephen Hewitt
                    wrote on last edited by
                    #9

                    Your post is malformed. For example, I can't see and angle brackets after the template keyword. I'll have a go anyway but please revise your posts and make sure they are readable. Try something like this:

                    template <typename T>
                    inline void showListSizeAndContents(const list<T> &lst)
                    {
                    cout << "Size = " << lst.size() << endl;
                     
                    cout << "List Contents: ";
                     
                    list<T>::const_iterator e = lst.end();
                    for (list<T>::const_iterator i=lst.begin(); i!=e; ++i)
                    {
                    cout << *i << " ";
                    }
                     
                    cout << endl;
                    }

                    Steve

                    G 1 Reply Last reply
                    0
                    • S Stephen Hewitt

                      Your post is malformed. For example, I can't see and angle brackets after the template keyword. I'll have a go anyway but please revise your posts and make sure they are readable. Try something like this:

                      template <typename T>
                      inline void showListSizeAndContents(const list<T> &lst)
                      {
                      cout << "Size = " << lst.size() << endl;
                       
                      cout << "List Contents: ";
                       
                      list<T>::const_iterator e = lst.end();
                      for (list<T>::const_iterator i=lst.begin(); i!=e; ++i)
                      {
                      cout << *i << " ";
                      }
                       
                      cout << endl;
                      }

                      Steve

                      G Offline
                      G Offline
                      gvanto
                      wrote on last edited by
                      #10

                      Thanks Steve that works perfectly!! gvanto

                      Find Your Dream Job in Australia, Free! http://www.WebCV.com.au

                      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