Thread: Passing a vector back to main

  1. #1
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374

    Passing a vector back to main

    Question: How do I pass a vector of strings back to main?

    The code below is wrong, but what should it be?


    Code:
    #include<iostream>
    #include<vector>
    
    void passback(???);
    
    int main()
    {
       
      void passback(array???); 
       
       for(int i=0; i<array.size(); i++)
       {
          cout<<array[i];
        }
    
    }
    
    void passback(????)
    {
         vector <string> array;
        array.push_back("blah");
        array.push_back("yada");
        array.push_back("blah");
    
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    main should pass a reference to the vector.

    Code:
    #include<iostream>
    #include<vector>
    
    void passback(std::vector<std::string>& s);
    
    int main()
    {
         std::vector <std::string> array;
       
         passback(array); 
       
       for(int i=0; i<array.size(); i++)
       {
          cout<<array[i];
        }
    
    }
    
    void passback(std::vector<std::string>& array)
    {
        array.push_back("blah");
        array.push_back("yada");
        array.push_back("blah");
    
    }

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can also just return it like any other type, although passing by reference is usually better since it avoids copynig everything in the vector.

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Thanks

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or you can use a pointer (although you don't need to, and so a reference would be better).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing params to main()
    By mike11 in forum C++ Programming
    Replies: 14
    Last Post: 06-21-2005, 12:36 PM
  2. Passing a memory location back to main
    By jbsloan in forum C Programming
    Replies: 5
    Last Post: 03-07-2005, 12:17 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. passing a file name to main()
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 09-06-2001, 04:08 PM