string::size_type

Started by
18 comments, last by swiftcoder 11 years, 7 months ago
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;
}
Advertisement
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>

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. :/
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();

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. Ex-BigTech Software Engineer. Future farmer. [https://trist.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.
Game Engine's WIP Videos - http://www.youtube.com/sicgames88
SIC Games @ GitHub - https://github.com/SICGames?tab=repositories
Simple D2D1 Font Wrapper for D3D11 - https://github.com/SICGames/D2DFontX
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

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 [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.
[size=2][ 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 ]

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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

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.2 Download http://www.sfml-dev.org/download.php

SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/

// Excellent 2d physics library Box2D

http://box2d.org/about/

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

This topic is closed to new replies.

Advertisement