Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
complex
exception
functional
iterator
limits
locale
memory
new
numeric
stdexcept
typeinfo
utility
valarray
locale
has_facet
isalnum
isalpha
iscntrl
isdigit
isgraph
islower
isprint
ispunct
isspace
isupper
isxdigit
locale
tolower
toupper
use_facet
standard facets:
codecvt
codecvt_base
codecvt_byname
collate
collate_byname
ctype
ctype_base
ctype_byname
messages
messages_base
messages_byname
moneypunct
moneypunct_byname
money_base
money_get
money_put
numpunct
numpunct_byname
num_get
num_put
time_base
time_get
time_get_byname
time_put
time_put_byname
codecvt
codecvt::codecvt
public member functions:
codecvt::always_noconv
codecvt::encoding
codecvt::in
codecvt::length
codecvt::max_length
codecvt::out
codecvt::unshift
public member types:
codecvt::extern_type
codecvt::intern_type
codecvt::result
codecvt::state_type
protected members:
codecvt::do_always_noconv
codecvt::do_encoding
codecvt::do_in
codecvt::do_length
codecvt::do_max_length
codecvt::do_out
codecvt::do_unshift
codecvt::~codecvt


codecvt::length

public member function
int length ( stateT& state, const externT* from, const externT* from_end, size_t max ) const;

Return length of translated sequence

Returns the amount of external characters in the range [from,from_end) that could be translated into at maximum max internal characters, as if done with codecvt::in.

state is also updated as if codecvt::in was called for a buffer of max internal characters.

During its operation, this function simply calls the virtual protected member codecvt::do_length, which is the member function in charge of performing the actions described above.

Parameters

state
State object to keep track of the state of a multibyte character conversion. Typically, this is an object of type mbstate_t.
stateT is the state type (i.e., the third template parameter of codecvt).
from, from_end
Pointer to the initial and final characters of the source sequence. The range used is [from,from_end), which contains all the characters between from and from_end, including the character pointed by from but not the character pointed by from_end.
externT is the external character type (i.e., the second template parameter of codecvt).
max
Maximum length of the translated sequence (in terms of internal characters).
size_t is an integral type.

Return value

The length of the sequence of characters if translated to internal characters.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// codecvt::length example
#include <iostream>
#include <locale>
using namespace std;
int main ()
{
  locale loc;
  mbstate_t mystate;
  char origin[] = "abcdefghijklmnopqrstuvwxyz";
  const char * pc;
  wchar_t * pwc;
  const codecvt<wchar_t,char,mbstate_t>& myfacet = 
    use_facet<codecvt<wchar_t,char,mbstate_t> >(loc);
  // calculate length for 10 wchar_t's:
  int length = myfacet.length (mystate, origin, origin+sizeof(origin), 10);
  wchar_t * dest = new wchar_t[length+1];
  myfacet.in (mystate, origin, origin+sizeof(origin), pc, dest, dest+length, pwc);
  dest[length]=0;  // Terminating null-character
  cout << "A string of 10 wchar_t elements: ";
  wcout << dest << endl;
  return 0;
}


Output:

A string of 10 wchar_t elements: abcdefghij

See also