Difference between revisions of "cpp/locale/isprint"
From cppreference.com
m (Text replace - "/sidebar" to "/navbar") |
Andreas Krug (Talk | contribs) m (+is, fmt) |
||
(5 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/title | isprint{{small|(std::locale)}}}} | + | {{cpp/title|isprint{{small|(std::locale)}}}} |
{{cpp/locale/navbar}} | {{cpp/locale/navbar}} | ||
− | + | {{ddcl|header=locale| | |
− | {{ddcl | header=locale | | + | template< class CharT > |
− | template< class | + | bool isprint( CharT ch, const locale& loc ); |
− | bool isprint( | + | |
}} | }} | ||
− | Checks if the given character classified as a printable character (including space) by the given locale's {{ | + | Checks if the given character is classified as a printable character (including space) by the given locale's {{lc|std::ctype}} facet. |
===Parameters=== | ===Parameters=== | ||
− | {{ | + | {{par begin}} |
− | {{ | + | {{par|ch|character}} |
− | {{ | + | {{par|loc|locale}} |
− | {{ | + | {{par end}} |
===Return value=== | ===Return value=== | ||
− | |||
Returns {{c|true}} if the character is classified as printable, {{c|false}} otherwise. | Returns {{c|true}} if the character is classified as printable, {{c|false}} otherwise. | ||
===Possible implementation=== | ===Possible implementation=== | ||
− | {{eq fun | + | {{eq fun |
− | + | |1= | |
− | template< class | + | template<class CharT> |
− | bool isprint( | + | bool isprint(CharT ch, const std::locale& loc) |
− | return std::use_facet<std::ctype< | + | { |
+ | return std::use_facet<std::ctype<CharT>>(loc).is(std::ctype_base::print, ch); | ||
} | } | ||
}} | }} | ||
Line 30: | Line 29: | ||
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | + | |Demonstrates the use of {{tt|isprint()}} with different locales (OS-specific). | |
− | + | |code= | |
#include <iostream> | #include <iostream> | ||
#include <locale> | #include <locale> | ||
+ | |||
int main() | int main() | ||
{ | { | ||
Line 40: | Line 40: | ||
std::locale loc1("C"); | std::locale loc1("C"); | ||
std::cout << "isprint('™', C locale) returned " | std::cout << "isprint('™', C locale) returned " | ||
− | + | << std::boolalpha << std::isprint(c, loc1) << '\n'; | |
std::locale loc2("en_US.UTF-8"); | std::locale loc2("en_US.UTF-8"); | ||
Line 46: | Line 46: | ||
<< std::boolalpha << std::isprint(c, loc2) << '\n'; | << std::boolalpha << std::isprint(c, loc2) << '\n'; | ||
} | } | ||
− | + | |p=true | |
+ | |output= | ||
isprint('™', C locale) returned false | isprint('™', C locale) returned false | ||
isprint('™', Unicode locale) returned true | isprint('™', Unicode locale) returned true | ||
Line 52: | Line 53: | ||
===See also=== | ===See also=== | ||
− | {{ | + | {{dsc begin}} |
− | {{ | + | {{dsc inc|cpp/string/byte/dsc isprint}} |
− | {{ | + | {{dsc inc|cpp/string/wide/dsc iswprint}} |
− | {{ | + | {{dsc end}} |
+ | |||
+ | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 10:30, 3 October 2023
Defined in header <locale>
|
||
template< class CharT > bool isprint( CharT ch, const locale& loc ); |
||
Checks if the given character is classified as a printable character (including space) by the given locale's std::ctype facet.
Contents |
[edit] Parameters
ch | - | character |
loc | - | locale |
[edit] Return value
Returns true if the character is classified as printable, false otherwise.
[edit] Possible implementation
template<class CharT> bool isprint(CharT ch, const std::locale& loc) { return std::use_facet<std::ctype<CharT>>(loc).is(std::ctype_base::print, ch); } |
[edit] Example
Demonstrates the use of isprint()
with different locales (OS-specific).
Run this code
#include <iostream> #include <locale> int main() { const wchar_t c = L'\u2122'; // trademark sign std::locale loc1("C"); std::cout << "isprint('™', C locale) returned " << std::boolalpha << std::isprint(c, loc1) << '\n'; std::locale loc2("en_US.UTF-8"); std::cout << "isprint('™', Unicode locale) returned " << std::boolalpha << std::isprint(c, loc2) << '\n'; }
Possible output:
isprint('™', C locale) returned false isprint('™', Unicode locale) returned true
[edit] See also
checks if a character is a printing character (function) | |
checks if a wide character is a printing character (function) |