vector in functions

This is a discussion on vector in functions within the C++ Programming forums, part of the General Programming Boards category; I use something like this Code: vector<pair<int, int> >a[100001]; Now i need to pass a to a function called func... ...

  1. #1
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    vector in functions

    I use something like this
    Code:
    vector<pair<int, int> >a[100001];
    Now i need to pass a to a function called func...
    How will the call look like..
    Code:
    func(a)
    gives error

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    a is an array of vectors. Are you sure you want an array of vectors? If you did, then make sure your function accepts an array of vectors. If you wanted just a vector, but with 100001 elements, then you need to call the constructor, not declare an array:
    Code:
    vector<pair<int, int> >a(100001);

  3. #3
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    Yes i need exactly what i posted...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Then fix your function.

  5. #5
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    There is no prob with the function..
    I define it this way
    Code:
     void func(vector<pair<int, int> > a[])

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    Code:
    void func (vector <pair <int, int> >* a) {
    	a[0][0] = make_pair (7, 8);
    	// do something with a...
    }
    And then ...
    Code:
    vector <pair <int, int> > a[100001];
    a[0].push_back (make_pair (5, 6) );
    func (a);
    cout << a[0][0].first << endl;
    cout << a[0][0].second;

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by dpp View Post
    There is no prob with the function..
    I define it this way
    Code:
     void func(vector<pair<int, int> > a[])
    Then there's no error, which I guess we should have realized given that there was no error message in the original post.

  8. #8
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    fine ll try

  9. #9
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Quote Originally Posted by dpp View Post
    fine ll try
    That's gratitude there :-D Good job helping tabstop and rossipoo, I'm sure he appreciates it with messages like that!

  10. #10
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    well,rossipoo you were a great help

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    2,590
    just out of curiosity, why do you need a c-style array of vectors? why not use another vector to contain the array?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    You can use push_back instead of a fixed-size vector too... It will save memory.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    2,590
    Quote Originally Posted by Elysia View Post
    You can use push_back instead of a fixed-size vector too... It will save memory.
    that will work fine until it grows very large, and the reallocation exhausts the available memory on the machine. we had a thread about that a while back, where inserting an item caused it to attempt to allocate more than the 2GB available to the process. if you know how big it will be, the best bet is to reserve the space ahead of time, then it will never need to re-allocate memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 06:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 12:50 PM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 02:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21