Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/locale/isprint"

From cppreference.com
< cpp‎ | locale
m (Text replace - "/sidebar" to "/navbar")
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 charT >
+
bool isprint( CharT ch, const locale& loc );
bool isprint( charT ch, const locale& loc );
+
 
}}
 
}}
  
Checks if the given character classified as a printable character (including space) by the given locale's {{c|std::ctype}} facet.
+
Checks if the given character is classified as a printable character (including space) by the given locale's {{lc|std::ctype}} facet.
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | ch | character}}
+
{{par|ch|character}}
{{param list item | loc | locale }}
+
{{par|loc|locale}}
{{param list end}}
+
{{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 cpp
+
{{eq fun
| 1=
+
|1=
template< class charT >
+
template<class CharT>
bool isprint( charT ch, const std::locale& loc ) {
+
bool isprint(CharT ch, const std::locale& loc)
     return std::use_facet<std::ctype<charT>>(loc).is(std::ctype_base::print, ch);
+
{
 +
     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 isprint() with different locales (OS-specific).
+
|Demonstrates the use of {{tt|isprint()}} with different locales (OS-specific).
| code=
+
|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::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';
 
}
 
}
| output=
+
|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===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/string/byte/dcl list isprint}}
+
{{dsc inc|cpp/string/byte/dsc isprint}}
{{dcl list template | cpp/string/wide/dcl list iswprint}}
+
{{dsc inc|cpp/string/wide/dsc iswprint}}
{{dcl list end}}
+
{{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).

#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) [edit]
checks if a wide character is a printing character
(function) [edit]