Espacios de nombres
Variantes
Acciones

Diferencia entre revisiones de «cpp/string/basic string/operator cmp»

De cppreference.com
< cpp‎ | string‎ | basic string
(Traducción completa)
m (A la par con la versión en inglés de 27 de agosto de 2020)
 
Línea 5:Línea 5:
 
{{dcl h | Compara dos objetos {{tt|basic_string}} }}
 
{{dcl h | Compara dos objetos {{tt|basic_string}} }}
 
{{dcl rev multi | num=1
 
{{dcl rev multi | num=1
| until1=c++14 | dcl1=
+
| until1=c++11 | dcl1=
 
template< class CharT, class Traits, class Alloc >
 
template< class CharT, class Traits, class Alloc >
 
bool operator==( const std::basic_string<CharT,Traits,Alloc>& lhs,
 
bool operator==( const std::basic_string<CharT,Traits,Alloc>& lhs,
Línea 20:Línea 20:
 
}}
 
}}
 
{{dcl rev multi | num=2
 
{{dcl rev multi | num=2
| until1=c++14 | dcl1=
+
| until1=c++11 | dcl1=
 
template< class CharT, class Traits, class Alloc >
 
template< class CharT, class Traits, class Alloc >
 
bool operator!=( const std::basic_string<CharT,Traits,Alloc>& lhs,
 
bool operator!=( const std::basic_string<CharT,Traits,Alloc>& lhs,
Línea 30:Línea 30:
 
}}
 
}}
 
{{dcl rev multi | num=3
 
{{dcl rev multi | num=3
| until1=c++14 | dcl1=
+
| until1=c++11 | dcl1=
 
template< class CharT, class Traits, class Alloc >
 
template< class CharT, class Traits, class Alloc >
 
bool operator<( const std::basic_string<CharT,Traits,Alloc>& lhs,
 
bool operator<( const std::basic_string<CharT,Traits,Alloc>& lhs,
Línea 40:Línea 40:
 
}}
 
}}
 
{{dcl rev multi | num=4
 
{{dcl rev multi | num=4
| until1=c++14 | dcl1=
+
| until1=c++11 | dcl1=
 
template< class CharT, class Traits, class Alloc >
 
template< class CharT, class Traits, class Alloc >
bool operator>=( const std::basic_string<CharT,Traits,Alloc>& lhs,
+
bool operator<=( const std::basic_string<CharT,Traits,Alloc>& lhs,
 
                 const std::basic_string<CharT,Traits,Alloc>& rhs );
 
                 const std::basic_string<CharT,Traits,Alloc>& rhs );
 
| until2=c++20 | dcl2=
 
| until2=c++20 | dcl2=
 
template< class CharT, class Traits, class Alloc >
 
template< class CharT, class Traits, class Alloc >
bool operator>=( const std::basic_string<CharT,Traits,Alloc>& lhs,
+
bool operator<=( const std::basic_string<CharT,Traits,Alloc>& lhs,
 
                 const std::basic_string<CharT,Traits,Alloc>& rhs ) noexcept;
 
                 const std::basic_string<CharT,Traits,Alloc>& rhs ) noexcept;
 
}}
 
}}
 
{{dcl rev multi | num=5
 
{{dcl rev multi | num=5
| until1=c++14 | dcl1=
+
| until1=c++11 | dcl1=
 
template< class CharT, class Traits, class Alloc >
 
template< class CharT, class Traits, class Alloc >
 
bool operator>( const std::basic_string<CharT,Traits,Alloc>& lhs,
 
bool operator>( const std::basic_string<CharT,Traits,Alloc>& lhs,
Línea 180:Línea 180:
 
Si al menos un parámetro es de tipo {{lc|std::string}}, {{lc|std::wstring}}, {{lc|std::u8string}}, {{lc|std::u16string}}, o {{lc|std::u32string}}, el tipo de retorno de {{tt|1=operator<=>}} es {{lc|std::strong_ordering}}.
 
Si al menos un parámetro es de tipo {{lc|std::string}}, {{lc|std::wstring}}, {{lc|std::u8string}}, {{lc|std::u16string}}, o {{lc|std::u32string}}, el tipo de retorno de {{tt|1=operator<=>}} es {{lc|std::strong_ordering}}.
 
}}
 
}}
 +
 +
===Informes de defectos===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=2064|std=C++11|before=Era inconsistente si sobrecargas que toman dos {{tt|basic_string}}s eran noexcept;<br>las sobrecargas que toman un {{tt|CharT*}} eran noexcept|after=Se hizo consistente;<br>se eliminó noexcept ya que puede ocurrir comportamiento indefinido.
 +
}}
 +
{{dr list end}}
  
 
{{langlinks|de|en|fr|it|ja|pl|pt|ru|zh}}
 
{{langlinks|de|en|fr|it|ja|pl|pt|ru|zh}}

Última revisión de 12:53 29 ago 2020

 
 
 
std::basic_string
Funciones no miembro
E/S
Comparación
operator==operator!=operator<operator>operator<=operator>=operator<=>
(hasta C++20)(hasta C++20)(hasta C++20)(hasta C++20)(hasta C++20)(C++20)
Conversión numérica (C++11)
Clases auxiliares
Guías de deducción(C++17)
 
Definido en el archivo de encabezado <string>
Compara dos objetos basic_string
(1)
template< class CharT, class Traits, class Alloc >

bool operator==( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(hasta C++11)
template< class CharT, class Traits, class Alloc >

bool operator==( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs ) noexcept;
(desde C++11)
(hasta C++20)
template< class CharT, class Traits, class Alloc >

constexpr bool
    operator==( const std::basic_string<CharT,Traits,Alloc>& lhs,

                const std::basic_string<CharT,Traits,Alloc>& rhs ) noexcept;
(desde C++20)
(2)
template< class CharT, class Traits, class Alloc >

bool operator!=( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(hasta C++11)
template< class CharT, class Traits, class Alloc >

bool operator!=( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs ) noexcept;
(desde C++11)
(hasta C++20)
(3)
template< class CharT, class Traits, class Alloc >

bool operator<( const std::basic_string<CharT,Traits,Alloc>& lhs,

                const std::basic_string<CharT,Traits,Alloc>& rhs );
(hasta C++11)
template< class CharT, class Traits, class Alloc >

bool operator<( const std::basic_string<CharT,Traits,Alloc>& lhs,

                const std::basic_string<CharT,Traits,Alloc>& rhs ) noexcept;
(desde C++11)
(hasta C++20)
(4)
template< class CharT, class Traits, class Alloc >

bool operator<=( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(hasta C++11)
template< class CharT, class Traits, class Alloc >

bool operator<=( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs ) noexcept;
(desde C++11)
(hasta C++20)
(5)
template< class CharT, class Traits, class Alloc >

bool operator>( const std::basic_string<CharT,Traits,Alloc>& lhs,

                const std::basic_string<CharT,Traits,Alloc>& rhs );
(hasta C++11)
template< class CharT, class Traits, class Alloc >

bool operator>( const std::basic_string<CharT,Traits,Alloc>& lhs,

                const std::basic_string<CharT,Traits,Alloc>& rhs ) noexcept;
(desde C++11)
(hasta C++20)
(6)
template< class CharT, class Traits, class Alloc >

bool operator>=( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(hasta C++14)
template< class CharT, class Traits, class Alloc >

bool operator>=( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs ) noexcept;
(desde C++14)
(hasta C++20)
template< class CharT, class Traits, class Alloc >

constexpr /*comp-cat*/
    operator<=>( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs ) noexcept;
(7)(desde C++20)
Compara un objeto basic_string y un array de T terminado en nulo
(8)
template< class CharT, class Traits, class Alloc >

bool operator==( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const CharT* rhs );
(hasta C++20)
template< class CharT, class Traits, class Alloc >

constexpr bool operator==( const std::basic_string<CharT,Traits,Alloc>& lhs,

                           const CharT* rhs );
(desde C++20)
template< class CharT, class Traits, class Alloc >

bool operator==( const CharT* lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(hasta C++20)
(9)
template< class CharT, class Traits, class Alloc >

bool operator!=( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const CharT* rhs );
(hasta C++20)
template< class CharT, class Traits, class Alloc >

bool operator!=( const CharT* lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(hasta C++20)
(10)
template< class CharT, class Traits, class Alloc >

bool operator<( const std::basic_string<CharT,Traits,Alloc>& lhs,

                const CharT* rhs );
(hasta C++20)
template< class CharT, class Traits, class Alloc >

bool operator<( const CharT* lhs,

                const std::basic_string<CharT,Traits,Alloc>& rhs );
(hasta C++20)
(11)
template< class CharT, class Traits, class Alloc >

bool operator<=( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const CharT* rhs );
(hasta C++20)
template< class CharT, class Traits, class Alloc >

bool operator<=( const CharT* lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(hasta C++20)
(12)
template< class CharT, class Traits, class Alloc >

bool operator>( const std::basic_string<CharT,Traits,Alloc>& lhs,

                const CharT* rhs );
(hasta C++20)
template< class CharT, class Traits, class Alloc >

bool operator>( const CharT* lhs,

                const std::basic_string<CharT,Traits,Alloc>& rhs );
(hasta C++20)
(13)
template< class CharT, class Traits, class Alloc >

bool operator>=( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const CharT* rhs );
(hasta C++20)
template< class CharT, class Traits, class Alloc >

bool operator>=( const CharT* lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(hasta C++20)
template< class CharT, class Traits, class Alloc >

constexpr /*comp-cat*/
    operator<=>( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const CharT* rhs );
(14)(desde C++20)

Compara el contenido de una cadena con otra cadena o un array de CharT terminado en nulo.

Todas las comparaciones se hacen mediante la función miembro compare() (que en sí misma se define en términos de Traits::compare()):

  • Dos cadenas son iguales si tanto el tamaño de lhs y rhs son iguales y cada carácter en lhs tiene un carácter equivalente en rhs en la misma posición.
  • El orden de las comparaciones se hace lexicográficamente--la comparación se realiza por una función equivalente a std::lexicographical_compare o std::lexicographical_compare_three_way (desde C++20).

El tipo de retorno de los operadores de comparación de tres vías (/*comp-cat*/) es Traits::comparison_category si es que existe, de lo contrario, std::weak_ordering.

(desde C++20)
1-7) Compara dos objetos basic_string.
8-14) Compara un objeto basic_string y un array de CharT terminado en nulo.

Contenido

[editar] Parámetros

lhs, rhs-Cadenas cuyo contenido hay que comparar.

[editar] Valor de retorno

1-6,8-13) true si se mantiene la comparación correspondiente, de lo contrario, false.
7,14) static_cast</*comp-cat*/>(lhs.compare(rhs) <=> 0).

[editar] Complejidad

Lineal en el tamaño de las cadenas.

[editar] Notas

Si al menos un parámetro es de tipo std::string, std::wstring, std::u8string, std::u16string, o std::u32string, el tipo de retorno de operator<=> es std::strong_ordering.

(desde C++20)

[editar] Informes de defectos

Los siguientes informes de defectos de cambio de comportamiento se aplicaron de manera retroactiva a los estándares de C++ publicados anteriormente.

IDAplicado aComportamiento según lo publicadoComportamiento correcto
LWG 2064C++11Era inconsistente si sobrecargas que toman dos basic_strings eran noexcept;
las sobrecargas que toman un CharT* eran noexcept
Se hizo consistente;
se eliminó noexcept ya que puede ocurrir comportamiento indefinido.