Results 1 to 6 of 6

Thread: Difference between capacity and size functions of string class

  1. #1
    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?

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

  3. #3
    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"

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

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

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

Similar Threads

  1. What is the difference between Local class and global class in C++?
    By Dëfrim in forum Software Development
    Replies: 4
    Last Post: 03-01-2011, 10:44 PM
  2. Debug class and Trace class Difference
    By Amaresh in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 10:00 AM
  3. VB : How can I make the use String functions
    By Fielder in forum Software Development
    Replies: 3
    Last Post: 19-01-2010, 01:10 PM
  4. Difference among concrete class and abstract class
    By Roxy_jacob in forum Software Development
    Replies: 4
    Last Post: 07-12-2009, 01:22 PM
  5. How to Manipulate String using PHP String Functions
    By ComPaCt in forum Software Development
    Replies: 3
    Last Post: 21-09-2009, 09:07 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,691,521.77543 seconds with 17 queries