Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::collate(3)		      C++ Standard Libary	       std::collate(3)

NAME
       std::collate - std::collate

Synopsis
	  Defined in header <locale>
	  template< class CharT	>
	  class	collate;

	  Class	 std::collate encapsulates locale-specific collation (compari-
       son) and	hashing
	  of strings. This facet is used by std::basic_regex and  can  be  ap-
       plied, by means of
	  std::locale::operator(),  directly  to  all standard algorithms that
       expect a	string
	  comparison predicate.

	  std-collate-inheritance.svg

					  Inheritance diagram

Specializations
	  The standard library is guaranteed to	provide	the following special-
       izations	(they
	  are required to be implemented by any	locale object):

	  Defined in header <locale>
	  std::collate<char>	implements lexicographical  ordering  of  byte
       strings
	  std::collate<wchar_t>	 implements  lexicographical  ordering of wide
       strings

Member types
	  Member type Definition
	  char_type   CharT
	  string_type std::basic_string<CharT>

Member functions
	  constructor	constructs a new collate facet
			(public	member function)
	  destructor	destructs a collate facet
			(protected member function)
	  compare	invokes	do_compare
			(public	member function)
	  transform	invokes	do_transform
			(public	member function)
	  hash		invokes	do_hash
			(public	member function)

Member objects
	  static std::locale::id id id of the locale
				    (public member object)

Protected member functions
	  do_compare   compares	two strings using this facet's collation rules
	  [virtual]    (virtual	protected member function)
	  do_transform transforms a string so that collation can  be  replaced
       by comparison
	  [virtual]    (virtual	protected member function)
	  do_hash      generates an integer hash value using this facet's col-
       lation rules
	  [virtual]    (virtual	protected member function)

Example
       // Run this code

	#include <algorithm>
	#include <iostream>
	#include <locale>
	#include <string>
	#include <vector>

	int main()
	{
	    std::locale::global(std::locale("en_US.utf8"));
	    std::wcout.imbue(std::locale(""));
	    std::vector<std::wstring> v{
		L"ar", L"zebra", L"\u00f6grupp", L"Zebra",
		L"\u00e4ngel",L"\u00e5r", L"f\u00f6rnamn"
	    };

	    std::wcout << "Default locale collation order: ";
	    std::sort(v.begin(), v.end());
	    for	(auto s	: v)
		std::wcout << s	<< ' ';
	    std::wcout << '\n';

	    std::wcout << "English locale collation order: ";
	    std::sort(v.begin(), v.end(), std::locale("en_US.UTF-8"));
	    for	(auto s	: v)
		std::wcout << s	<< ' ';
	    std::wcout << '\n';

	    std::wcout << "Swedish locale collation order: ";
	    std::sort(v.begin(), v.end(), std::locale("sv_SE.UTF-8"));
	    for	(auto s	: v)
		std::wcout << s	<< ' ';
	    std::wcout << '\n';
	}

Output:
	Default	locale collation order:	Zebra ar frnamn	zebra ngel r grupp
	English	locale collation order:	ngel ar	r frnamn grupp zebra Zebra
	Swedish	locale collation order:	ar frnamn zebra	Zebra r	ngel grupp

See also
			 lexicographically compares two	strings	using this lo-
       cale's collate
	  operator()	 facet
			 (public member	function of std::locale)
	  collate_byname  represents  the system-supplied std::collate for the
       named locale
			 (class	template)

http://cppreference.com		  2024.06.10		       std::collate(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::collate&sektion=3&manpath=FreeBSD+Ports+15.1.quarterly>

home | help