Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::basic_...ng::capacity(3) C++ Standard Libarystd::basic_...ng::capacity(3)

NAME
       std::basic_string::capacity - std::basic_string::capacity

Synopsis
	  size_type capacity() const;			  (until C++11)
	  size_type capacity() const noexcept;		  (since C++11)
							  (until C++20)
	  constexpr size_type capacity() const noexcept;  (since C++20)

	  Returns the number of	characters that	the string has currently allo-
       cated space for.

Parameters
	  (none)

Return value
	  Capacity of the currently allocated storage, i.e. the	storage	avail-
       able for	storing
	  elements.

Complexity
	  Constant

Notes
	  Memory  locations  obtained from the allocator but not available for
       storing any
	  element are not counted in the allocated storage. Note that the null
       terminator is
	  not an element of the	std::basic_string.

Example
       // Run this code

	#include <iostream>
	#include <iomanip>
	#include <string>

	void show_capacity(std::string const& s)
	{
	    std::cout << std::quoted(s)	<< " has capacity " << s.capacity() <<
       ".\n";
	}

	int main()
	{
	    std::string	s{"Exemplar"};
	    show_capacity(s);

	    s += " is an example string.";
	    show_capacity(s);

	    s.clear();
	    show_capacity(s);

	    std::cout << "\nDemonstrate	the capacity's growth policy."
			 "\nSize:  Capacity:  Ratio:\n"	<< std::left;

	    std::string	g;
	    auto old_cap {g.capacity()};

	    for(int mark{}; mark != 5; ++mark)
	    {
		while (old_cap == g.capacity())	g.push_back('.');

		std::cout << std::setw(	7) << g.size()
			  << std::setw(11) << g.capacity()
			  <<   std::setw(10)   <<    g.capacity()    /	  sta-
       tic_cast<float>(old_cap)	<< '\n';

		old_cap	= g.capacity();
	    }
	}

Possible output:
	"Exemplar" has capacity	15.
	"Exemplar is an	example	string." has capacity 30.
	"" has capacity	30.

	Demonstrate the	capacity's growth policy.
	Size:  Capacity:  Ratio:
	16     30	  2
	31     60	  2
	61     120	  2
	121    240	  2
	241    480	  2

See also
	  size	  returns the number of	characters
	  length  (public member function)
	  reserve reserves storage
		  (public member function)

http://cppreference.com		  2022.07.31	 std::basic_...ng::capacity(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::basic_string::capacity&sektion=3&manpath=FreeBSD+Ports+15.0>

home | help