473,545 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what's the difference between "capacity" and "size" functions ofstring class?

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string cc(31, 'c');

string bb=cc.assign(3, 'dd');

cout << bb.capacity() << endl;

cout << bb.size() << endl;

getchar();

}

And "bb.capacit y()" returns "15" on my computer, which is wierd, where
did it come from?

Oct 23 '08 #1
7 3739
Luna Moon wrote:
#include "stdafx.h"
Why?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string cc(31, 'c');

string bb=cc.assign(3, 'dd');
'dd' isn't a character constant.
cout << bb.capacity() << endl;

cout << bb.size() << endl;

getchar();

}

And "bb.capacit y()" returns "15" on my computer, which is wierd, where
did it come from?
Capacity is the size of the string's buffer, size is the number of
characters in the buffer.

15 does appear to be incorrect.

--
Ian Collins
Oct 23 '08 #2
On Oct 23, 8:30*am, Luna Moon <lunamoonm...@g mail.comwrote:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
* string cc(31, 'c');

* string bb=cc.assign(3, 'dd');

* cout << bb.capacity() << endl;

* cout << bb.size() << endl;

* getchar();

}

And "bb.capacit y()" returns "15" on my computer, which is wierd, where
did it come from?
string::capacit y >= string::size(), while the latter one indicates
the
data size. (capacity - size) indicates that you can push_back such
amount
of charT without allocating the new space.

you can call "string::reserv e" to affect the "capacity"
while you can call "string::resize " to affect the "size"

--
Best Regards
Barry
Oct 23 '08 #3
On Oct 23, 2:47 am, Ian Collins <ian-n...@hotmail.co mwrote:
Luna Moon wrote:
#include "stdafx.h"
Why?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string cc(31, 'c');
string bb=cc.assign(3, 'dd');
'dd' isn't a character constant.
Yes it is:-). "A character literal is one or more characters
enclosed in single quotes[...]" (first sentence of section
2.1.3.2). But it probably won't do what he wants: "An ordinary
character literal that contains more than one c-char is a
multicharacter literal. A multicharacter literal has type int
and implementation-defined value." (A c-char is "any member of
the source character set except the single-quote ', backslash \,
or new-line character", an escape sequence or a universal
character name. Which, if I'm reading things write, means that
something like '\u20A0' only contains one c-char, and so should
have type char---even if the encoding is UTF-8:-).

As a general rule, character constants work well for single
characters in the basic execution set, but I'd avoid them for
anything else.
cout << bb.capacity() << endl;
cout << bb.size() << endl;
getchar();
}
And "bb.capacit y()" returns "15" on my computer, which is
wierd, where did it come from?
Capacity is the size of the string's buffer, size is the
number of characters in the buffer.
15 does appear to be incorrect.
Why? The only requirement is that capacity() >= size(). The
post condition of the copy constructor called to initialize bb
is that bb == str, where str is the other string. The ==
operator ignores capacity, and it is usual that the copy
constructor set the capacity to no more than is needed, modulo
any internal constraints. The g++ implementation of
std::string, for example, should more or less imposes a capacity
which is a multiple of sizeof( size_t ), because of alignment
issues (it doesn't---the actual implementation seems to ignore
all alignment issues), and implementations which use the small
string optimization impose a minimum capacity corresponding to
the size of the small string. Implementations like g++ which
use reference counting will, of course, also "copy" the
capacity. Thus, while all the standard requires in the above is
that capacity() >= 3 (which is the size); with g++, I'd expect
at least 31, because no copy is actually being done. With VC++,
15 (the size of its small string optimization), etc. With Sun
CC (which also uses reference counting), I get 31 with exact
program above, but if I insert a "char& r = cc[ 0 ];" (which
inhibits sharing) before the initialization of bb, I get a
capacity of 3.

All perfectly conform.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 23 '08 #4
On Oct 22, 7:01*pm, Barry <dhb2...@gmail. comwrote:
On Oct 23, 8:30*am, Luna Moon <lunamoonm...@g mail.comwrote:


#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
* string cc(31, 'c');
* string bb=cc.assign(3, 'dd');
* cout << bb.capacity() << endl;
* cout << bb.size() << endl;
* getchar();
}
And "bb.capacit y()" returns "15" on my computer, which is wierd, where
did it come from?

string::capacit y >= string::size(), while the latter one indicates
the
data size. (capacity - size) indicates that you can push_back such
amount
of charT without allocating the new space.

you can call "string::reserv e" to affect the "capacity"
while you can call "string::resize " to affect the "size"

--
Best Regards
Barry- Hide quoted text -

- Show quoted text -
What's the difference between "reserve" and "resize" then? I got more
confused now...

To me, they seem to be the same...

Also, in string class, there is a member called "max_size", what's
that? How is that different from "capacity" and "size"?
Oct 23 '08 #5
Luna Moon wrote:
>>
--
Best Regards
Barry- Hide quoted text -

- Show quoted text -
Please stop quoting signatures and google crap (the stuff above this line).
What's the difference between "reserve" and "resize" then? I got more
confused now...

To me, they seem to be the same...

Also, in string class, there is a member called "max_size", what's
that? How is that different from "capacity" and "size"?
This was done to death a few days ago in the thread "reserve of vector".

--
Ian Collins
Oct 23 '08 #6
On Oct 23, 1:30*pm, Luna Moon <lunamoonm...@g mail.comwrote:
* cout << bb.capacity() << endl;
* cout << bb.size() << endl;

And "bb.capacit y()" returns "15" on my computer, which is wierd, where
did it come from?
'size' is how many characters are in the string.
'capacity' is how much memory is allocated. (some
of it might not yet be in use).

For example if 'capacity' is 15 and 'size' is 6
then you can add up to 9 more characters to the
string, and the string object is guaranteed to
not request any more memory.

'reserve' increases the capacity, and 'resize'
changes the size. You would use capacity and
reserve if you were interested in controlling
when the string allocated memory.
Oct 24 '08 #7
Luna Moon wrote:
[...]
What's the difference between "reserve" and "resize" then? I got more
confused now...

To me, they seem to be the same...
'resize()'/'size()' change/tell the size of the string. Those are
what you need for your algorithms.
'reserve()'/'capacity()' are there for optimizations only. As long
as you don't run into performance problems with strings which grow
char by char, you can safely ignore them.
Also, in string class, there is a member called "max_size", what's
that? How is that different from "capacity" and "size"?
'max_size()' tells the maximum nunber of characters a string could,
in theory, have on the given platform. (It might be 2^16-1 on a 16bit
platform, as you cannot create indices greater than that.) Note that
this doesn't mean you can, practically, allocate such a string.

Schobi
Oct 24 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
3523
by: seesaw | last post by:
Is it right thing to always avoid using "new" to create objects? What if after starting the application, then decide which and how many objects to create? (Seems like under such situation is there no other choice but using "new")
8
2985
by: CoolPint | last post by:
Is there any way I can reduce the size of internal buffer to store characters by std::string? After having used a string object to store large strings, the object seems to retain the large buffer as I found out calling by capacity(). What if I do not need all that buffer space again want to reduce the size to a smaller one? I tried...
2
1527
by: Michael Olea | last post by:
Hiya. Frequently (as in at least twice now) I find that when I write some sort of container I want at least two out of three possible versions: o fixed max size known at compile time o fixed max size known at run time o max size is dynamic The question is how to name the variants consistently. My usual approach is
5
2970
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig <<<<<<<<<<<<<<CODE>>>>>>>>>>>>>>>> <html>
145
6170
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two programs: /*** a.c ***/
21
13790
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered by Sets. - ICollection cannot decide containment - IList promises indexability by the natural numbers, which is not achievable (since i hash...
5
2258
by: JNariss | last post by:
Hello, Within a form I created I have placed links that users can click on to view information. I am trying to write a javascript code so it opens in a sized window but I can't seem to figure this out. So far I have come up with the following: <script language="JavaScript" type="text/javascript"> var WindowObjectReference; // global...
12
4272
by: Avalon1178 | last post by:
Hi, I have an application that periodically uses a std::string variable which is assigned a VERY VERY large string (15000000+ bytes long). This application is essentially a daemon, and it polls a data set which can have a lot of information and it is concatenated in this single string variable object. When the daemon finishes its job, it...
36
3765
by: James Harris | last post by:
Initial issue: read in an arbitrary-length piece of text. Perceived issue: handle variable-length data The code below is a suggestion for implementing a variable length buffer that could be used to read text or handle arrays of arbitrary length. I don't have the expertise in C of many folks here so I feel like I'm offering a small furry...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7756
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3442
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1879
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
703
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.