• Advertisement
Sign in to follow this  

string::size_type

This topic is 1927 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

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]

Share this post


Link to post
Share on other sites
Advertisement
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. :/

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]

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.

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.

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

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]
I'm gonna nitpick this 'cause otherwise it might confuse some beginners. First, there is no [font=courier new,courier,monospace]SIZE_T[/font]. Perhaps were you referring to [font=courier new,courier,monospace]std::size_t[font=arial,helvetica,sans-serif]/[/font]::size_t[/font] (remember C++ is case sensitive)? Second, even if [font=courier new,courier,monospace]SIZE_T[/font] existed, it clearly doesn't "return" anything. It's just the name for a datatype.

[font=courier new,courier,monospace]std::size_t[/font] (and [font=courier new,courier,monospace]::size_t[/font]) is the type of the result of the [font=courier new,courier,monospace]sizeof[/font] 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.

[font=courier new,courier,monospace]std::string::size_type[/font] is what [font=courier new,courier,monospace]std::string[/font] uses to represent sizes. As swiftcoder says, it's usually just the same thing as [font=courier new,courier,monospace]std::size_t[/font]. You'll see a lot of containers in the standard library have a [font=courier new,courier,monospace]size_type[/font] 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 [font=courier new,courier,monospace]T::size_type[/font] (where [font=courier new,courier,monospace]T[/font] 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

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

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

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

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.

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.

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

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]

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.

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.

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

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.

Share this post


Link to post
Share on other sites

This topic is 1927 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.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement