Re: [suse-programming-e] STL vector as a function parameter with default value (heres another one for you Jerry...)
Ah, I tried const vector<unsigned int>::iterator; instead of vector<unsigned int>::const_iterator; good guess. Your solution makes sense. Thanks B-) On Tuesday 15 February 2005 01:14 pm, Michael Stevens wrote:
Brad,
On Tuesday 15 February 2005 16:37, Brad Bourn wrote:
On Saturday 12 February 2005 03:52 am, Michael Stevens wrote:
This is an error because 'vector<unsigned int>()' is a temporary and you can only pass temporaries to 'const' reference parameters. Your function must be declared func(const vector<unsigned int>& foo); OR func(vector<unsigned int> foo); to work with temporaries.
This does allow it to compile, however, once inside the function, I can't use the std::find function because STL doesn't seem to like const iterators...
I don't think you posted the code inside your function so this is just guess work.
void func(const vector<unsigned int>& foo) { xx = std::find( foo.begin(), foo.end(), 1); }
Will work. foo.begin() and .end() will automatically return 'const_iterators' to the vector. The tricky part is getting the type of 'xx' correct. You are probably using the type vector<unsigned int>::iterator Because foo is const you need to us e the vector<unsigned int>::const_iterator this is const with regard to the elements in the vector.
If you are interested in using the STL further and looking for a book that takes a look at the fundamentals that have a look at "Generic Programming and the STL", Matthew H. Austern
Good luck, Michael
participants (1)
-
Brad Bourn