Is it possible to have stl containers as default parameters

1
2
3
4
   class ABC
   {
        ABC(int i,int j=0);
   }


The above code takes j as a default parameter. But is it possible to assign a default value to an std::vector or std::set???



Wait wait wait. You're not hoping to assign an std::vector<int> or std::list<int> to a single int, are you? If so, you're going to have some trouble making it work in that direction.

-Albatross
lol.. Nope
1
2
3
4
   class ABC
   {
        ABC(int i,int j=0);
   }


I am checking the possibility of something like this

1
2
3
4
   class ABC
   {
        ABC(int i,std::vector<int>=%some default value%);
   }
Yes, although you can't do much other than fill it with a single value or a specific size...

Example:
1
2
3
4
class ABC
{
     ABC(int i, std::vector<int> vec = std::vector<int>(3));
}
Last edited on
What do you mean by specified size? can you please elaborate?
He means that you cannot do much more than set the default size of the vector being used as a variable, and if you want fill all its spaces with one value.

-Albatross
closed account (S6k9GNh0)
You can tell it to reserve a certain amount of space on construction and then you can tell it to give each reserved slot a default value. That's really the best possible you can do with the constructor.

http://cplusplus.com/reference/stl/vector/vector/
Thats great.. Thanks...


explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );

I can see how it can be used through the example. But still, I am not able to understand the prototype.

I guess vector is a constructor. If so, whats being passed?

const T& value= T() -- This is an accessor/mutator. but how is that we are able to have it as a funtion argument.

const Allocator& = Allocator() - what is this?

Thanks
1st argument - The size you want the vector to be
2nd argument - No it isn't. It is what you want to "filled" stuff to be, i.e. if you put a size of 3 and put 5 as the second argument, you would get a vector with three 5s in it.
3rd argument - Something you probably won't need to change or care about
@firedraco

I think i should ve framed the question a little different..

2nd argument -

Alrite.. but what does this stand for?
const T& value= T()
What ll happen if I use it in a function i write?? I am trying to understand the way the prototype itself is declared

3rd argument - Something you probably won't need to change or care about
Yeah.. But what is it?
2nd arg - It means it takes a const T value by reference with the default being a defaultly constructed T. T being whatever type you are putting in the vector.

3rd arg - It tells the vector where and how to allocate and delete it's memory. So you could partition some memory yourself and give it to the vector (assuming you have created an allocator for it)
The second default, to simplify it, calls the constructor of the type of object passed to it (T is a template) should no instance be given.

The third argument manipulates the allocator the vector uses. The default uses a standard allocator.

-Albatross
Thanks firedraco & Albatross

If my understanding is rite about the third argument,

Point 1
=====
The third argument could be used in case if I want to write my own fucntion for allocation. In that case I should pass the name of the function ( when I pass the name, i assume that the address of the function is passed)

Point 2
=====
Both 2nd and 3rd arguments are default arguments..


Correct me if I am wrong.




Topic archived. No new replies allowed.