Thread: vector in functions

1. 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. 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. Yes i need exactly what i posted...

4. Then fix your function.

5. There is no prob with the function..
I define it this way
Code:
` void func(vector<pair<int, int> > a[])`

6. 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. Originally Posted by dpp
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. fine ll try

9. Originally Posted by dpp
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. well,rossipoo you were a great help

11. just out of curiosity, why do you need a c-style array of vectors? why not use another vector to contain the array?

12. You can use push_back instead of a fixed-size vector too... It will save memory.

13. Originally Posted by Elysia
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