• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
markrodgers11

string::size_type

19 posts in this topic

One of the example programs in my C++ book ;)

I'm not really understanding this completely... I kinda understand how it is working but there are a few things that are confusing me..
Such as string::size_type. The book said that it returns the size of something (derp) but i tried outputting the result and it was like a bunch of random number. I didn't count but it was probably around 8 random numbers, for example: 35694625. I don't understand how that gets returned if it says it gets the size of something? o.o

[code]#include <iostream>
#include <string>
using std::cin; using std::endl;
using std::cout; using std::string;
int main()
{
cout << "Please enter your first name: ";
string name;
cin >> name;
const string greeting = "Hello, " + name + "!";

int pad;
cout << "Enter the desired amount of padding: ";
cin >> pad;
const int rows = pad * 2 + 3;
const string::size_type cols = greeting.size() + pad * 2 + 2;
cout << endl;
for (int r = 0; r != rows; ++r) {
string::size_type c = 0;
while (c != cols) {
if (r == pad + 1 && c == pad + 1) {
cout << greeting;
c += greeting.size();
} else {
if (r== 0 || r == rows - 1 ||
c == 0 || c == cols - 1)
cout << "*";
else
cout << " ";
++c;
}
}
cout << endl;
}
system("pause");
return 0;
}[/code]
0

Share this post


Link to post
Share on other sites
Well the code is taken directly from the book I am reading on C++ and it compiles fine? The book was pretty highly recommended by people on lots of different forums so i'm sure the code is fine its just that I don't really understand what ::size_type is/does. :/
0

Share this post


Link to post
Share on other sites
As Endurion said, it is just a typedef. It is the type used by the [i]std::string[/i] class to represent a size. For example, the [i]size()[/i] member function returns the size of the string, and the type of the value returned is [i]std::string::size_type[/i].

[code]
std::string foo = "Hello, world!";
std::string::size_type length = foo.size();
[/code]
1

Share this post


Link to post
Share on other sites
[quote name='markrodgers11' timestamp='1349884563' post='4988750']
I don't really understand what ::size_type is/does.[/quote]
Exactly what it says on the tin: it is the type used to store the size of the string.

In general, std::string::size_type will be a typedef (alias) to size_t, and size_t will be a typedef to an unsigned int.

- What does that mean? It means that you can use string::size_type exactly as you would use an unsigned int.

- If size_type is just an unsigned int, why do we need it? That's probably too technical for this forum. But in brief, there are cases where size_type might not be an unsigned int (it could be an unsigned long instead, on a 64-bit machine). Using size_type lets our code work seamlessly on such a platform.
0

Share this post


Link to post
Share on other sites
SIZE_T just returns a integar or length of bytes for the string. SIZE_T is the same function of sizeof(). SIZE_T just stores the integar length of a string or what not.

[code]

SIZE_T StringLength;
string Greeting = "Hello, I'm George!";
StringLength = sizeof(Greeting);
cout << StringLength;

[/code]

As you see the SIZE_T is a typdef that stores the integer of length. Where as sizeof function returns the length of the string Greeting.
-4

Share this post


Link to post
Share on other sites
ohokay well thanks for the useful responses, i read closer in my book and it was explained I just happened to miss it on the first read through of that section :P

Thanks for helping out anyways! :D
0

Share this post


Link to post
Share on other sites
[quote name='SIC Games' timestamp='1349931097' post='4988961']
As you see the SIZE_T is a typdef that stores the integer of length.
[/quote]
This is misleading.
swiftcoder was closest to the correct answer.

It is not an unsigned int. But sometimes it might be.
It is a type that is defined to be the resulting type of the sizeof() and offsetof() operators, which means compiling for an x86 or other 32-bit system means it will have a range from 0 to 4,294,967,295, whereas on x64 and other 64-bit systems it will have a range from 0 to 18,446,744,073,709,551,615.

But more simply, 32-bit machines and 64-bit machines have different ranges of addresses, and size_t is a type that is guaranteed to cover that whole range, whatever it may be.

Why does string use that type to return the length of the string?
Because the length of the string could be longer on a 64-bit machine that is possible on a 32-bit machine. If you used an “unsigned int” in Microsoft® Visual Studio® your string will be limited to 4,294,967,295 characters even if you are building for a 64-bit machine.

To use an integer instead of size_t is always a flaw.
If any function returns a size_t, you should always store the result in a size_t unless you have specific constraints (such as additional checks to ensure the string is not longer than 4,294,967,295 characters in length) that allow you to do so.


[quote name='Cornstalks' timestamp='1349933808' post='4988968']
I'm gonna nitpick this 'cause otherwise it might confuse some beginners. First, there is no SIZE_T.
[/quote]
[url="http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx"]SIZE_T[/url] is a Windows data type.


L. Spiro Edited by L. Spiro
2

Share this post


Link to post
Share on other sites
its just a typedef.

for example on visual studio

size_type is 32bit number on 32bit operating system.
size_type is 64bit number on 64bit operating system

size_type is what's used within the string for tracking guess what, the size.

so instead of returning int string::size();
its just size_type string::size();

you can make a type out of any other type using typedefs.

typedef unsigned int uint;

from now on I can use uint in as if it was an unsigned int. Doing this creates one place where you can change a type

It can also save typing.

Say you have a

std::vector< MyClass*> mVector;

now the iterator for it is

std::vector< MyClass*>::iterator

thats a lot of typing.

you can instead do

typedef std::vector< MyClass*>::iterator MyClassIter;

from now on you can use MyClassIter instead of typing out that long peice of code ( though out the code ) just to get a type.

c++11 can further help typing using auto. But the example still stands because you can also use it for return types and as method parameters ect... Edited by EddieV223
0

Share this post


Link to post
Share on other sites
[quote name='SIC Games' timestamp='1349931097' post='4988961']
SIZE_T just returns a integar or length of bytes for the string. SIZE_T is the same function of sizeof(). SIZE_T just stores the integar length of a string or what not.

[code]

SIZE_T StringLength;
string Greeting = "Hello, I'm George!";
StringLength = sizeof(Greeting);
cout << StringLength;

[/code]

As you see the SIZE_T is a typdef that stores the integer of length. Where as sizeof function returns the length of the string Greeting.
[/quote]

std::string varString = "Hello World";
unsigned int size = sizeof(varString);
sizeof DOES NOT RETURN THE SIZE OF THE STRING INSIDE THE STD::STRING

It returns the size of the string class. The string class uses dynamic memory to store cstrings. So only the pointer is counted as well as any other members. If you want the size of an std::string you use .size();
0

Share this post


Link to post
Share on other sites
[quote name='SIC Games' timestamp='1349931097' post='4988961']
SIZE_T just returns a integar or length of bytes for the string. SIZE_T is the same function of sizeof(). SIZE_T just stores the integar length of a string or what not.

[code]

SIZE_T StringLength;
string Greeting = "Hello, I'm George!";
StringLength = sizeof(Greeting);
cout << StringLength;

[/code]

As you see the SIZE_T is a typdef that stores the integer of length. Where as sizeof function returns the length of the string Greeting.
[/quote]

First of all

http://www.cplusplus.com/reference/clibrary/cstring/size_t/

http://msdn.microsoft.com/en-us/library/4s7x1k91%28v=vs.71%29.aspx

there is size_t; and plus sizeof() returns bytes of characters. Rather it be a sturct, a class or whatever.

I helped out and that's it.
0

Share this post


Link to post
Share on other sites
[quote name='EddieV223' timestamp='1349940188' post='4988999']
[quote name='SIC Games' timestamp='1349931097' post='4988961']
SIZE_T just returns a integar or length of bytes for the string. SIZE_T is the same function of sizeof(). SIZE_T just stores the integar length of a string or what not.

[code]

SIZE_T StringLength;
string Greeting = "Hello, I'm George!";
StringLength = sizeof(Greeting);
cout << StringLength;

[/code]

As you see the SIZE_T is a typdef that stores the integer of length. Where as sizeof function returns the length of the string Greeting.
[/quote]

std::string varString = "Hello World";
unsigned int size = sizeof(varString);
sizeof DOES NOT RETURN THE SIZE OF THE STRING INSIDE THE STD::STRING

It returns the size of the string class. The string class uses dynamic memory to store cstrings. So only the pointer is counted as well as any other members. If you want the size of an std::string you use .size();
[/quote]

sizeof returns the length in bytes of a char. - whether it be a array or what not. Whatever - I was trying to help out; screw that idea.
-2

Share this post


Link to post
Share on other sites
[quote name='SIC Games' timestamp='1349964519' post='4989104']
there is size_t; and plus sizeof() returns bytes of characters. Rather it be a sturct, a class or whatever.
[/quote]
“string” is a structure, so sizeof() returns the size of that structure, not the size of the string it manages.
If it was instead:
[CODE]const char szString[] = "Hello fellow citizens.";[/CODE]
Then sizeof() would return the number of characters, including the terminating NULL.


[quote name='SIC Games' timestamp='1349964757' post='4989106']
screw that idea.
[/quote]
I supported you on SIZE_T but I feel less sorry for you after you posted this.


L. Spiro
2

Share this post


Link to post
Share on other sites
[quote name='SIC Games' timestamp='1349964757' post='4989106']
sizeof returns the length in bytes of a char. - whether it be a array or what not. Whatever - I was trying to help out; screw that idea.[/quote]
You are, unfortunately, blatantly wrong on this count.

sizeof() returns the correct size for the given type, or the correct length of a statically allocated array. std::string uses dynamically allocated memory to store the string contents, so sizeof() will not see that memory.

[code]#include <string>
#include <cassert>
int main() {
std::string A = "Hello, World!";
std::string B = "Hi";

assert( sizeof(A) == sizeof(B) );
}[/code]
1

Share this post


Link to post
Share on other sites
[quote name='L. Spiro' timestamp='1349967670' post='4989118']
[quote name='SIC Games' timestamp='1349964519' post='4989104']
there is size_t; and plus sizeof() returns bytes of characters. Rather it be a sturct, a class or whatever.
[/quote]
“string” is a structure, so sizeof() returns the size of that structure, not the size of the string it manages.
If it was instead:
[CODE]const char szString[] = "Hello fellow citizens.";[/CODE]
Then sizeof() would return the number of characters, including the terminating NULL.


[quote name='SIC Games' timestamp='1349964757' post='4989106']
screw that idea.
[/quote]
I supported you on SIZE_T but I feel less sorry for you after you posted this.


L. Spiro
[/quote]

Watever, dude. Seriously, all I hear is "IM WRONG" "I AM WRONG" from all you people on here. So, whatever. Have a good day; dude.
-3

Share this post


Link to post
Share on other sites
[quote name='swiftcoder' timestamp='1349967713' post='4989119']
[quote name='SIC Games' timestamp='1349964757' post='4989106']
sizeof returns the length in bytes of a char. - whether it be a array or what not. Whatever - I was trying to help out; screw that idea.[/quote]
You are, unfortunately, blatantly wrong on this count.

sizeof() returns the correct size for the given type, or the correct length of a statically allocated array. std::string uses dynamically allocated memory to store the string contents, so sizeof() will not see that memory.

[code]#include <string>
#include <cassert>
int main() {
std::string A = "Hello, World!";
std::string B = "Hi";

assert( sizeof(A) == sizeof(B) );
}[/code]
[/quote]

Yeah you too have a good day, keep on warning people; bro! Cause you're cool like that.
-3

Share this post


Link to post
Share on other sites
I have been wrong too; it is all about how you handle it.
http://www.gamedev.net/topic/628778-compressing-vertex-attribute-normals/page__p__4965067#entry4965067

People don’t lose respect for you for being wrong. They lose respect for you for handling it poorly.


L. Spiro
2

Share this post


Link to post
Share on other sites
[quote name='L. Spiro' timestamp='1349968887' post='4989126']
I have been wrong too; it is all about how you handle it.
[url="http://www.gamedev.net/topic/628778-compressing-vertex-attribute-normals/page__p__4965067#entry4965067"]http://www.gamedev.n...67#entry4965067[/url]

People don’t lose respect for you for being wrong. They lose respect for you for handling it poorly.


L. Spiro
[/quote]

True. I probably woke up on the wrong side of the bed today. You're right bro, if a game critic gave me poor game review - probably would aggravate me. So, yeah - I agree I have to react more proactive and professional about. I also have to realize critizism comes in a helping manner and less of a attack. I was being defensive and that's not good demonstration for others to see on this forum. Professionalism is the way to lead people in the right direction. Which, again I have to work upon if I am ever going to be taken seriously. So, yeah I agree with the quote above.
2

Share this post


Link to post
Share on other sites
And with that, we are far enough off topic.

@markrodgers11 - feel free to create a new thread if you still have questions.
0

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  
Followers 0