Jump to content
  • Advertisement
Sign in to follow this  
3Dgonewild

2 questions

This topic is 4259 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

1) In c++ , when using large numbers (eg 8000000) is it better to use "long" variable or "integer" ? (note that i dont want "commas" or "periods" in the final output) 2) How can i copy some characters of a string at a specific position? Eg : I have this string : "Hello" how can i grab the first 3 characters? By the way , in pascal you can do this : copy(string,begin_pos,end_pos)... Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by 3Dgonewild

1) In c++ , when using large numbers (eg 8000000) is it better to use "long" variable or "integer" ?
(note that i dont want "commas" or "periods" in the final output)


The actual size of int and long isn't defined in C++ - it varies from platform to platform (and from compiler to compiler).

On windows, int will be 32bits (−2147483648 to +2147483647) which is easily big enough for 8000000.


Quote:

I have this string :
"Hello"
how can i grab the first 3 characters?

By the way , in pascal you can do this : copy(string,begin_pos,end_pos)...



#include <string>

int main()
{
std::string hello("Hello");
std::string result(hello.begin(),hello.begin()+3);
}


Share this post


Link to post
Share on other sites
Quote:
Original post by Nitage
Quote:
Original post by 3Dgonewild

1) In c++ , when using large numbers (eg 8000000) is it better to use "long" variable or "integer" ?
(note that i dont want "commas" or "periods" in the final output)


The actual size of int and long isn't defined in C++ - it varies from platform to platform (and from compiler to compiler).

On windows, int with be 32bits (−2147483648 to +2147483647) which is easily big enough for 8000000.


Quote:

I have this string :
"Hello"
how can i grab the first 3 characters?

By the way , in pascal you can do this : copy(string,begin_pos,end_pos)...


*** Source Snippet Removed ***


Really thanks , but what about linux? int variable will do ?

Share this post


Link to post
Share on other sites
Quote:
Original post by 3Dgonewild
1) In c++ , when using large numbers (eg 8000000) is it better to use "long" variable or "integer" ?
(note that i dont want "commas" or "periods" in the final output)

It depends on the magnitude of the number and the sizes of the various types under your compiler and system. On most 32bit systems and compilers int and long are the same size and have a range of -231 to 231 - 1. The display of commas or periods in output will be a property of the display mechanism, not the variable type. int is the type to use by default if you have no particular reason to use something else.

Quote:
2) How can i copy some characters of a string at a specific position?

Eg :

I have this string :
"Hello"
how can i grab the first 3 characters?

By the way , in pascal you can do this : copy(string,begin_pos,end_pos)...

That depends. Do you have a string (std::string string = "Hello";), or a string literal ("Hello")? In the former case the substr() member function will obtain a substring. In your case you want std::string substring = string.substr(0 /*start from first character*/, 3 /*number of characters in substring*/);. In the latter case you'll want to construct a std::string from a subrange of your string literal using std::string substring("Hello", 3 /*number of characters in substring*/);.

Σnigma

Share this post


Link to post
Share on other sites
You can also use substr.


std::string myString = "Hello";
std::string mySubString = myString.substr ( 0 , 3 );




EDIT: Beaten by enigma [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by 3Dgonewild
Thanks all!..
But please tell me if "int" will work with linux too!(we're talking about 32bit systems)


Will it work? Usually.

Is it guaranteeed? No.

If you're going for portability, then the only guaranteed way is to define your own types and base your code on that.

Keep in mind, linux may mean someone will run it on embedded linux on a platform that uses a highly non-standard chipset and proprietary compiler.

I've found myself in such situation, but in my case the problem was with floats, although I don't really remember exactly what.

Share this post


Link to post
Share on other sites
In practice, on a 32-bit operating system, int will be 32 bits. As Nitage noted, that's plenty of space for the numbers you've got.

It might be overkill for your situation, but if you absolutely need to guarantee the size of your variables, I highly recommend Boost's cstdint. You may also find std::numeric_limits useful.

Share this post


Link to post
Share on other sites
The usual policy is to use 'int' unless/until you discover that it doesn't do what you need. (It's intended to be the "default" integral type on your system; i.e. the one that is most readily handled.) The reason (or so I like to think) it's "int" and not "integer" is so it will be shorter (== lazy programmers will be encouraged to use it ;) And laziness is a virtue, just ask Larry Wall) than the other names for numeric types.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!