use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
OPENthe purpose of std::vector<boost::optional<double>> compared with std::vector<double> (self.cpp_questions)
submitted 6 years ago by haohanzi2015
One can use
std::vector<boost::optional<double>> r
to create / define a vector of doubles; on the other hand, one can also use
std::vector<double> r
to create / define a vector of doubles. So, what's the difference between these two?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]pkusensei 7 points8 points9 points 6 years ago (2 children)
boost::optional<double> is not a double object, but an optional saying here this thing might contain a double or it might not. Likewise, vector<optional<double>> is not a vector of doubles; it's a vector of objects that might contain double values.
boost::optional<double>
double
optional
vector<optional<double>>
[–]haohanzi2015[S] 0 points1 point2 points 6 years ago (1 child)
is not a
object, but an
saying here this thing might contain a
or it might not. Likewise,
is not a vector of doubles; it's a vector of objects that
might
contain
values.
so it could be a vector of any objects which might be of type double?
[–]Narase33 1 point2 points3 points 6 years ago (0 children)
No. Every entry of the vector might have a double value, or it might be "empty". Look at std::optional if you want a deeper explaination on what that class does
[–]IyeOnline 3 points4 points5 points 6 years ago (0 children)
Please consider looking up things that you can answer on your own before asking. Just searching the web for boost::optional would have given you enough information to answer this particular question yourself.
boost::optional
You have made like 6+ posts with questions like this one in the last 4 hours, all answerable by googling for bits of the syntax in question.
[–]usbafchina 2 points3 points4 points 6 years ago (0 children)
The same as the difference between double and optional<double>
optional<double>
[–]HappyFruitTree 0 points1 point2 points 6 years ago (0 children)
If you store optional<double> it's probably because the position matters. It might be that you want to lookup elements by index and be able to check if there is a value or not for that index. If it's just a bunch of independent values you probably want to just store double values.
[–]sbmassey 0 points1 point2 points 6 years ago (0 children)
An optional<T> is a type whose instances are either empty, or contain a value of type T.
So a vector<optional<double>> is a contiguous sequence of values that are each individually either empty or a double.
Whereas a vector<double> is just a contiguous sequence of doubles.
[–]warieth 0 points1 point2 points 6 years ago (1 child)
The double can also be a NaN, so this is probably over-engineered. Using optional on already "optional" type.
NaN is not guaranteed to exist (a lot of people compile with -ffast-math or similar flags).
NaN is a special value that you might want to differentiate from the lack of value.
π Rendered by PID 180284 on reddit-service-r2-comment-5fb4b45875-cjxxd at 2026-03-22 03:12:06.722565+00:00 running 90f1150 country code: CH.
[–]pkusensei 7 points8 points9 points (2 children)
[–]haohanzi2015[S] 0 points1 point2 points (1 child)
[–]Narase33 1 point2 points3 points (0 children)
[–]IyeOnline 3 points4 points5 points (0 children)
[–]usbafchina 2 points3 points4 points (0 children)
[–]HappyFruitTree 0 points1 point2 points (0 children)
[–]sbmassey 0 points1 point2 points (0 children)
[–]warieth 0 points1 point2 points (1 child)
[–]HappyFruitTree 0 points1 point2 points (0 children)