Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


string::size_type


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • This topic is locked This topic is locked
19 replies to this topic

#1 markrodgers11   Members   -  Reputation: 149

Like
0Likes
Like

Posted 09 October 2012 - 11:20 PM

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

#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;
}


Sponsor:

#2 Endurion   Crossbones+   -  Reputation: 3681

Like
3Likes
Like

Posted 09 October 2012 - 11:52 PM

Your question and code do not match up. You never write out something with numbers.

string::size_type is not a function, it's a typedef for size_t usually.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#3 markrodgers11   Members   -  Reputation: 149

Like
0Likes
Like

Posted 10 October 2012 - 09:56 AM

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. :/

#4 Brother Bob   Moderators   -  Reputation: 8570

Like
1Likes
Like

Posted 10 October 2012 - 10:10 AM

As Endurion said, it is just a typedef. It is the type used by the std::string class to represent a size. For example, the size() member function returns the size of the string, and the type of the value returned is std::string::size_type.

std::string foo = "Hello, world!";
std::string::size_type length = foo.size();


#5 swiftcoder   Senior Moderators   -  Reputation: 10361

Like
0Likes
Like

Posted 10 October 2012 - 10:16 AM

I don't really understand what ::size_type is/does.

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.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#6 SIC Games   Members   -  Reputation: 617

Like
-4Likes
Like

Posted 10 October 2012 - 10:51 PM

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.


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


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.

Game Engine's WIP Videos - http://www.youtube.com/sicgames88


#7 markrodgers11   Members   -  Reputation: 149

Like
0Likes
Like

Posted 10 October 2012 - 11:27 PM

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

#8 Cornstalks   Crossbones+   -  Reputation: 6991

Like
3Likes
Like

Posted 10 October 2012 - 11:36 PM

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.


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


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.

I'm gonna nitpick this 'cause otherwise it might confuse some beginners. First, there is no SIZE_T. Perhaps were you referring to std::size_t/::size_t (remember C++ is case sensitive)? Second, even if SIZE_T existed, it clearly doesn't "return" anything. It's just the name for a datatype.

std::size_t (and ::size_t) is the type of the result of the sizeof operator. It doesn't have a specific size required by the standard, but it is guaranteed to be able to store and represent the size of any data structure that fits in the machine (which is why on a 32-bit machine, it's usually a 32-bit unsigned integer, and on a 64-bit machine, a 64-bit unsigned integer), which is why it's commonly used as the type of an index into an array/data structure.

std::string::size_type is what std::string uses to represent sizes. As swiftcoder says, it's usually just the same thing as std::size_t. You'll see a lot of containers in the standard library have a size_type defined. One reason this is done is so that if you're writing a templated function that works on some container, you can just do T::size_type (where T is the template parameter) and be sure you're using the right data type. After all, the C++ standard doesn't strictly require them all to be exactly the same type, so this allows you to be really careful.

Edited by Cornstalks, 10 October 2012 - 11:37 PM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#9 L. Spiro   Crossbones+   -  Reputation: 14198

Like
2Likes
Like

Posted 10 October 2012 - 11:40 PM

As you see the SIZE_T is a typdef that stores the integer of length.

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.


I'm gonna nitpick this 'cause otherwise it might confuse some beginners. First, there is no SIZE_T.

SIZE_T is a Windows data type.


L. Spiro

Edited by L. Spiro, 10 October 2012 - 11:45 PM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#10 EddieV223   Members   -  Reputation: 1407

Like
0Likes
Like

Posted 11 October 2012 - 01:15 AM

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, 11 October 2012 - 01:18 AM.

If this post or signature was helpful and/or constructive please give rep.

 

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

 

// Easy to learn 2D Game Library c++

SFML2.1 Download http://www.sfml-dev.org/download.php

SFML2.1 Tutorials http://www.sfml-dev.org/tutorials/2.1/

 

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

 


#11 EddieV223   Members   -  Reputation: 1407

Like
0Likes
Like

Posted 11 October 2012 - 01:23 AM

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.


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


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.


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();

If this post or signature was helpful and/or constructive please give rep.

 

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

 

// Easy to learn 2D Game Library c++

SFML2.1 Download http://www.sfml-dev.org/download.php

SFML2.1 Tutorials http://www.sfml-dev.org/tutorials/2.1/

 

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

 


#12 SIC Games   Members   -  Reputation: 617

Like
0Likes
Like

Posted 11 October 2012 - 08:08 AM

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.


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


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.


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.

Game Engine's WIP Videos - http://www.youtube.com/sicgames88


#13 SIC Games   Members   -  Reputation: 617

Like
-2Likes
Like

Posted 11 October 2012 - 08:12 AM


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.


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


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.


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();


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.

Game Engine's WIP Videos - http://www.youtube.com/sicgames88


#14 L. Spiro   Crossbones+   -  Reputation: 14198

Like
2Likes
Like

Posted 11 October 2012 - 09:01 AM

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

“string” is a structure, so sizeof() returns the size of that structure, not the size of the string it manages.
If it was instead:
const char szString[] = "Hello fellow citizens.";
Then sizeof() would return the number of characters, including the terminating NULL.


screw that idea.

I supported you on SIZE_T but I feel less sorry for you after you posted this.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#15 swiftcoder   Senior Moderators   -  Reputation: 10361

Like
1Likes
Like

Posted 11 October 2012 - 09:01 AM

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.

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.

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

    assert( sizeof(A) == sizeof(B) );
}

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#16 SIC Games   Members   -  Reputation: 617

Like
-3Likes
Like

Posted 11 October 2012 - 09:12 AM


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

“string” is a structure, so sizeof() returns the size of that structure, not the size of the string it manages.
If it was instead:
const char szString[] = "Hello fellow citizens.";
Then sizeof() would return the number of characters, including the terminating NULL.


screw that idea.

I supported you on SIZE_T but I feel less sorry for you after you posted this.


L. Spiro


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.

Game Engine's WIP Videos - http://www.youtube.com/sicgames88


#17 SIC Games   Members   -  Reputation: 617

Like
-3Likes
Like

Posted 11 October 2012 - 09:13 AM


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.

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.

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

	assert( sizeof(A) == sizeof(B) );
}


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

Game Engine's WIP Videos - http://www.youtube.com/sicgames88


#18 L. Spiro   Crossbones+   -  Reputation: 14198

Like
2Likes
Like

Posted 11 October 2012 - 09:21 AM

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#19 SIC Games   Members   -  Reputation: 617

Like
2Likes
Like

Posted 11 October 2012 - 09:27 AM

I have been wrong too; it is all about how you handle it.
http://www.gamedev.n...67#entry4965067

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


L. Spiro


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.

Game Engine's WIP Videos - http://www.youtube.com/sicgames88


#20 swiftcoder   Senior Moderators   -  Reputation: 10361

Like
0Likes
Like

Posted 11 October 2012 - 09:32 AM

And with that, we are far enough off topic.

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

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS