Serving the Quantitative Finance Community

 
User avatar
wynand494
Topic Author
Posts: 0
Joined: February 20th, 2004, 5:22 am

Vectors must be passed by value to be able to use iterators on them in C++?

August 7th, 2008, 9:44 pm

HiI have the following code which does nt compile and has error message Error 1 error C2440: 'initializing' : cannot convert from 'std::_Vector_const_iterator<_Ty,_Alloc>' to 'std::_Vector_iterator<_Ty,_Alloc>'However if I make the "const std::vector<dat2008:ate>& Holidays" to just by value i.e "std::vector<dat2008:ate> Holidays", i get a succesful compilation. I am not sure why this is happening. Do i need to pass vectors by value to be able to use iterators on them?
 
User avatar
wynand494
Topic Author
Posts: 0
Joined: February 20th, 2004, 5:22 am

Vectors must be passed by value to be able to use iterators on them in C++?

August 7th, 2008, 9:52 pm

Posted the incorrect code for the question. Same question but code as below..."const std::vector<dat2008:ate>& Holidays" gives a compilation error while "std::vector<dat2008:ate>&Holidays" does not...
 
User avatar
Athletico
Posts: 14
Joined: January 7th, 2002, 4:17 pm

Vectors must be passed by value to be able to use iterators on them in C++?

August 8th, 2008, 12:23 am

Change iterator to const_iterator and it should compile. The vector is passed const, elements are immutable, so they must be referenced via const_iterators.
 
User avatar
wynand494
Topic Author
Posts: 0
Joined: February 20th, 2004, 5:22 am

Vectors must be passed by value to be able to use iterators on them in C++?

August 8th, 2008, 4:12 am

thanks a lot. really appreciate.
 
User avatar
bojan
Posts: 0
Joined: August 8th, 2008, 5:35 am

Vectors must be passed by value to be able to use iterators on them in C++?

August 8th, 2008, 9:58 am

By the way, I do not think your code will correctly handle holiday days that fall on a Friday or two consecutive holiday days.
 
User avatar
wynand494
Topic Author
Posts: 0
Joined: February 20th, 2004, 5:22 am

Vectors must be passed by value to be able to use iterators on them in C++?

August 8th, 2008, 12:01 pm

you are right. thanks very much.
 
User avatar
Athletico
Posts: 14
Joined: January 7th, 2002, 4:17 pm

Vectors must be passed by value to be able to use iterators on them in C++?

August 8th, 2008, 12:13 pm

bojan is right. Assuming the Holidays vector is sorted, try this code:
Last edited by Athletico on August 14th, 2008, 10:00 pm, edited 1 time in total.
 
User avatar
bojan
Posts: 0
Joined: August 8th, 2008, 5:35 am

Vectors must be passed by value to be able to use iterators on them in C++?

August 8th, 2008, 1:44 pm

Athletico fragment would need a few adjustments:first while loop condition : test for end of container first then access iterator (i.e. swap expressions on either side of &&)second while loop conditions: parenthesis around the last two sub expressions required I thinkhols_iter->serial() rather than hols_iter.serial()Ah, the joys of writing C++ without a compiler and debugger...
 
User avatar
Athletico
Posts: 14
Joined: January 7th, 2002, 4:17 pm

Vectors must be passed by value to be able to use iterators on them in C++?

August 8th, 2008, 1:57 pm

first point is spot on, and that guards against an empty Holidays vector.second point, no because && takes precedence. but go ahead w/the parens if style points are awarded.>> Ah, the joys of writing C++ without a compiler and debugger... not to mention before the first cup of morning joe...
 
User avatar
wynand494
Topic Author
Posts: 0
Joined: February 20th, 2004, 5:22 am

Vectors must be passed by value to be able to use iterators on them in C++?

August 9th, 2008, 7:35 am

thanks guys
 
User avatar
ahew
Posts: 0
Joined: November 30th, 2002, 12:43 pm

Vectors must be passed by value to be able to use iterators on them in C++?

August 13th, 2008, 10:44 am

Hi,Does this fragment"while (retval.weekday() == 1 || retval.weekday() == 7 "refer to weekends of Saturdays and Sundays?If so, and if your code will be used in the Middle East, you may need to allow a flexible definition of the weekend days, eg, Friday and Saturday.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

Vectors must be passed by value to be able to use iterators on them in C++?

August 13th, 2008, 1:53 pm

QuoteOriginally posted by: ahewHi,Does this fragment"while (retval.weekday() == 1 || retval.weekday() == 7 "refer to weekends of Saturdays and Sundays?If so, and if your code will be used in the Middle East, you may need to allow a flexible definition of the weekend days, eg, Friday and Saturday.'1' and '7' are magic numbers, a NO NO in computing.
 
User avatar
dirtydroog
Posts: 0
Joined: July 12th, 2007, 6:32 pm

Vectors must be passed by value to be able to use iterators on them in C++?

August 15th, 2008, 8:03 am

QuoteOriginally posted by: CuchulainnQuoteOriginally posted by: ahewHi,Does this fragment"while (retval.weekday() == 1 || retval.weekday() == 7 "refer to weekends of Saturdays and Sundays?If so, and if your code will be used in the Middle East, you may need to allow a flexible definition of the weekend days, eg, Friday and Saturday.'1' and '7' are magic numbers, a NO NO in computing.Usually, not always.Which would you rather see:a >>= 1;or a >>= SHIFT_AMOUNT;The former for me every time.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

Vectors must be passed by value to be able to use iterators on them in C++?

August 15th, 2008, 8:27 am

QuoteOriginally posted by: dirtydroogQuoteOriginally posted by: CuchulainnQuoteOriginally posted by: ahewHi,Does this fragment"while (retval.weekday() == 1 || retval.weekday() == 7 "refer to weekends of Saturdays and Sundays?If so, and if your code will be used in the Middle East, you may need to allow a flexible definition of the weekend days, eg, Friday and Saturday.'1' and '7' are magic numbers, a NO NO in computing.Usually, not always.Which would you rather see:a >>= 1;or a >>= SHIFT_AMOUNT;The former for me every time.This is a decision between readability and maintainability, so on each count latter wins IMO.The problem is '1' is heavily overloaded; it means different things in different places, while SHIFT_AMOUNT has (should have) a unique semantic meaning.So, somewhere (once) we say 'define SHIFT_AMOUNT = 2'The former for me 'me' can be the creator of code or the maintainer of code. They have orthogonal viewpoints on the quality of code, but this does not have to be so.The Arianne V used a magic number, and we know what happened there.
Last edited by Cuchulainn on August 14th, 2008, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

Vectors must be passed by value to be able to use iterators on them in C++?

August 15th, 2008, 8:31 am

magic numbers The term magic number also refers to the bad programming practice of using numbers directly in source code without explanation. In most cases this makes programs harder to read, understand, and maintain. Although most guides make an exception for the numbers zero and one, it is a good idea to define all other numbers in code as named constants.Because magic numbers are of an arbitrary value, they do not often carry a meaning by themselves; in most cases it is up to the documentation of one's code to specify exactly what the magic number represents. Also, magic numbers are not typesafe, that is, one could erroneously add one to another and arrive at a nonsensical result. Lastly, although highly coincidental, situations arise when numbers may accidentally match magic numbers during comparison operations. It is for these reasons that the use of Enumerated types, or enums, is quickly overtaking the use of magic numbers. Although enums are represented in most languages as an integer (a notable exception is Java)[5], the name of the enum is used rather than the number itself.lots of fun with magic numbers, not droogI am reallly surprised that you could say such a thing.
Last edited by Cuchulainn on August 14th, 2008, 10:00 pm, edited 1 time in total.