|
I'd like a floor function with the syntax
int floor(double x);
but std::floor returns a double . Is
static_cast <int> (std::floor(x));
guaranteed to give me the correct integer, or could I have an off-by-one problem? It seems to work, but I'd like...
Started by Jesse Beder on
, 6 posts
by 6 people.
Answer Snippets (Read the full thread at stackoverflow):
(or at least/converter.hpp> int main() { typedef boost::numeric::converter<int,double> Double2Int ; int....
That'd be hard to do if std::floor returned an int.
That is, round 1.3f to 1.0f, rather than to 1 .
|
|
Hi,
how do I bind a std::ostream to either std::cout or to an std::ofstream object, depending on a certain program condition? Although this invalid for many reasons, I would like to achieve something that is semantically equivalent to the following:
std...
Started by Matthias Vallentin on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
This is exception-safe:
void process(std::ostream &os); int main(int argc, char *argv[]) { std() { os.rdbuf(old_buf); } std::ostream& os; std::streambuf * old_buf; }; int main() { // or: ....
|
|
I have next code:
#include <iostream> #include <algorithm> #include <map> #include <iterator> //namespace std //{ std::ostream& operator << ( std::ostream& out, const std::pair< size_t, size_t >& rhs ) { out <<...
Started by bb on
, 7 posts
by 7 people.
Answer Snippets (Read the full thread at stackoverflow):
Typedef std::map<size_t, size_t> MyMap; template....
You can then pass that as an argument to std::for_each to do the work.
There is no standard way to cout a std::pair because, well, how you want it printed is probably function.
|
Ask your Facebook Friends
|
Hi everyone,
I am programming in C++ and I'm not sure how to achieve the following:
I am copying a file stream to memory (because I was asked to, I'd prefer reading from stream), and and then trying to access its values to store them into strings and ...
Started by Val on
, 6 posts
by 6 people.
Answer Snippets (Read the full thread at stackoverflow):
Example:
/....
// istringstream iss(buffer); int for "parser generator".
// Create buffer and copy stream to it std::vector<char> follows:
#include <sstream> #include <string> // ...
This makes memory management implicit.
|
|
(I'm using Visual C++ 2008) I've always heard that main() is required to return an integer, but here I didn't put in return 0; and and it compiled with 0 errors and 0 warnings! In the debug window it says the program has exited with code 0. If this function...
Started by da_code_monkey on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
The same return statement....
And it continuesI'm pretty sure VC++ just inserts a return 0 if you don't include one in main functions.
*/ }
....
*/ }
and
int main(int argc, char* argv[]) { /* ...
int main() { /* ...
|
|
What is the benefit of inheriting from std::binary_function (or std::unary_function)?
For example I have such code:
class Person { public: Person(); Person(int a, std::string n); Person(const Person& src); int age; std::string name; }; Person::Person(...
Started by Darius Kucinskas on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
If you ever going to use your Functors with other std Functors modificators like not1 person1 against // another person std::bind1st(PersonGreater(), person1)
Now, the returned binder1st Standard as std::function....
There is no benefits.
|
|
I want to write a function that outputs something to a ostream that's passed in, and return the stream, like this:
std::ostream& MyPrint(int val, std::ostream* out) { *out << val; return *out; } int main(int argc, char** argv){ std::cout <<...
Answer Snippets (Read the full thread at stackoverflow):
Lt;< mp.val_; return os; } private: int val_; }; int main(int argc, char** argv) { std::cout <) { mp.os_ << mp.val_; return os_; } private: int val_; std::ostream& os_ }; int main....
|
|
How can operator bool() cause an error when declaring operator std::string in a class and also serving as an implicit conversion to string by itself?
#include <iostream> #include <string> using namespace std; class Test { public: operator ...
Started by piotr on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
When the compiler sees....
The problem you are facing (besides operator std::string() returning a bool) is that implicit conversions trigger when you want and when you don't.
Your operator std::string() needs to return a string, not a bool.
|
|
This isn't a design question, really, though it may seem like it. (Well, okay, it's kind of a design question). What I'm wondering is why the C++ std::fstream classes don't take a std::string in their constructor or open methods. Everyone loves code examples...
Started by Bernard on
, 10 posts
by 9 people.
Answer Snippets (Read the full thread at stackoverflow):
A better question would....
The same goes for std.
Many containers have a swap member function, but no overload of std::swap is supplied.
One other example is std::swap .
std::string and its use in the library is one of these.
|
|
In the following example i expected the swap of the bits. Instead the second bit becomes overwritten, but why and how could i achieve the expected behavior?
#include <iostream> #include <string> #include <algorithm> using namespace std...
Started by Christian Ammer on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
Right ) { bool temp = (bool)left; left = (bool)right; right = (bool)temp; } }
Then:
int main to 'swap(std::bitset<2u>::reference, std::bitset<2u>::reference)' /usr/include/c++/4.3/bits/stl_move.h:80: note: candidates....
|