Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-10-2008
Member
 
Join Date: May 2008
Posts: 10
Difference between capacity and size functions of string class

Difference between "capacity" and "size" functions ofstring class?

Code:
#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.capacity()" returns "15" on my computer, which is wierd, where
did it come from?
Reply With Quote
  #2  
Old 25-10-2008
Member
 
Join Date: Oct 2008
Posts: 20
Re: Difference between capacity and size functions of string class

'dd' isn't a character constant.

Capacity is the size of the string's buffer, size is the number of characters in the buffer.

15 does appear to be incorrect.
Reply With Quote
  #3  
Old 25-10-2008
Member
 
Join Date: Mar 2008
Posts: 52
Re: Difference between capacity and size functions of string class

string::capacity >= 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::reserve" to affect the "capacity" while you can call "string::resize" to affect the "size"
Reply With Quote
  #4  
Old 25-10-2008
Member
 
Join Date: May 2008
Posts: 72
Re: Difference between capacity and size functions of string class

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.
Reply With Quote
  #5  
Old 25-10-2008
Member
 
Join Date: May 2008
Posts: 10
Re: Difference between capacity and size functions of string class

What's the difference between "reserve" and "resize" then? I got more confused now...
Reply With Quote
  #6  
Old 25-10-2008
Member
 
Join Date: Oct 2008
Posts: 22
Re: Difference between capacity and size functions of string class

'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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Difference between capacity and size functions of string class"
Thread Thread Starter Forum Replies Last Post
What is the difference between Local class and global class in C++? Dėfrim Software Development 4 03-01-2011 10:44 PM
Debug class and Trace class Difference Amaresh Software Development 5 22-01-2010 10:00 AM
VB : How can I make the use String functions Fielder Software Development 3 19-01-2010 01:10 PM
Difference among concrete class and abstract class Roxy_jacob Software Development 4 07-12-2009 01:22 PM
How to Manipulate String using PHP String Functions ComPaCt Software Development 3 21-09-2009 09:07 AM


All times are GMT +5.5. The time now is 04:50 PM.