Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

The C++ standard library

The C++ standard library

In the C++ standard library,locale class provides a mechanism to control the program's localization behavior, especially formatting and conversion operations related to language and culture.locale class is defined in the #include header file.

The C++ standard library's locale module provides a way for programs to handle text data according to the user's locale, such as formatting numbers, dates, and times, as well as comparing and sorting strings. This makes it easier to write internationalized applications.

Syntax

The following is how to use locale Basic syntax of the class:

#include 

#include 



int main() {

    // Create a default locale object

    std::locale loc;



    // Use locale object

    std::cout.imbue(loc); // 设置 cout 的 locale



    // Display the name of the current locale

    std::cout << "Current locale: " << loc.name() << std::endl;



    // 更多操作...

    return 0;

}

Example

1. Basic usage

Here is a simple example showing how to use locale to format numbers:

Example

#include

#include

int

main

(

)

{

    std

::

locale

loc

(

"en_US.UTF-8"

)

;

// Set to U.S. English

    std

::

cout

.

imbue

(

loc

)

;

// Set cout's locale

   

double

number

=

1234567.89

;

    std

::

cout

<<

"Formatted number: "

<<

number

<<

std

::

endl

;

   

return

0

;

}

Output result:

Formatted number: 1,234,567.89

2. Compare strings

Use locale can compare strings according to the rules of a specific locale:

Example

#include

#include

#include

int

main

(

)

{

    std

::

locale

loc

(

"en_US.UTF-8"

)

;

    std

::

string

str1

=

"apple"

;

    std

::

string

str2

=

"banana"

;

   

if

(

std

::

use_facet

<

std

::

collate

<

char

(

loc

)

.

compare

(

str1.

c_str

(

)

, str1.

c_str

(

)

str1.

size

(

)

,

                                                       str2.

c_str

(

)

, str2.

c_str

(

)

str2.

size

(

)

)

<

0

)

{

std

::

cout

<<

str1

<<

" comes before "

<<

str2

<<

std

::

endl

;

   

}

else

{

        std

::

cout

<<

str1

<<

" comes after "

<<

str2

<<

std

::

endl

;

   

}

   

return

0

;

}

Output result:

apple comes before banana

3. Date and time formatting

locale can also be used to format dates and times:

Example

#include

#include

#include

int

main

(

)

{

    std

::

locale

loc

(

"en_US.UTF-8"

)

;

    std

::

cout

.

imbue

(

loc

)

;

    std

::

time_t

now

=

std

::

time

(

nullptr

)

;

    std

::

tm

timeinfo

=

std

::

localtime

(

&

now

)

;

   

char

buffer

[

100

]

;

    std

::

strftime

(

buffer,

sizeof

(

buffer

)

,

"%A, %B %d, %Y"

, timeinfo

)

;

    std

::

cout

<<

"Current date: "

<<

buffer

<<

std

::

endl

;

   

return

0

;

}

Output result(Example):

Current date: Monday, March 14, 2023

locale class is a powerful tool in the C++ standard library; it allows developers to write applications that adapt to different locales.