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. C++ template function returning "vector" ?

C++ template function returning "vector" ?

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicshardwaretutorialquestion
6 Posts 3 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I am trying to get comfortable using "vector". I have a function "returning" hardware parameters I need to process further "upstream" - including "main". I can pass a vector by reference or have been told to define "local vector" and "return" if from the function. I am not sure how "return vector" really works and have not tried it yet. The function returning "vector " syntax is scary... Could I return pointer to locally defined vector ? ( Or is is even feasible ?) Is there a specific resource I could read ? Something similar to this example , but with "return * vector - string "?

    // C++ program to demonstrate how vectors
    // can be passed by reference.
    #include
    using namespace std;

    // The vect is passed by reference and changes
    // made here reflect in main()
    void func(vector &vect)
    {
    vect.push_back(30);
    }

    int main()
    {
    vector vect;
    vect.push_back(10);
    vect.push_back(20);

    func(vect); 
    
    for (int i=0; i
    
    Greg UtasG CPalliniC 2 Replies Last reply
    0
    • L Lost User

      I am trying to get comfortable using "vector". I have a function "returning" hardware parameters I need to process further "upstream" - including "main". I can pass a vector by reference or have been told to define "local vector" and "return" if from the function. I am not sure how "return vector" really works and have not tried it yet. The function returning "vector " syntax is scary... Could I return pointer to locally defined vector ? ( Or is is even feasible ?) Is there a specific resource I could read ? Something similar to this example , but with "return * vector - string "?

      // C++ program to demonstrate how vectors
      // can be passed by reference.
      #include
      using namespace std;

      // The vect is passed by reference and changes
      // made here reflect in main()
      void func(vector &vect)
      {
      vect.push_back(30);
      }

      int main()
      {
      vector vect;
      vect.push_back(10);
      vect.push_back(20);

      func(vect); 
      
      for (int i=0; i
      
      Greg UtasG Offline
      Greg UtasG Offline
      Greg Utas
      wrote on last edited by
      #2

      Your code looks good to me. If you want func to return a vector, it should allocate it on the heap, and returning it in a unique_ptr would be better than just returning a raw pointer. But it looks like you want to update a vector that might already have entries, so passing it by reference makes sense. EDIT: func isn't a function template; it simply takes a vector argument. The term function template refers to a function defined by template func...

      Robust Services Core | Software Techniques for Lemmings | Articles
      The fox knows many things, but the hedgehog knows one big thing.

      <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
      <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

      1 Reply Last reply
      0
      • L Lost User

        I am trying to get comfortable using "vector". I have a function "returning" hardware parameters I need to process further "upstream" - including "main". I can pass a vector by reference or have been told to define "local vector" and "return" if from the function. I am not sure how "return vector" really works and have not tried it yet. The function returning "vector " syntax is scary... Could I return pointer to locally defined vector ? ( Or is is even feasible ?) Is there a specific resource I could read ? Something similar to this example , but with "return * vector - string "?

        // C++ program to demonstrate how vectors
        // can be passed by reference.
        #include
        using namespace std;

        // The vect is passed by reference and changes
        // made here reflect in main()
        void func(vector &vect)
        {
        vect.push_back(30);
        }

        int main()
        {
        vector vect;
        vect.push_back(10);
        vect.push_back(20);

        func(vect); 
        
        for (int i=0; i
        
        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        You function is OK as it stands.

        Quote:

        can pass a vector by reference or have been told to define "local vector" and "return" if from the function.

        That is fine whenever you have code similar to

        vector get_rand_vect(size_t size)
        {
        vector v;
        while (size--)
        {
        v.push_back(rand());
        }
        return v; // this is optimized by the compiler
        }

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

        In testa che avete, signor di Ceprano?

        L 1 Reply Last reply
        0
        • CPalliniC CPallini

          You function is OK as it stands.

          Quote:

          can pass a vector by reference or have been told to define "local vector" and "return" if from the function.

          That is fine whenever you have code similar to

          vector get_rand_vect(size_t size)
          {
          vector v;
          while (size--)
          {
          v.push_back(rand());
          }
          return v; // this is optimized by the compiler
          }

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

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

          That would not work in my function which retrieves unknown "size" of (string) data. As I said, I want to learn about vector , but more I look into it the more I like "old fashioned way " to pass a pointer to the function and let the calling code process the pointer afterwards. Seems much simpler.

          L CPalliniC 2 Replies Last reply
          0
          • L Lost User

            That would not work in my function which retrieves unknown "size" of (string) data. As I said, I want to learn about vector , but more I look into it the more I like "old fashioned way " to pass a pointer to the function and let the calling code process the pointer afterwards. Seems much simpler.

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

            Vectors are quite simple, but very powerful. But the usage will depend on what you are actually trying to do. In your example your func method does nothing useful, so it is not clear what actual problem you are trying to solve.

            1 Reply Last reply
            0
            • L Lost User

              That would not work in my function which retrieves unknown "size" of (string) data. As I said, I want to learn about vector , but more I look into it the more I like "old fashioned way " to pass a pointer to the function and let the calling code process the pointer afterwards. Seems much simpler.

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

              Quote:

              That would not work in my function which retrieves unknown "size" of (string) data.

              That's not a problem. It would work as well. Try, for instance

              #include #include using namespace std;

              vector get_a_fresh_vector_with_unknown_size()
              {
              vector v;
              int N = rand() % 128;

              for (int n = 0; n < N; ++n)
              v.push_back( rand() );

              return v;
              }

              int main()
              {
              auto v = get_a_fresh_vector_with_unknown_size();
              for (auto x : v)
              cout << x << " ";
              cout << endl;
              }

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

              In testa che avete, signor di Ceprano?

              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