|
|
|
|
|
|
| |
| |
|
|
|
|
| |
| |
|
|
Orchid XP v8 wrote:
> (I'm also curios to know exactly what the hell
> the function is returning...
Whatever random value happened to be in the appropriate place of the stack.
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
Invisible wrote:
> I wrote a function that returns the top element from the stack while
> simultaneously removing it. That cuts down the typing quite a bit.
The reason that isn't built-in is that it doesn't work well with objects.
If vector.pop_back() returned a reference to the popped object, you'd get
sure crashes (reading from freed memory, or at least, a destructed object).
If vector.pop_back() returned a copy of the popped object, you'd get bad
performance even when you don't need the object (useless copying), you just
want to remove it. And who would free that copy?
ints, however, are easily copyable and don't need "freeing" :)
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
> int foo(vector<int>& stack)
> {
> int x = stack.back();
> stack.pop_back();
> }
>
> For reasons I can't begin to understand,
>
> 1. This doesn't produce any compile-time error.
What compiler are you using?
If it's possible for you to acquire a copy of Visual Studio (even an oldish
one), I strongly recommend it.
- Slime
[ http://www.slimeland.com/ ]
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
> The tutorial recommends the C function atoi() - which, being a C function,
> is about as user-friendly as Windows 2. Much hair-pulling involving
> pointers ensues.
>
> (It turns out that you can assign a string to a char*, but not the
> reverse. Go figure. Anyway, the solution is simple enough...)
C strings aren't so bad. If you have a std::string, you can call its c_str()
method to get a (const) pointer to the internal string, which is just a
sequence of bytes. So, you can use
atoi( mystring.c_str() )
to convert to an integer. This is what I always use, though I see there are
other ways to do it.
> (For one thing, AFAIK, there is *no way* to know whether atoi()
succeeded or failed; if it "fails", you get a zero. So was the string
actually a zero, or did the conversion fail??)
As I understand it, atoi's behavior is to return the integer represented by
the longest sequence of characters starting at the beginning of the string
that represent an integer, defaulting to 0. By this definition it's not
possible to fail. =) Sometimes this is useful.
- Slime
[ http://www.slimeland.com/ ]
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
> What you could do, is have a function like DoOperation(stack,op), and use
> "op" as a token for the operation.
> if (cmd == "+") {DoOP(stack,0); continue;}
> if (cmd == "-") {DoOP(stack,1); continue;}
> if (cmd == "/") {DoOP(stack,2); continue;}
> if (cmd == "*") {DoOP(stack,3); continue;}
This is good, but instead of using integers like 0 1 2 3, I'd use an enum:
enum Operation
{
OP_ADD,
OP_SUBTRACT,
OP_DIVIDE,
OP_MULTIPLY,
};
An enum gives meaning to this sort of approach so you don't have to remember
what something like "2" means.
- Slime
[ http://www.slimeland.com/ ]
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
Slime schrieb:
> What compiler are you using?
>
> If it's possible for you to acquire a copy of Visual Studio (even an oldish
> one), I strongly recommend it.
>
Related to that, the express editions of Visual Studio are available
free of charge. Some say that the express editions are somewhat limited,
I however did not run into any walls with them. OpenGL, DirectX, GUI
work with Qt, all not a problem. Commandline apps work well, too.
For C/C++ work obviously you only need "Visual C++ 2008 Express
Edition". Download here: http://www.microsoft.com/express/download/
Manuel Kasten
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
Nicolas Alvarez <nic### [at] gmailcom> wrote:
> If you're writing C, you should use strtol instead. If you're writing C++,
> you should use a stringstream instead.
As I said in my other post, there is merit in the C functions: They are
much more efficient than stringstreams.
So if you are converting a *huge* amount of strings to ints in a tight
loop, you might want to consider the C functions rather than stringstreams.
In situations where speed has absolutely no relevance stringstreams are
much easier and safer to use.
--
- Warp
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
Warp <war### [at] tagpovrayorg> wrote:
> Another C function can be used to both parse an integer from a string
> and know if there was one: strtod(). However, its usage is even more
> complicated than atoi().
Yeah, it's strtol(), not strtod(). The latter is to read a floating
point number.
(This shows how clear those C function names are... ;) )
--
- Warp
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
Slime <fak### [at] emailaddress> wrote:
> atoi( mystring.c_str() )
> to convert to an integer. This is what I always use, though I see there are
> other ways to do it.
The problem is that with atoi() you can't know if there was an integer
in that string in the first place. If there wasn't, it just returns zero,
and you can't know if there was a zero in the string or something which
was not a parseable number.
(And no, it's not enough to check if mystring[0] == '0'. There are a
multitude of other ways for a valid number 0 to be in the string.)
This might be very relevant if you are checking if there was a number
of something else in the string, and act accordingly.
--
- Warp
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
Mueen Nawaz <m.n### [at] ieeeorg> wrote:
> If you DO want to read a whole line, look up the getline function. That
> will also take in an "empty" line.
The problem with getline() is precisely that it reads an entire line.
If there are several whitespace-separated things in that line, it will
still read the entire line.
Of course you can give that line to istringstream and then read those
whitespace-separated things with >> (and check with good() to see if it
succeeds).
--
- Warp
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |