|
|
![]() |
| Thread Tools | Search this Thread |
#1
| |||
| |||
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(); } did it come from? |
#2
| |||
| |||
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. |
#3
| |||
| |||
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" |
#4
| |||
| |||
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. |
#5
| |||
| |||
Re: Difference between capacity and size functions of string class What's the difference between "reserve" and "resize" then? I got more confused now... |
#6
| |||
| |||
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. |
![]() |
|
Tags: capacity, class, difference, functions, size, string |
Thread Tools | Search this Thread |
|
![]() | ||||
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 |