Kelas lokal
Kelas yang menjelaskan objek lokal yang merangkum informasi khusus budaya sebagai sekumpulan faset yang secara kolektif menentukan lingkungan lokal tertentu.
Sintaks
class locale;
Keterangan
Faset adalah penunjuk ke objek kelas yang berasal dari faset kelas yang memiliki objek publik formulir:
static locale::id id;
Anda dapat menentukan sekumpulan faset ini yang terbuka. Anda juga dapat membuat objek lokal yang menunjuk jumlah faset arbitrer.
Grup faset yang telah ditentukan sebelumnya mewakili kategori lokal yang dikelola secara tradisional di Pustaka Standard C oleh fungsi setlocale
.
Kategori collate
(LC_COLLATE) mencakup faset:
collate<char>
collate<wchar_t>
Kategori ctype
(LC_CTYPE) mencakup faset:
ctype<char>
ctype<wchar_t>
codecvt<char, char, mbstate_t>
codecvt<wchar_t, char, mbstate_t>
codecvt<char16_t, char, mbstate_t>
codecvt<char32_t, char, mbstate_t>
Kategori monetary
(LC_MONETARY) mencakup faset:
moneypunct<char, false>
moneypunct<wchar_t, false>
moneypunct<char, true>
moneypunct<wchar_t, true>
money_get<char, istreambuf_iterator<char>>
money_get<wchar_t, istreambuf_iterator<wchar_t>>
money_put<char, ostreambuf_iterator<char>>
money_put<wchar_t, ostreambuf_iterator<wchar_t>>
Kategori numeric
(LC_NUMERIC) mencakup faset:
num_get<char, istreambuf_iterator<char>>
num_get<wchar_t, istreambuf_iterator<wchar_t>>
num_put<char, ostreambuf_iterator<char>>
num_put<wchar_t, ostreambuf_iterator<wchar_t>>
numpunct<char>
numpunct<wchar_t>
Kategori time
(LC_TIME) mencakup faset:
time_get<char, istreambuf_iterator<char>>
time_get<wchar_t, istreambuf_iterator<wchar_t>>
time_put<char, ostreambuf_iterator<char>>
time_put<wchar_t, ostreambuf_iterator<wchar_t>>
Kategori messages
(LC_MESSAGES) mencakup faset:
messages<char>
messages<wchar_t>
(Kategori terakhir diperlukan oleh POSIX, tetapi bukan C Standard.)
Beberapa faset yang telah ditentukan sebelumnya ini digunakan oleh iostream
kelas, untuk mengontrol konversi nilai numerik ke dan dari urutan teks.
Objek lokal kelas juga menyimpan nama lokal sebagai objek string kelas. Menggunakan nama lokal yang tidak valid untuk membuat faset lokal atau objek lokal melemparkan objek kelas runtime_error. Nama lokal yang disimpan adalah "*"
jika objek lokal tidak dapat dipastikan bahwa lokal gaya C sesuai persis dengan yang diwakili oleh objek. Jika tidak, Anda dapat membuat lokal yang cocok dalam Pustaka C Standar, untuk beberapa objek locale_object
lokal , dengan memanggil setlocale(LC_ALL , locale_object.
nama().c_str())
.
Dalam implementasi ini, Anda juga dapat memanggil fungsi anggota statis:
static locale empty();
untuk membuat objek lokal yang tidak memiliki faset. Ini juga merupakan lokal transparan. Jika templat berfungsi has_facet dan use_facet tidak dapat menemukan faset yang diminta dalam lokal transparan, mereka berkonsultasi terlebih dahulu dengan lokal global dan kemudian, jika itu transparan, lokal klasik. Jadi, Anda dapat menulis:
cout.imbue(locale::empty());
Penyisipan cout
berikutnya akan dimediasi oleh status lokal global saat ini. Anda bahkan dapat menulis:
locale loc(locale::empty(),
locale::classic(),
locale::numeric);
cout.imbue(loc);
Aturan pemformatan numerik untuk penyisipan berikutnya tetap cout
sama seperti di lokal C, bahkan karena lokal global menyediakan aturan yang berubah untuk memasukkan tanggal dan jumlah moneter.
Konstruktor
Konstruktor | Deskripsi |
---|---|
lokal | Membuat lokal, atau salinan lokal, atau salinan lokal di mana faset atau kategori telah digantikan oleh faset atau kategori dari lokal lain. |
Typedefs
Nama jenis | Deskripsi |
---|---|
category | Jenis bilangan bulat yang menyediakan nilai bitmask untuk menunjukkan keluarga faset standar. |
Fungsi anggota
Fungsi anggota | Deskripsi |
---|---|
Menggabungkan | Menyisipkan faset dari lokal tertentu ke dalam lokal target. |
nama | Mengembalikan nama lokal tersimpan. |
Fungsi Statis
Nama | Deskripsi |
---|---|
Klasik | Fungsi anggota statis mengembalikan objek lokal yang mewakili lokal C klasik. |
global | Mereset lokal default untuk program. |
Operator
Operator | Deskripsi |
---|---|
operator= | Menetapkan lokal. |
operator!= | Menguji dua lokal untuk ketidaksamaan. |
operator( ) | Membandingkan dua basic_string objek. |
operator== | Menguji dua lokal untuk kesetaraan. |
Kelas
Kelas | Deskripsi |
---|---|
faset | Kelas yang berfungsi sebagai kelas dasar untuk semua faset lokal. |
id |
Kelas anggota menyediakan identifikasi faset unik yang digunakan sebagai indeks untuk mencari faset di lokal. |
Persyaratan
Header:<locale>
Namespace: std
locale::category
Jenis bilangan bulat yang menyediakan nilai bitmask untuk menunjukkan keluarga faset standar.
typedef int category;
static const int collate = LC_COLLATE;
static const int ctype = LC_CTYPE;
static const int monetary = LC_MONETARY;
static const int numeric = LC_NUMERIC;
static const int time = LC_TIME;
static const int messages = LC_MESSAGES;
static const int all = LC_ALL;
static const int none = 0;
Keterangan
Jenisnya adalah sinonim untuk int
jenis yang dapat mewakili sekelompok elemen berbeda dari jenis bitmask lokal ke lokal kelas atau dapat digunakan untuk mewakili salah satu kategori lokal C yang sesuai. Elemennya adalah:
collate
, sesuai dengan kategori C LC_COLLATEctype
, sesuai dengan kategori C LC_CTYPEmonetary
, sesuai dengan kategori C LC_MONETARYnumeric
, sesuai dengan kategori C LC_NUMERICtime
, sesuai dengan kategori C LC_TIMEmessages
, sesuai dengan kategori POSIX LC_MESSAGES
Dua nilai yang lebih berguna adalah:
none
, sesuai dengan tidak ada kategori Call
, sesuai dengan serikat C dari semua kategori LC_ALL
Anda dapat mewakili sekelompok kategori arbitrer dengan menggunakan bitwise-OR dengan konstanta ini, seperti dalam monetary | time
.
locale::classic
Fungsi anggota statis mengembalikan objek lokal yang mewakili lokal C klasik.
static const locale& classic();
Tampilkan Nilai
Referensi ke lokal C.
Keterangan
Lokal C klasik adalah lokal ASCII Inggris A.S. dalam pustaka Standard C. Ini adalah lokal yang digunakan secara implisit dalam program yang tidak diinternasialisasikan.
Contoh
// locale_classic.cpp
// compile with: /EHsc
#include <iostream>
#include <string>
#include <locale>
using namespace std;
int main( )
{
locale loc1( "german" );
locale loc2 = locale::global( loc1 );
cout << "The name of the previous locale is: " << loc2.name( )
<< "." << endl;
cout << "The name of the current locale is: " << loc1.name( )
<< "." << endl;
if (loc2 == locale::classic( ) )
cout << "The previous locale was classic." << endl;
else
cout << "The previous locale was not classic." << endl;
if (loc1 == locale::classic( ) )
cout << "The current locale is classic." << endl;
else
cout << "The current locale is not classic." << endl;
}
The name of the previous locale is: C.
The name of the current locale is: German_Germany.1252.
The previous locale was classic.
The current locale is not classic.
locale::combine
Menyisipkan faset dari lokal tertentu ke dalam lokal target.
template <class Facet>
locale combine(const locale& source_locale) const;
Parameter
source_locale
Lokal yang berisi faset yang akan dimasukkan ke dalam lokal target.
Tampilkan Nilai
Fungsi anggota mengembalikan objek lokal yang menggantikan atau menambahkan ke *this
faset Facet
yang tercantum dalam source_locale.
Contoh
// locale_combine.cpp
// compile with: /EHsc
#include <locale>
#include <iostream>
#include <tchar.h>
using namespace std;
int main() {
locale loc ( "German_germany" );
_TCHAR * s1 = _T("Das ist wei\x00dfzz."); // \x00df is the German sharp-s; it comes before z in the German alphabet
_TCHAR * s2 = _T("Das ist weizzz.");
int result1 = use_facet<collate<_TCHAR> > ( loc ).
compare (s1, &s1[_tcslen( s1 )-1 ], s2, &s2[_tcslen( s2 )-1 ] );
cout << isalpha (_T ( '\x00df' ), loc ) << result1 << endl;
locale loc2 ( "C" );
int result2 = use_facet<collate<_TCHAR> > ( loc2 ).
compare (s1, &s1[_tcslen( s1 )-1 ], s2, &s2[_tcslen( s2 )-1 ] );
cout << isalpha (_T ( '\x00df' ), loc2 ) << result2 << endl;
locale loc3 = loc2.combine<collate<_TCHAR> > (loc);
int result3 = use_facet<collate<_TCHAR> > ( loc3 ).
compare (s1, &s1[_tcslen( s1 )-1 ], s2, &s2[_tcslen( s2 )-1 ] );
cout << isalpha (_T ( '\x00df' ), loc3 ) << result3 << endl;
}
Kelas faset
Kelas yang berfungsi sebagai kelas dasar untuk semua faset lokal.
class facet {
protected:
explicit facet(size_t references = 0);
virtual ~facet();
private:
facet(const facet&) // not defined
void operator=(const facet&) // not defined
};
Keterangan
Anda tidak dapat menyalin atau menetapkan objek kelas facet
. Anda dapat membangun dan menghancurkan objek yang berasal dari kelas locale::facet
tetapi bukan objek kelas dasar yang tepat. Biasanya, Anda membuat objek _Myfac
yang berasal dari facet
saat Anda membuat locale
, seperti dalam locale loc(locale::classic(), new _Myfac);
Dalam kasus seperti itu, konstruktor untuk kelas facet
dasar harus memiliki argumen referensi nol. Ketika objek tidak lagi diperlukan, objek akan dihapus. Berikan argumen referensi bukan nol hanya dalam kasus langka di mana Anda bertanggung jawab atas masa pakai objek.
locale::global
Mereset lokal default untuk program. Panggilan ini memengaruhi lokal global untuk C dan C++.
static locale global(const locale& new_default_locale);
Parameter
new_default_locale
Lokal yang akan digunakan sebagai lokal default oleh program.
Tampilkan Nilai
Lokal sebelumnya sebelum lokal default direset.
Keterangan
Pada startup program, lokal global sama dengan lokal klasik. Fungsi ini global()
memanggil setlocale( LC_ALL, loc.name. c_str())
untuk membuat lokal yang cocok di pustaka Standard C.
Contoh
// locale_global.cpp
// compile by using: /EHsc
#include <locale>
#include <iostream>
#include <tchar.h>
using namespace std;
int main( )
{
locale loc ( "German_germany" );
locale loc1;
cout << "The initial locale is: " << loc1.name( ) << endl;
locale loc2 = locale::global ( loc );
locale loc3;
cout << "The current locale is: " << loc3.name( ) << endl;
cout << "The previous locale was: " << loc2.name( ) << endl;
}
The initial locale is: C
The current locale is: German_Germany.1252
The previous locale was: C
Kelas id
Kelas anggota menyediakan identifikasi faset unik yang digunakan sebagai indeks untuk mencari faset di lokal.
class id
{
protected: id();
private: id(const id&)
void operator=(const id&) // not defined
};
Keterangan
Kelas anggota menjelaskan objek anggota statis yang diperlukan oleh setiap faset lokal unik. Anda tidak dapat menyalin atau menetapkan objek kelas id
.
locale::locale
Membuat lokal, atau salinan lokal, atau salinan lokal di mana faset atau kategori telah digantikan oleh faset atau kategori dari lokal lain. Juga termasuk destruktor.
locale();
explicit locale(const char* locale_name, category new_category = all);
explicit locale(const string& locale_name);
locale(const locale& from_locale);
locale(const locale& from_locale, const locale& Other, category new_category);
locale(const locale& from_locale, const char* locale_name, category new_category);
template <class Facet>
locale(const locale& from_locale, const Facet* new_facet);
~locale();
Parameter
locale_name
Nama lokal.
from_locale
Lokal yang akan disalin dalam membangun lokal baru.
Lainnya
Lokal tempat memilih kategori.
new_category
Kategori yang akan diganti ke dalam lokal yang dibangun.
new_facet
Faset yang akan diganti menjadi lokal yang dibangun.
Keterangan
Konstruktor pertama menginisialisasi objek agar sesuai dengan lokal global. Konstruktor kedua dan ketiga menginisialisasi semua kategori lokal agar memiliki perilaku yang konsisten dengan nama lokal locale_name. Konstruktor yang tersisa menyalin from_locale, dengan pengecualian yang dicatat:
locale(const locale& from_locale, const locale& Other, category new_category);
menggantikan dari Faset lainnya yang sesuai dengan kategori C yang C & new_category-nya bukan nol.
locale(const locale& from_locale, const char* locale_name, category new_category);
locale(const locale& from_locale, const string& locale_name, category new_category);
menggantikan dari locale(locale_name, all)
faset yang sesuai dengan kategori replace_category yang replace_category & new_category
bukan nol.
template<class Facet> locale(const locale& from_locale, Facet* new_facet);
mengganti dalam (atau menambahkan ke) from_locale faset new_facet, jika new_facet bukan penunjuk null.
Jika nama lokal locale_name adalah penunjuk null atau tidak valid, fungsi akan melemparkan runtime_error.
Contoh
// locale_locale.cpp
// compile with: /EHsc
#include <locale>
#include <iostream>
#include <tchar.h>
using namespace std;
int main( ) {
// Second constructor
locale loc ( "German_germany" );
_TCHAR * s1 = _T("Das ist wei\x00dfzz."); // \x00df is the German sharp-s, it comes before z in the German alphabet
_TCHAR * s2 = _T("Das ist weizzz.");
int result1 = use_facet<collate<_TCHAR> > ( loc ).
compare (s1, &s1[_tcslen( s1 )-1 ], s2, &s2[_tcslen( s2 )-1 ] );
cout << isalpha (_T ( '\x00df' ), loc ) << result1 << endl;
// The first (default) constructor
locale loc2;
int result2 = use_facet<collate<_TCHAR> > ( loc2 ).
compare (s1, &s1[_tcslen( s1 )-1 ], s2, &s2[_tcslen( s2 )-1 ] );
cout << isalpha (_T ( '\x00df' ), loc2 ) << result2 << endl;
// Third constructor
locale loc3 (loc2,loc, _M_COLLATE );
int result3 = use_facet<collate<_TCHAR> > ( loc3 ).
compare (s1, &s1[_tcslen( s1 )-1 ], s2, &s2[_tcslen( s2 )-1 ] );
cout << isalpha (_T ( '\x00df' ), loc3 ) << result3 << endl;
// Fourth constructor
locale loc4 (loc2, "German_Germany", _M_COLLATE );
int result4 = use_facet<collate<_TCHAR> > ( loc4 ).
compare (s1, &s1[_tcslen( s1 )-1 ], s2, &s2[_tcslen( s2 )-1 ] );
cout << isalpha (_T ( '\x00df' ), loc4 ) << result4 << endl;
}
locale::name
Mengembalikan nama lokal tersimpan.
string name() const;
Tampilkan Nilai
String yang memberikan nama lokal.
Contoh
// locale_name.cpp
// compile with: /EHsc
#include <iostream>
#include <string>
#include <locale>
using namespace std;
int main( )
{
locale loc1( "german" );
locale loc2 = locale::global( loc1 );
cout << "The name of the previous locale is: "
<< loc2.name( ) << "." << endl;
cout << "The name of the current locale is: "
<< loc1.name( ) << "." << endl;
}
The name of the previous locale is: C.
The name of the current locale is: German_Germany.1252.
locale::operator=
Menetapkan lokal.
const locale& operator=(const locale& other) noexcept;
locale::operator!=
Menguji dua lokal untuk ketidaksamaan.
bool operator!=(const locale& right) const;
Parameter
right
Salah satu lokal yang akan diuji ketidaksamaannya.
Tampilkan Nilai
Nilai Boolean adalah true
jika lokal tidak menyalin lokal yang sama. Ini false
jika lokal adalah salinan dari lokal yang sama.
Keterangan
Dua lokal sama jika lokalnya sama, jika satu adalah salinan yang lain, atau jika mereka memiliki nama yang identik.
Contoh
// locale_op_ne.cpp
// compile with: /EHsc
#include <iostream>
#include <string>
#include <locale>
using namespace std;
int main( )
{
locale loc1( "German_Germany" );
locale loc2( "German_Germany" );
locale loc3( "English" );
if ( loc1 != loc2 )
cout << "locales loc1 (" << loc1.name( )
<< ") and\n loc2 (" << loc2.name( ) << ") are not equal." << endl;
else
cout << "locales loc1 (" << loc1.name( )
<< ") and\n loc2 (" << loc2.name( ) << ") are equal." << endl;
if ( loc1 != loc3 )
cout << "locales loc1 (" << loc1.name( )
<< ") and\n loc3 (" << loc3.name( ) << ") are not equal." << endl;
else
cout << "locales loc1 (" << loc1.name( )
<< ") and\n loc3 (" << loc3.name( ) << ") are equal." << endl;
}
locales loc1 (German_Germany.1252) and
loc2 (German_Germany.1252) are equal.
locales loc1 (German_Germany.1252) and
loc3 (English_United States.1252) are not equal.
locale::operator()
Membandingkan dua basic_string
objek sesuai dengan aturan perbandingan leksikografis yang ditentukan oleh faset lokal std::collate<charT>
ini.
template <class CharType, class Traits, class Allocator>
bool operator()(
const basic_string<CharType, Traits, Allocator>& left,
const basic_string<CharType, Traits, Allocator>& right) const;
Parameter
left
String pertama yang akan dibandingkan.
right
String kedua yang akan dibandingkan.
Tampilkan Nilai
true
jika kiri secara leksikografis kurang dari kanan, jika tidakfalse
.
Keterangan
Fungsi anggota secara efektif menjalankan:
const collate<CharType>& fac = use_fac<collate<CharType>>(*this);
return (fac.compare(left.begin(), left.end(), right.begin(), right.end()) < 0);
Ini berarti Anda dapat menggunakan objek lokal sebagai objek fungsi.
Contoh
// locale_op_compare.cpp
// compile with: /EHsc
#include <iostream>
#include <string>
#include <locale>
int main( )
{
using namespace std;
const wchar_t *sa = L"ztesting";
const wchar_t *sb = L"\0x00DFtesting";
basic_string<wchar_t> a( sa );
basic_string<wchar_t> b( sb );
locale loc( "German_Germany" );
cout << loc( a,b ) << endl;
const collate<wchar_t>& fac = use_facet<collate<wchar_t> >( loc );
cout << ( fac.compare( sa, sa + a.length( ),
sb, sb + b.length( ) ) < 0) << endl;
}
0
0
locale::operator==
Menguji dua lokal untuk kesetaraan.
bool operator==(const locale& right) const;
Parameter
right
Salah satu lokal yang akan diuji kesetaraannya.
Tampilkan Nilai
Nilai Boolean yang jika true
lokal adalah salinan dari lokal yang sama. Ini false
jika lokal tidak menyalin lokal yang sama.
Keterangan
Dua lokal sama jika lokalnya sama, jika satu adalah salinan yang lain, atau jika mereka memiliki nama yang identik.
Contoh
// locale_op_eq.cpp
// compile with: /EHsc
#include <iostream>
#include <string>
#include <locale>
using namespace std;
int main( )
{
locale loc1( "German_Germany" );
locale loc2( "German_Germany" );
locale loc3( "English" );
if ( loc1 == loc2 )
cout << "locales loc1 (" << loc1.name( )
<< ")\n and loc2 (" << loc2.name( ) << ") are equal."
<< endl;
else
cout << "locales loc1 (" << loc1.name( )
<< ")\n and loc2 (" << loc2.name( ) << ") are not equal."
<< endl;
if ( loc1 == loc3 )
cout << "locales loc1 (" << loc1.name( )
<< ")\n and loc3 (" << loc3.name( ) << ") are equal."
<< endl;
else
cout << "locales loc1 (" << loc1.name( )
<< ")\n and loc3 (" << loc3.name( ) << ") are not equal."
<< endl;
}
locales loc1 (German_Germany.1252)
and loc2 (German_Germany.1252) are equal.
locales loc1 (German_Germany.1252)
and loc3 (English_United States.1252) are not equal.
Baca juga
<lokal>
Halaman Kode
Nama Lokal, Bahasa, dan String Negara/Wilayah
Keamanan utas di Pustaka Standar C++
Saran dan Komentar
https://aka.ms/ContentUserFeedback.
Segera hadir: Sepanjang tahun 2024 kami akan menghentikan penggunaan GitHub Issues sebagai mekanisme umpan balik untuk konten dan menggantinya dengan sistem umpan balik baru. Untuk mengetahui informasi selengkapnya, lihat:Kirim dan lihat umpan balik untuk